Skip to content

Commit

Permalink
Use context.TODO,rebase and clenaup opts handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Achooo committed Apr 28, 2023
1 parent 17a4095 commit 84a6932
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
20 changes: 9 additions & 11 deletions agent/hcp/telemetry/otel_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
type OTELSinkOpts struct {
Reader otelsdk.Reader
Logger hclog.Logger
Ctx context.Context
}

type OTELSink struct {
spaceReplacer *strings.Replacer
logger hclog.Logger
ctx context.Context

meterProvider *otelsdk.MeterProvider
meter *otelmetric.Meter
Expand All @@ -43,16 +41,17 @@ type OTELSink struct {
}

func NewOTELReader(client client.MetricsClient, endpoint string, exportInterval time.Duration) otelsdk.Reader {
exporter := &OTELExporter{
client: client,
endpoint: endpoint,
}
exporter := NewOTELExporter(client, endpoint)
return otelsdk.NewPeriodicReader(exporter, otelsdk.WithInterval(exportInterval))
}

func NewOTELSink(opts *OTELSinkOpts) (*OTELSink, error) {
if opts.Logger == nil || opts.Reader == nil || opts.Ctx == nil {
return nil, fmt.Errorf("failed to init OTEL sink: provide valid OTELSinkOpts")
if opts.Logger == nil {
return nil, fmt.Errorf("failed to init OTEL sink: provide valid OTELSinkOpts Logger")
}

if opts.Reader == nil {
return nil, fmt.Errorf("failed to init OTEL sink: provide valid OTELSinkOpts Reader")
}

// Setup OTEL Metrics SDK to aggregate, convert and export metrics periodically.
Expand All @@ -68,7 +67,6 @@ func NewOTELSink(opts *OTELSinkOpts) (*OTELSink, error) {
return &OTELSink{
spaceReplacer: strings.NewReplacer(" ", "_"),
logger: opts.Logger.Named("otel_sink"),
ctx: opts.Ctx,
meterProvider: meterProvider,
meter: &meter,
mutex: sync.Mutex{},
Expand Down Expand Up @@ -135,7 +133,7 @@ func (o *OTELSink) AddSampleWithLabels(key []string, val float32, labels []gomet
}

attrs := toAttributes(labels)
(*inst).Record(o.ctx, float64(val), attrs...)
(*inst).Record(context.TODO(), float64(val), attrs...)
}

// IncrCounterWithLabels emits a Consul counter metric that gets registed by an OpenTelemetry Histogram instrument.
Expand All @@ -158,7 +156,7 @@ func (o *OTELSink) IncrCounterWithLabels(key []string, val float32, labels []gom
}

attrs := toAttributes(labels)
(*inst).Add(o.ctx, float64(val), attrs...)
(*inst).Add(context.TODO(), float64(val), attrs...)
}

// EmitKey unsupported.
Expand Down
7 changes: 3 additions & 4 deletions agent/hcp/telemetry/otel_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ func TestNewOTELSink(t *testing.T) {
opts *OTELSinkOpts
}{
"failsWithEmptyLogger": {
wantErr: "failed to init OTEL sink: provide valid OTELSinkOpts",
wantErr: "failed to init OTEL sink: provide valid OTELSinkOpts Logger",
opts: &OTELSinkOpts{
Logger: nil,
Reader: metric.NewManualReader(),
},
},
"failsWithEmptyExporter": {
wantErr: "failed to init OTEL sink: provide valid OTELSinkOpts",
"failsWithEmptyReader": {
wantErr: "failed to init OTEL sink: provide valid OTELSinkOpts Reader",
opts: &OTELSinkOpts{
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
Reader: nil,
Expand Down Expand Up @@ -149,7 +149,6 @@ func TestOTELSink(t *testing.T) {
opts := &OTELSinkOpts{
Logger: hclog.New(&hclog.LoggerOptions{Output: io.Discard}),
Reader: reader,
Ctx: ctx,
}

sink, err := NewOTELSink(opts)
Expand Down

0 comments on commit 84a6932

Please sign in to comment.