From a92adb612f65fba799472ca5916c9f5f0b4d908b Mon Sep 17 00:00:00 2001 From: Anuarbek Mukhanov Date: Thu, 11 Jul 2024 14:10:27 +0500 Subject: [PATCH] remove sdk/metric as per OpenTelemetry specs see: https://github.com/open-telemetry/opentelemetry-go/issues/3454 --- sdk/metric/instrument.go | 144 -------------------- sdk/metric/instrument_provider.go | 214 ------------------------------ sdk/metric/meter.go | 64 --------- sdk/metric/provider.go | 73 ---------- 4 files changed, 495 deletions(-) delete mode 100644 sdk/metric/instrument.go delete mode 100644 sdk/metric/instrument_provider.go delete mode 100644 sdk/metric/meter.go delete mode 100644 sdk/metric/provider.go diff --git a/sdk/metric/instrument.go b/sdk/metric/instrument.go deleted file mode 100644 index a3ce08e..0000000 --- a/sdk/metric/instrument.go +++ /dev/null @@ -1,144 +0,0 @@ -package metric - -import ( - "context" - - "github.com/tel-io/tel/v2/pkg/cardinalitydetector" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" - "go.opentelemetry.io/otel/metric/instrument/asyncint64" - "go.opentelemetry.io/otel/metric/instrument/syncfloat64" - "go.opentelemetry.io/otel/metric/instrument/syncint64" -) - -type asyncFloat64Counter struct { - asyncfloat64.Counter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *asyncFloat64Counter) Observe(ctx context.Context, x float64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.Counter.Observe(ctx, x, attrs...) - } -} - -type asyncFloat64Gauge struct { - asyncfloat64.Gauge - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (g *asyncFloat64Gauge) Observe(ctx context.Context, x float64, attrs ...attribute.KeyValue) { - if g.cardinalityDetector.CheckAttrs(attrs) { - g.Gauge.Observe(ctx, x, attrs...) - } -} - -type asyncFloat64UpDownCounter struct { - asyncfloat64.UpDownCounter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *asyncFloat64UpDownCounter) Observe(ctx context.Context, x float64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.UpDownCounter.Observe(ctx, x, attrs...) - } -} - -type asyncInt64Counter struct { - asyncint64.Counter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *asyncInt64Counter) Observe(ctx context.Context, x int64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.Counter.Observe(ctx, x, attrs...) - } -} - -type asyncInt64Gauge struct { - asyncint64.Gauge - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (g *asyncInt64Gauge) Observe(ctx context.Context, x int64, attrs ...attribute.KeyValue) { - if g.cardinalityDetector.CheckAttrs(attrs) { - g.Gauge.Observe(ctx, x, attrs...) - } -} - -type asyncInt64UpDownCounter struct { - asyncint64.UpDownCounter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *asyncInt64UpDownCounter) Observe(ctx context.Context, x int64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.UpDownCounter.Observe(ctx, x, attrs...) - } -} - -type syncFloat64Counter struct { - syncfloat64.Counter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *syncFloat64Counter) Add(ctx context.Context, incr float64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.Counter.Add(ctx, incr, attrs...) - } -} - -type syncFloat64Histogram struct { - syncfloat64.Histogram - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (h *syncFloat64Histogram) Record(ctx context.Context, incr float64, attrs ...attribute.KeyValue) { - if h.cardinalityDetector.CheckAttrs(attrs) { - h.Histogram.Record(ctx, incr, attrs...) - } -} - -type syncFloat64UpDownCounter struct { - syncfloat64.UpDownCounter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *syncFloat64UpDownCounter) Add(ctx context.Context, incr float64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.UpDownCounter.Add(ctx, incr, attrs...) - } -} - -type syncInt64Counter struct { - syncint64.Counter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *syncInt64Counter) Add(ctx context.Context, incr int64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.Counter.Add(ctx, incr, attrs...) - } -} - -type syncInt64Histogram struct { - syncint64.Histogram - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (h *syncInt64Histogram) Record(ctx context.Context, incr int64, attrs ...attribute.KeyValue) { - if h.cardinalityDetector.CheckAttrs(attrs) { - h.Histogram.Record(ctx, incr, attrs...) - } -} - -type syncInt64UpDownCounter struct { - syncint64.UpDownCounter - cardinalityDetector cardinalitydetector.CardinalityDetector -} - -func (c *syncInt64UpDownCounter) Add(ctx context.Context, incr int64, attrs ...attribute.KeyValue) { - if c.cardinalityDetector.CheckAttrs(attrs) { - c.UpDownCounter.Add(ctx, incr, attrs...) - } -} diff --git a/sdk/metric/instrument_provider.go b/sdk/metric/instrument_provider.go deleted file mode 100644 index 242ba52..0000000 --- a/sdk/metric/instrument_provider.go +++ /dev/null @@ -1,214 +0,0 @@ -package metric - -import ( - "errors" - - "github.com/tel-io/tel/v2/pkg/cardinalitydetector" - "go.opentelemetry.io/otel/metric/instrument" - "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" - "go.opentelemetry.io/otel/metric/instrument/asyncint64" - "go.opentelemetry.io/otel/metric/instrument/syncfloat64" - "go.opentelemetry.io/otel/metric/instrument/syncint64" -) - -var errLimitExceededCardinalityDetector = errors.New("limit exceeded cardinality detector") - -type asyncFloat64Provider struct { - asyncfloat64.InstrumentProvider - cardinalityDetectorPool cardinalitydetector.CardinalityDetectorPool -} - -// Counter implements asyncfloat64.InstrumentProvider. -func (p *asyncFloat64Provider) Counter(name string, opts ...instrument.Option) (asyncfloat64.Counter, error) { - c, err := p.InstrumentProvider.Counter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &asyncFloat64Counter{c, cardinalityDetector}, nil -} - -// Gauge implements asyncfloat64.InstrumentProvider. -func (p *asyncFloat64Provider) Gauge(name string, opts ...instrument.Option) (asyncfloat64.Gauge, error) { - g, err := p.InstrumentProvider.Gauge(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &asyncFloat64Gauge{g, cardinalityDetector}, nil -} - -// UpDownCounter implements asyncfloat64.InstrumentProvider. -func (p *asyncFloat64Provider) UpDownCounter(name string, opts ...instrument.Option) (asyncfloat64.UpDownCounter, error) { - c, err := p.InstrumentProvider.UpDownCounter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &asyncFloat64UpDownCounter{c, cardinalityDetector}, nil -} - -type asyncInt64Provider struct { - asyncint64.InstrumentProvider - cardinalityDetectorPool cardinalitydetector.CardinalityDetectorPool -} - -// Counter implements asyncint64.InstrumentProvider. -func (p *asyncInt64Provider) Counter(name string, opts ...instrument.Option) (asyncint64.Counter, error) { - c, err := p.InstrumentProvider.Counter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &asyncInt64Counter{c, cardinalityDetector}, nil -} - -// Gauge implements asyncint64.InstrumentProvider. -func (p *asyncInt64Provider) Gauge(name string, opts ...instrument.Option) (asyncint64.Gauge, error) { - g, err := p.InstrumentProvider.Gauge(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &asyncInt64Gauge{g, cardinalityDetector}, nil -} - -// UpDownCounter implements asyncint64.InstrumentProvider. -func (p *asyncInt64Provider) UpDownCounter(name string, opts ...instrument.Option) (asyncint64.UpDownCounter, error) { - c, err := p.InstrumentProvider.UpDownCounter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &asyncInt64UpDownCounter{c, cardinalityDetector}, nil -} - -type syncFloat64Provider struct { - syncfloat64.InstrumentProvider - cardinalityDetectorPool cardinalitydetector.CardinalityDetectorPool -} - -// Counter implements syncfloat64.InstrumentProvider. -func (p *syncFloat64Provider) Counter(name string, opts ...instrument.Option) (syncfloat64.Counter, error) { - c, err := p.InstrumentProvider.Counter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &syncFloat64Counter{c, cardinalityDetector}, nil -} - -// Histogram implements syncfloat64.InstrumentProvider. -func (p *syncFloat64Provider) Histogram(name string, opts ...instrument.Option) (syncfloat64.Histogram, error) { - h, err := p.InstrumentProvider.Histogram(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &syncFloat64Histogram{h, cardinalityDetector}, nil -} - -// UpDownCounter implements syncfloat64.InstrumentProvider. -func (p *syncFloat64Provider) UpDownCounter(name string, opts ...instrument.Option) (syncfloat64.UpDownCounter, error) { - c, err := p.InstrumentProvider.UpDownCounter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &syncFloat64UpDownCounter{c, cardinalityDetector}, nil -} - -type syncInt64Provider struct { - syncint64.InstrumentProvider - cardinalityDetectorPool cardinalitydetector.CardinalityDetectorPool -} - -// Counter implements syncint64.InstrumentProvider. -func (p *syncInt64Provider) Counter(name string, opts ...instrument.Option) (syncint64.Counter, error) { - c, err := p.InstrumentProvider.Counter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &syncInt64Counter{c, cardinalityDetector}, nil -} - -// Histogram implements syncint64.InstrumentProvider. -func (p *syncInt64Provider) Histogram(name string, opts ...instrument.Option) (syncint64.Histogram, error) { - h, err := p.InstrumentProvider.Histogram(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &syncInt64Histogram{h, cardinalityDetector}, nil -} - -// UpDownCounter implements syncint64.InstrumentProvider. -func (p *syncInt64Provider) UpDownCounter(name string, opts ...instrument.Option) (syncint64.UpDownCounter, error) { - c, err := p.InstrumentProvider.UpDownCounter(name, opts...) - if err != nil { - return nil, err - } - - cardinalityDetector, ok := p.cardinalityDetectorPool.Lookup(name) - if !ok { - return nil, errLimitExceededCardinalityDetector - } - - return &syncInt64UpDownCounter{c, cardinalityDetector}, nil -} diff --git a/sdk/metric/meter.go b/sdk/metric/meter.go deleted file mode 100644 index 7eaecb5..0000000 --- a/sdk/metric/meter.go +++ /dev/null @@ -1,64 +0,0 @@ -package metric - -import ( - "github.com/tel-io/tel/v2/pkg/cardinalitydetector" - "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/metric/instrument/asyncfloat64" - "go.opentelemetry.io/otel/metric/instrument/asyncint64" - "go.opentelemetry.io/otel/metric/instrument/syncfloat64" - "go.opentelemetry.io/otel/metric/instrument/syncint64" -) - -var _ metric.Meter = (*meter)(nil) - -func newMeter( - delegate metric.Meter, - cardinalityDetectorPool cardinalitydetector.CardinalityDetectorPool, -) *meter { - return &meter{ - Meter: delegate, - cardinalityDetectorPool: cardinalityDetectorPool, - } -} - -type meter struct { - metric.Meter - cardinalityDetectorPool cardinalitydetector.CardinalityDetectorPool -} - -// AsyncFloat64 implements metric.Meter. -func (m *meter) AsyncFloat64() asyncfloat64.InstrumentProvider { - return &asyncFloat64Provider{ - InstrumentProvider: m.Meter.AsyncFloat64(), - cardinalityDetectorPool: m.cardinalityDetectorPool, - } -} - -// AsyncInt64 implements metric.Meter. -func (m *meter) AsyncInt64() asyncint64.InstrumentProvider { - return &asyncInt64Provider{ - InstrumentProvider: m.Meter.AsyncInt64(), - cardinalityDetectorPool: m.cardinalityDetectorPool, - } -} - -// SyncFloat64 implements metric.Meter. -func (m *meter) SyncFloat64() syncfloat64.InstrumentProvider { - return &syncFloat64Provider{ - InstrumentProvider: m.Meter.SyncFloat64(), - cardinalityDetectorPool: m.cardinalityDetectorPool, - } -} - -// SyncInt64 implements metric.Meter. -func (m *meter) SyncInt64() syncint64.InstrumentProvider { - return &syncInt64Provider{ - InstrumentProvider: m.Meter.SyncInt64(), - cardinalityDetectorPool: m.cardinalityDetectorPool, - } -} - -// Shutdown implements metric.Meter. -func (m *meter) Shutdown() { - m.cardinalityDetectorPool.Shutdown() -} diff --git a/sdk/metric/provider.go b/sdk/metric/provider.go deleted file mode 100644 index d7800df..0000000 --- a/sdk/metric/provider.go +++ /dev/null @@ -1,73 +0,0 @@ -package metric - -import ( - "context" - "sync" - - "github.com/tel-io/tel/v2/pkg/cardinalitydetector" - "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/sdk/instrumentation" - metricsdk "go.opentelemetry.io/otel/sdk/metric" - "go.opentelemetry.io/otel/sdk/metric/view" -) - -type LookupCardinalityDetector func(view.InstrumentKind, string) cardinalitydetector.CardinalityDetector - -var _ metric.MeterProvider = (*MeterProvider)(nil) - -func NewMeterProvider( - cardinalityDetectorConfig *cardinalitydetector.Config, - opts ...metricsdk.Option, -) *MeterProvider { - return &MeterProvider{ - MeterProvider: metricsdk.NewMeterProvider(opts...), - cardinalityDetectorConfig: cardinalityDetectorConfig, - meters: make(map[instrumentation.Scope]*meter), - } -} - -type MeterProvider struct { - *metricsdk.MeterProvider - cardinalityDetectorConfig *cardinalitydetector.Config - meters map[instrumentation.Scope]*meter - - mu sync.Mutex -} - -// Meter implements metric.MeterProvider. -func (p *MeterProvider) Meter(name string, opts ...metric.MeterOption) metric.Meter { - c := metric.NewMeterConfig(opts...) - s := instrumentation.Scope{ - Name: name, - Version: c.InstrumentationVersion(), - SchemaURL: c.SchemaURL(), - } - - p.mu.Lock() - defer p.mu.Unlock() - - if meter, ok := p.meters[s]; ok { - return meter - } - - meter := newMeter( - p.MeterProvider.Meter(name, opts...), - cardinalitydetector.NewPool(name, p.cardinalityDetectorConfig), - ) - - p.meters[s] = meter - - return meter -} - -// Shutdown implements metric.MeterProvider. -func (p *MeterProvider) Shutdown(ctx context.Context) error { - p.mu.Lock() - defer p.mu.Unlock() - - for _, meter := range p.meters { - meter.Shutdown() - } - - return p.MeterProvider.Shutdown(ctx) -}