Skip to content

Commit

Permalink
config and shutdown cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
smithclay committed Oct 2, 2023
1 parent 689c399 commit fe6c234
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
5 changes: 4 additions & 1 deletion src/mapper/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
44 changes: 13 additions & 31 deletions src/mapper/pkg/otelexporter/otel_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package otelexporter

import (
"context"
"os"
"time"

"github.com/otterize/network-mapper/src/mapper/pkg/intentsstore"
Expand All @@ -21,6 +20,7 @@ import (

type OtelExporter struct {
config Config
meterProvider *sdk.MeterProvider
counter metric.Int64Counter
intentsHolder *intentsstore.IntentsHolder
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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) {
Expand All @@ -134,6 +115,7 @@ func (o *OtelExporter) PeriodicIntentsExport(ctx context.Context) {
o.countDiscoveredIntents(ctx)

case <-ctx.Done():
o.meterProvider.Shutdown(ctx)
return
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/mapper/pkg/otelexporter/otel_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fe6c234

Please sign in to comment.