Skip to content

Commit 8f3a769

Browse files
Chief-Rishabravisuhag
authored andcommitted
refactor: consistent err handling for OpenTelemetry
1 parent 38c01c9 commit 8f3a769

File tree

7 files changed

+124
-101
lines changed

7 files changed

+124
-101
lines changed

agent/agent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (r *Agent) setupProcessor(ctx context.Context, pr recipe.PluginRecipe, str
236236
return fmt.Errorf("find processor %q: %w", pr.Name, err)
237237
}
238238

239-
proc, err = otelmw.WithProcessorMW(proc, pr.Name, recipeName)
239+
proc = otelmw.WithProcessor(pr.Name, recipeName)(proc)
240240
if err != nil {
241241
return fmt.Errorf("wrap processor %q: %w", pr.Name, err)
242242
}
@@ -269,7 +269,7 @@ func (r *Agent) setupSink(ctx context.Context, sr recipe.PluginRecipe, stream *s
269269
return fmt.Errorf("find sink %q: %w", sr.Name, err)
270270
}
271271

272-
sink, err = otelmw.WithSinkMW(sink, sr.Name, recipeName)
272+
sink = otelmw.WithSink(sr.Name, recipeName)(sink)
273273
if err != nil {
274274
return fmt.Errorf("wrap otel sink %q: %w", sr.Name, err)
275275
}

cmd/run.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,7 @@ func RunCmd() *cobra.Command {
8888
}
8989
defer doneOtlp()
9090

91-
mt, err := metrics.NewOtelMonitor()
92-
if err != nil {
93-
return err
94-
}
95-
96-
if mt != nil {
97-
mts = append(mts, mt)
98-
}
91+
mts = append(mts, metrics.NewOtelMonitor())
9992
}
10093

