Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
Signed-off-by: Kristof Gyuracz <[email protected]>
  • Loading branch information
kristofgyuracz committed Mar 26, 2024
1 parent 1b29dbd commit ca0b4e7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ $(CONTROLLER_GEN): $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)


ENVTEST_OTEL_OPERATOR_VERSION=v0.94.0
ENVTEST_OTEL_OPERATOR_VERSION=v0.96.0
# Download CRDs for envtest
crddir/github.com/open-telemetry/opentelemetry-operator:
git clone --depth 1 --branch ${ENVTEST_OTEL_OPERATOR_VERSION} https://github.com/open-telemetry/opentelemetry-operator.git crddir/github.com/open-telemetry/opentelemetry-operator
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/telemetry/collector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (r *CollectorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
return ctrl.Result{}, err
}

otelConfig, err := otelConfigInput.ToIntermediateRepresentation().ToYAML()
otelConfig, err := otelConfigInput.ToIntermediateRepresentation(ctx).ToYAML()
if err != nil {
return ctrl.Result{}, err
}
Expand Down
28 changes: 17 additions & 11 deletions internal/controller/telemetry/otel_conf_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
package telemetry

import (
"context"
"errors"
"fmt"
"slices"
"strings"

"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kube-logging/telemetry-controller/api/telemetry/v1alpha1"
)
Expand Down Expand Up @@ -99,31 +102,32 @@ type OtelColConfigIR struct {
Services Services `yaml:"service,omitempty"`
}

func (cfgInput *OtelColConfigInput) generateExporters() map[string]any {
func (cfgInput *OtelColConfigInput) generateExporters(ctx context.Context) map[string]any {
exporters := map[string]any{}
// TODO: add proper error handling

maps.Copy(exporters, cfgInput.generateOTLPExporters())
maps.Copy(exporters, cfgInput.generateLokiExporters())
maps.Copy(exporters, cfgInput.generateOTLPExporters(ctx))
maps.Copy(exporters, cfgInput.generateLokiExporters(ctx))
exporters["logging/debug"] = map[string]any{
"verbosity": "detailed",
}
return exporters
}

func (cfgInput *OtelColConfigInput) generateOTLPExporters() map[string]any {
func (cfgInput *OtelColConfigInput) generateOTLPExporters(ctx context.Context) map[string]any {
logger := log.FromContext(ctx)
var result = make(map[string]any)

for _, output := range cfgInput.Outputs {
if output.Spec.OTLP != nil {
name := fmt.Sprintf("otlp/%s_%s", output.Namespace, output.Name)
otlpGrpcValuesMarshaled, err := yaml.Marshal(output.Spec.OTLP)
if err != nil {
panic(fmt.Errorf("failed to compile config for output %q", output.NamespacedName().String()))
logger.Error(errors.New("failed to compile config for output"), "failed to compile config for output %q", output.NamespacedName().String())
}
var otlpGrpcValues map[string]any
if err := yaml.Unmarshal(otlpGrpcValuesMarshaled, &otlpGrpcValues); err != nil {
panic(fmt.Errorf("failed to compile config for output %q", output.NamespacedName().String()))
logger.Error(errors.New("failed to compile config for output"), "failed to compile config for output %q", output.NamespacedName().String())
}

result[name] = otlpGrpcValues
Expand All @@ -132,7 +136,9 @@ func (cfgInput *OtelColConfigInput) generateOTLPExporters() map[string]any {

return result
}
func (cfgInput *OtelColConfigInput) generateLokiExporters() map[string]any {
func (cfgInput *OtelColConfigInput) generateLokiExporters(ctx context.Context) map[string]any {
logger := log.FromContext(ctx)

var result = make(map[string]any)

for _, output := range cfgInput.Outputs {
Expand All @@ -142,11 +148,11 @@ func (cfgInput *OtelColConfigInput) generateLokiExporters() map[string]any {
name := fmt.Sprintf("loki/%s_%s", output.Namespace, output.Name)
lokiHTTPValuesMarshaled, err := yaml.Marshal(output.Spec.Loki)
if err != nil {
return result
logger.Error(errors.New("failed to compile config for output"), "failed to compile config for output %q", output.NamespacedName().String())
}
var lokiHTTPValues map[string]any
if err := yaml.Unmarshal(lokiHTTPValuesMarshaled, &lokiHTTPValues); err != nil {
return result
logger.Error(errors.New("failed to compile config for output"), "failed to compile config for output %q", output.NamespacedName().String())
}

result[name] = lokiHTTPValues
Expand Down Expand Up @@ -565,11 +571,11 @@ func (cfgInput *OtelColConfigInput) generateDefaultKubernetesReceiver() map[stri

}

func (cfgInput *OtelColConfigInput) ToIntermediateRepresentation() *OtelColConfigIR {
func (cfgInput *OtelColConfigInput) ToIntermediateRepresentation(ctx context.Context) *OtelColConfigIR {
result := OtelColConfigIR{}

// Get outputs based tenant names
result.Exporters = cfgInput.generateExporters()
result.Exporters = cfgInput.generateExporters(ctx)

// Add processors
result.Processors = cfgInput.generateProcessors()
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/telemetry/otel_conf_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestOtelColConfComplex(t *testing.T) {
inputCfg.TenantSubscriptionMap[tenant.Name] = seqs.ToSlice(mapseqs.KeysOf(inputCfg.Subscriptions))

// IR
generatedIR := inputCfg.ToIntermediateRepresentation()
generatedIR := inputCfg.ToIntermediateRepresentation(ctx)

// Final YAML
_, err := generatedIR.ToYAML()
Expand Down

0 comments on commit ca0b4e7

Please sign in to comment.