From fe6c23476317bbb66a682e6333379b380523b787 Mon Sep 17 00:00:00 2001 From: smithclay Date: Mon, 2 Oct 2023 11:54:33 -0700 Subject: [PATCH] config and shutdown cleanup --- src/mapper/cmd/main.go | 5 ++- src/mapper/pkg/otelexporter/otel_exporter.go | 44 ++++++------------- .../pkg/otelexporter/otel_exporter_test.go | 3 +- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/mapper/cmd/main.go b/src/mapper/cmd/main.go index 29bd8300..491e9cbe 100644 --- a/src/mapper/cmd/main.go +++ b/src/mapper/cmd/main.go @@ -129,7 +129,10 @@ func main() { otelCtx, otelCancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer otelCancel() otelExporterConfig := otelexporter.ConfigFromViper() - otelExporter := otelexporter.NewOtelExporter(otelCtx, intentsHolder, otelExporterConfig) + otelExporter, err := otelexporter.NewOtelExporter(otelCtx, intentsHolder, otelExporterConfig) + if err != nil { + logrus.WithError(err).Fatal("Failed to initialize otel exporter") + } go otelExporter.PeriodicIntentsExport(otelCtx) telemetrysender.SetGlobalVersion(version.Version()) diff --git a/src/mapper/pkg/otelexporter/otel_exporter.go b/src/mapper/pkg/otelexporter/otel_exporter.go index f12da138..39618329 100644 --- a/src/mapper/pkg/otelexporter/otel_exporter.go +++ b/src/mapper/pkg/otelexporter/otel_exporter.go @@ -2,7 +2,6 @@ package otelexporter import ( "context" - "os" "time" "github.com/otterize/network-mapper/src/mapper/pkg/intentsstore" @@ -21,6 +20,7 @@ import ( type OtelExporter struct { config Config + meterProvider *sdk.MeterProvider counter metric.Int64Counter intentsHolder *intentsstore.IntentsHolder } @@ -33,29 +33,17 @@ func newResource() (*resource.Resource, error) { )) } -const DefaultMetricEndpoint = "ingest.lightstep.com:443" - // uses same name as expected in opentelemetry-collector-contrib's servicegraphprocessor const CounterMetricName = "traces_service_graph_request_total" const ClientAttributeName = "client" const ServerAttributeName = "server" -func getenv(key, fallback string) string { - value := os.Getenv(key) - if len(value) == 0 { - return fallback - } - return value -} - func newMeterProvider(ctx context.Context, res *resource.Resource, exportInterval time.Duration) (*sdk.MeterProvider, error) { - metricExporter, err := otlpmetricgrpc.New(ctx, - otlpmetricgrpc.WithEndpoint(getenv("OTEL_EXPORTER_OTLP_ENDPOINT", DefaultMetricEndpoint)), - otlpmetricgrpc.WithHeaders(map[string]string{ - "lightstep-access-token": os.Getenv("LS_ACCESS_TOKEN"), - }), - otlpmetricgrpc.WithTimeout(7*time.Second), - ) + // SDK automatically configured via environment variables: + // - OTEL_EXPORTER_OTLP_ENDPOINT + // - OTEL_EXPORTER_OTLP_HEADERS + // - OTEL_EXPORTER_OTLP_TIMEOUT (...) + metricExporter, err := otlpmetricgrpc.New(ctx) if err != nil { return nil, err } @@ -83,39 +71,32 @@ func newMeterProvider(ctx context.Context, res *resource.Resource, exportInterva return meterProvider, nil } -func NewOtelExporter(ctx context.Context, ih *intentsstore.IntentsHolder, config Config) *OtelExporter { +func NewOtelExporter(ctx context.Context, ih *intentsstore.IntentsHolder, config Config) (*OtelExporter, error) { res, err := newResource() if err != nil { - panic(err) + return nil, err } meterProvider, err := newMeterProvider(ctx, res, config.ExportInterval) if err != nil { - panic(err) + return nil, err } - // TODO: this is not the right place to handle shutdown - // defer func() { - // err := meterProvider.Shutdown(context.Background()) - // if err != nil { - // logrus.Fatalln(err) - // } - // }() - var meter = meterProvider.Meter("otelexporter") edgeCounter, err := meter.Int64Counter( CounterMetricName, metric.WithDescription("Count of edges between two nodes"), ) if err != nil { - panic(err) + return nil, err } return &OtelExporter{ intentsHolder: ih, config: config, counter: edgeCounter, - } + meterProvider: meterProvider, + }, nil } func (o *OtelExporter) countDiscoveredIntents(ctx context.Context) { @@ -134,6 +115,7 @@ func (o *OtelExporter) PeriodicIntentsExport(ctx context.Context) { o.countDiscoveredIntents(ctx) case <-ctx.Done(): + o.meterProvider.Shutdown(ctx) return } } diff --git a/src/mapper/pkg/otelexporter/otel_exporter_test.go b/src/mapper/pkg/otelexporter/otel_exporter_test.go index 4004986b..269ac412 100644 --- a/src/mapper/pkg/otelexporter/otel_exporter_test.go +++ b/src/mapper/pkg/otelexporter/otel_exporter_test.go @@ -27,7 +27,8 @@ func (o *OTelExporterTestSuite) SetupTest() { } func (o *OTelExporterTestSuite) BeforeTest(_, testName string) { - o.otelExporter = NewOtelExporter(context.Background(), o.intentsHolder, Config{ExportInterval: 1 * time.Second}) + otelExporter, _ := NewOtelExporter(context.Background(), o.intentsHolder, Config{ExportInterval: 1 * time.Second}) + o.otelExporter = otelExporter } func (o *OTelExporterTestSuite) addIntent(source string, srcNamespace string, destination string, dstNamespace string) {