10194
runner := agent.NewAgent(agent.Config{

metrics/opentelemetry_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ func TestOtelMonitor_RecordRun(t *testing.T) {
3232
defer done()
3333
assert.Nil(t, err)
3434

35-
monitor, err := metrics.NewOtelMonitor()
35+
monitor := metrics.NewOtelMonitor()
3636

3737
monitor.RecordRun(ctx, agent.Run{Recipe: recipe, DurationInMs: duration, RecordCount: recordCount, Success: false})
3838

39-
assert.Nil(t, err)
4039
assert.NotNil(t, monitor)
4140
assert.NotNil(t, done)
4241
})
@@ -62,7 +61,7 @@ func TestOtelMonitor_RecordPlugin(t *testing.T) {
6261
defer done()
6362
assert.Nil(t, err)
6463

65-
monitor, err := metrics.NewOtelMonitor()
64+
monitor := metrics.NewOtelMonitor()
6665

6766
monitor.RecordPlugin(context.Background(),
6867
agent.PluginInfo{
@@ -71,7 +70,6 @@ func TestOtelMonitor_RecordPlugin(t *testing.T) {
7170
PluginType: "sink",
7271
Success: true,
7372
})
74-
assert.Nil(t, err)
7573
assert.NotNil(t, monitor)
7674
assert.NotNil(t, done)
7775
})

metrics/otel_monitor.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,39 @@ import (
1111

1212
// OtelMonitor represents the otel monitor.
1313
type OtelMonitor struct {
14-
recipeDuration metric.Int64Histogram
14+
recipeDuration metric.Float64Histogram
1515
extractorRetries metric.Int64Counter
1616
assetsExtracted metric.Int64Counter
1717
sinkRetries metric.Int64Counter
1818
}
1919

20-
func NewOtelMonitor() (*OtelMonitor, error) {
20+
func NewOtelMonitor() *OtelMonitor {
2121
// init meters
22-
meter := otel.Meter("")
23-
recipeDuration, err := meter.Int64Histogram("meteor.recipe.duration", metric.WithUnit("ms"))
24-
if err != nil {
25-
return nil, err
26-
}
22+
meter := otel.Meter("github.com/raystack/meteor/metrics")
23+
recipeDuration, err := meter.Float64Histogram("meteor.recipe.duration", metric.WithUnit("s"))
24+
handleOtelErr(err)
2725

2826
extractorRetries, err := meter.Int64Counter("meteor.extractor.retries")
29-
if err != nil {
30-
return nil, err
31-
}
27+
handleOtelErr(err)
3228

3329
assetsExtracted, err := meter.Int64Counter("meteor.assets.extracted")
34-
if err != nil {
35-
return nil, err
36-
}
30+
handleOtelErr(err)
3731

3832
sinkRetries, err := meter.Int64Counter("meteor.sink.retries")
39-
if err != nil {
40-
return nil, err
41-
}
33+
handleOtelErr(err)
4234

4335
return &OtelMonitor{
4436
recipeDuration: recipeDuration,
4537
extractorRetries: extractorRetries,
4638
assetsExtracted: assetsExtracted,
4739
sinkRetries: sinkRetries,
48-
}, nil
40+
}
4941
}
5042

5143
// RecordRun records a run behavior
5244
func (m *OtelMonitor) RecordRun(ctx context.Context, run agent.Run) {
5345
m.recipeDuration.Record(ctx,
54-
int64(run.DurationInMs),
46+
float64(run.DurationInMs)/1000.0,
5547
metric.WithAttributes(
5648
attribute.String("recipe_name", run.Recipe.Name),
5749
attribute.String("extractor", run.Recipe.Source.Name),
@@ -89,3 +81,9 @@ func (m *OtelMonitor) RecordSinkRetryCount(ctx context.Context, pluginInfo agent
8981
attribute.Int64("batch_size", int64(pluginInfo.BatchSize)),
9082
))
9183
}
84+
85+
func handleOtelErr(err error) {
86+
if err != nil {
87+
otel.Handle(err)
88+
}
89+
}

metrics/otelmw/processorsmw.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,43 @@ import (
1111
"go.opentelemetry.io/otel/metric"
1212
)
1313

14-
type ProcessorMW struct {
14+
type Processor struct {
1515
next plugins.Processor
1616
duration metric.Int64Histogram
1717
pluginName string
1818
recipeName string
1919
}
2020

21-
func WithProcessorMW(p plugins.Processor, pluginName, recipeName string) (plugins.Processor, error) {
22-
meter := otel.Meter("")
23-
24-
processorDuration, err := meter.Int64Histogram("meteor.processor.duration", metric.WithUnit("ms"))
21+
func WithProcessor(pluginName, recipeName string) func(plugins.Processor) plugins.Processor {
22+
processorDuration, err := otel.Meter("github.com/raystack/meteor/metrics/otelmw").
23+
Int64Histogram("meteor.processor.duration", metric.WithUnit("ms"))
2524
if err != nil {
26-
return nil, err
25+
otel.Handle(err)
2726
}
2827

29-
return &ProcessorMW{
30-
next: p,
31-
duration: processorDuration,
32-
pluginName: pluginName,
33-
recipeName: recipeName,
34-
}, nil
28+
return func(p plugins.Processor) plugins.Processor {
29+
return &Processor{
30+
next: p,
31+
duration: processorDuration,
32+
pluginName: pluginName,
33+
recipeName: recipeName,
34+
}
35+
}
3536
}
3637

37-
func (mw *ProcessorMW) Init(ctx context.Context, cfg plugins.Config) error {
38+
func (mw *Processor) Init(ctx context.Context, cfg plugins.Config) error {
3839
return mw.next.Init(ctx, cfg)
3940
}
4041

41-
func (mw *ProcessorMW) Info() plugins.Info {
42+
func (mw *Processor) Info() plugins.Info {
4243
return mw.next.Info()
4344
}
4445

45-
func (mw *ProcessorMW) Validate(cfg plugins.Config) error {
46+
func (mw *Processor) Validate(cfg plugins.Config) error {
4647
return mw.next.Validate(cfg)
4748
}
4849

49-
func (mw *ProcessorMW) Process(ctx context.Context, src models.Record) (dst models.Record, err error) {
50+
func (mw *Processor) Process(ctx context.Context, src models.Record) (dst models.Record, err error) {
5051
defer func(start time.Time) {
5152
mw.duration.Record(ctx,
5253
time.Since(start).Milliseconds(),

metrics/otelmw/sinksmw.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,47 @@ import (
1111
"go.opentelemetry.io/otel/metric"
1212
)
1313

14-
type SinksMW struct {
14+
type Sinks struct {
1515
next plugins.Syncer
1616
duration metric.Int64Histogram
1717
pluginName string
1818
recipeName string
1919
}
2020

21-
func WithSinkMW(s plugins.Syncer, pluginName, recipeName string) (plugins.Syncer, error) {
22-
meter := otel.Meter("")
23-
24-
sinkDuration, err := meter.Int64Histogram("meteor.sink.duration", metric.WithUnit("ms"))
21+
func WithSink(pluginName, recipeName string) func(plugins.Syncer) plugins.Syncer {
22+
sinkDuration, err := otel.Meter("github.com/raystack/meteor/metrics/otelmw").
23+
Int64Histogram("meteor.sink.duration", metric.WithUnit("ms"))
2524
if err != nil {
26-
return nil, err
25+
otel.Handle(err)
2726
}
2827

29-
return &SinksMW{
30-
next: s,
31-
duration: sinkDuration,
32-
pluginName: pluginName,
33-
recipeName: recipeName,
34-
}, nil
28+
return func(s plugins.Syncer) plugins.Syncer {
29+
return &Sinks{
30+
next: s,
31+
duration: sinkDuration,
32+
pluginName: pluginName,
33+
recipeName: recipeName,
34+
}
35+
}
3536
}
3637

37-
func (mw *SinksMW) Init(ctx context.Context, cfg plugins.Config) error {
38+
func (mw *Sinks) Init(ctx context.Context, cfg plugins.Config) error {
3839
return mw.next.Init(ctx, cfg)
3940
}
4041

41-
func (mw *SinksMW) Info() plugins.Info {
42+
func (mw *Sinks) Info() plugins.Info {
4243
return mw.next.Info()
4344
}
4445

45-
func (mw *SinksMW) Validate(cfg plugins.Config) error {
46+
func (mw *Sinks) Validate(cfg plugins.Config) error {
4647
return mw.next.Validate(cfg)
4748
}
4849

49-
func (mw *SinksMW) Close() error {
50+
func (mw *Sinks) Close() error {
5051
return mw.next.Close()
5152
}
5253

53-
func (mw *SinksMW) Sink(ctx context.Context, batch []models.Record) (err error) {
54+
func (mw *Sinks) Sink(ctx context.Context, batch []models.Record) (err error) {
5455
defer func(start time.Time) {
5556
mw.duration.Record(ctx,
5657
time.Since(start).Milliseconds(),

0 commit comments

Comments
 (0)