Skip to content

Commit

Permalink
refactor: move OtelManager initialization to constructor (#2301)
Browse files Browse the repository at this point in the history
  • Loading branch information
iskakaushik authored Nov 27, 2024
1 parent 04deaf0 commit 04036cf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
15 changes: 4 additions & 11 deletions flow/cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"runtime"

"github.com/grafana/pyroscope-go"
"go.opentelemetry.io/otel/metric"
"go.temporal.io/sdk/client"
temporalotel "go.temporal.io/sdk/contrib/opentelemetry"
"go.temporal.io/sdk/worker"
Expand Down Expand Up @@ -159,18 +158,12 @@ func WorkerSetup(opts *WorkerSetupOptions) (*workerSetupResponse, error) {

var otelManager *otel_metrics.OtelManager
if opts.EnableOtelMetrics {
metricsProvider, metricsErr := otel_metrics.SetupPeerDBMetricsProvider("flow-worker")
if metricsErr != nil {
return nil, metricsErr
}
otelManager = &otel_metrics.OtelManager{
MetricsProvider: metricsProvider,
Meter: metricsProvider.Meter("io.peerdb.flow-worker"),
Float64GaugesCache: make(map[string]metric.Float64Gauge),
Int64GaugesCache: make(map[string]metric.Int64Gauge),
Int64CountersCache: make(map[string]metric.Int64Counter),
otelManager, err = otel_metrics.NewOtelManager()
if err != nil {
return nil, fmt.Errorf("unable to create otel manager: %w", err)
}
}

w.RegisterActivity(&activities.FlowableActivity{
CatalogPool: conn,
Alerter: alerting.NewAlerter(context.Background(), conn),
Expand Down
15 changes: 15 additions & 0 deletions flow/otel_metrics/otel_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ type OtelManager struct {
Int64CountersCache map[string]metric.Int64Counter
}

func NewOtelManager() (*OtelManager, error) {
metricsProvider, err := SetupPeerDBMetricsProvider("flow-worker")
if err != nil {
return nil, err
}

return &OtelManager{
MetricsProvider: metricsProvider,
Meter: metricsProvider.Meter("io.peerdb.flow-worker"),
Float64GaugesCache: make(map[string]metric.Float64Gauge),
Int64GaugesCache: make(map[string]metric.Int64Gauge),
Int64CountersCache: make(map[string]metric.Int64Counter),
}, nil
}

func (om *OtelManager) Close(ctx context.Context) error {
return om.MetricsProvider.Shutdown(ctx)
}
Expand Down

0 comments on commit 04036cf

Please sign in to comment.