Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PoC] synchronous gauge #5280

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions internal/global/instruments.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,32 @@ func (i *sfUpDownCounter) Add(ctx context.Context, incr float64, opts ...metric.
}
}

type sfGauge struct {
embedded.Float64Gauge

name string
opts []metric.Float64GaugeOption

delegate atomic.Value // metric.Float64Gauge
}

var _ metric.Float64Gauge = (*sfGauge)(nil)

func (i *sfGauge) setDelegate(m metric.Meter) {
ctr, err := m.Float64Gauge(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}

func (i *sfGauge) Record(ctx context.Context, value float64, opts ...metric.RecordOption) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(metric.Float64Gauge).Record(ctx, value, opts...)
}
}

type sfHistogram struct {
embedded.Float64Histogram

Expand Down Expand Up @@ -307,6 +333,32 @@ func (i *siCounter) Add(ctx context.Context, x int64, opts ...metric.AddOption)
}
}

type siGauge struct {
embedded.Int64Gauge

name string
opts []metric.Int64GaugeOption

delegate atomic.Value // metric.Int64Gauge
}

var _ metric.Int64Gauge = (*siGauge)(nil)

func (i *siGauge) setDelegate(m metric.Meter) {
ctr, err := m.Int64Gauge(i.name, i.opts...)
if err != nil {
GetErrorHandler().Handle(err)
return
}
i.delegate.Store(ctr)
}

func (i *siGauge) Record(ctx context.Context, value int64, opts ...metric.RecordOption) {
if ctr := i.delegate.Load(); ctr != nil {
ctr.(metric.Int64Gauge).Record(ctx, value, opts...)
}
}

type siUpDownCounter struct {
embedded.Int64UpDownCounter

Expand Down
2 changes: 2 additions & 0 deletions internal/global/instruments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type testCountingFloatInstrument struct {
metric.Float64Observable
embedded.Float64Counter
embedded.Float64UpDownCounter
embedded.Float64Gauge
embedded.Float64Histogram
embedded.Float64ObservableCounter
embedded.Float64ObservableUpDownCounter
Expand All @@ -172,6 +173,7 @@ type testCountingIntInstrument struct {
metric.Int64Observable
embedded.Int64Counter
embedded.Int64UpDownCounter
embedded.Int64Gauge
embedded.Int64Histogram
embedded.Int64ObservableCounter
embedded.Int64ObservableUpDownCounter
Expand Down
22 changes: 22 additions & 0 deletions internal/global/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ func (m *meter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCou
return i, nil
}

func (m *meter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
if del, ok := m.delegate.Load().(metric.Meter); ok {
return del.Int64Gauge(name, options...)
}
m.mtx.Lock()
defer m.mtx.Unlock()
i := &siGauge{name: name, opts: options}
m.instruments = append(m.instruments, i)
return i, nil
}

func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
if del, ok := m.delegate.Load().(metric.Meter); ok {
return del.Int64Histogram(name, options...)
Expand Down Expand Up @@ -219,6 +230,17 @@ func (m *meter) Float64UpDownCounter(name string, options ...metric.Float64UpDow
return i, nil
}

func (m *meter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) {
if del, ok := m.delegate.Load().(metric.Meter); ok {
return del.Float64Gauge(name, options...)
}
m.mtx.Lock()
defer m.mtx.Unlock()
i := &sfGauge{name: name, opts: options}
m.instruments = append(m.instruments, i)
return i, nil
}

func (m *meter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
if del, ok := m.delegate.Load().(metric.Meter); ok {
return del.Float64Histogram(name, options...)
Expand Down
6 changes: 6 additions & 0 deletions internal/global/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ func TestMeterConcurrentSafe(t *testing.T) {
_, _ = mtr.Int64ObservableGauge(name)
_, _ = mtr.Float64Counter(name)
_, _ = mtr.Float64UpDownCounter(name)
_, _ = mtr.Float64Gauge(name)
_, _ = mtr.Float64Histogram(name)
_, _ = mtr.Int64Counter(name)
_, _ = mtr.Int64UpDownCounter(name)
_, _ = mtr.Int64Gauge(name)
_, _ = mtr.Int64Histogram(name)
_, _ = mtr.RegisterCallback(zeroCallback)
if !once {
Expand Down Expand Up @@ -140,13 +142,17 @@ func testSetupAllInstrumentTypes(t *testing.T, m metric.Meter) (metric.Float64Co
require.NoError(t, err)
_, err = m.Float64UpDownCounter("test_Async_UpDownCounter")
assert.NoError(t, err)
_, err = m.Float64Gauge("test_Async_Gauge")
assert.NoError(t, err)
_, err = m.Float64Histogram("test_Async_Histogram")
assert.NoError(t, err)

_, err = m.Int64Counter("test_Async_Counter")
assert.NoError(t, err)
_, err = m.Int64UpDownCounter("test_Async_UpDownCounter")
assert.NoError(t, err)
_, err = m.Int64Gauge("test_Async_Gauge")
assert.NoError(t, err)
_, err = m.Int64Histogram("test_Async_Histogram")
assert.NoError(t, err)

Expand Down
12 changes: 12 additions & 0 deletions internal/global/meter_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ type testMeter struct {

sfCount int
sfUDCount int
sfGauge int
sfHist int

siCount int
siUDCount int
siGauge int
siHist int

callbacks []metric.Callback
Expand All @@ -54,6 +56,11 @@ func (m *testMeter) Int64UpDownCounter(name string, options ...metric.Int64UpDow
return &testCountingIntInstrument{}, nil
}

func (m *testMeter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
m.siGauge++
return &testCountingIntInstrument{}, nil
}

func (m *testMeter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
m.siHist++
return &testCountingIntInstrument{}, nil
Expand Down Expand Up @@ -84,6 +91,11 @@ func (m *testMeter) Float64UpDownCounter(name string, options ...metric.Float64U
return &testCountingFloatInstrument{}, nil
}

func (m *testMeter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) {
m.sfGauge++
return &testCountingFloatInstrument{}, nil
}

func (m *testMeter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
m.sfHist++
return &testCountingFloatInstrument{}, nil
Expand Down
4 changes: 4 additions & 0 deletions metric/embedded/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ type Float64Counter interface{ float64Counter() }
// the API package).
type Float64Histogram interface{ float64Histogram() }

type Float64Gauge interface{ float64Gauge() }

// Float64ObservableCounter is embedded in
// [go.opentelemetry.io/otel/metric.Float64ObservableCounter].
//
Expand Down Expand Up @@ -174,6 +176,8 @@ type Int64Counter interface{ int64Counter() }
// the API package).
type Int64Histogram interface{ int64Histogram() }

type Int64Gauge interface{ int64Gauge() }

// Int64ObservableCounter is embedded in
// [go.opentelemetry.io/otel/metric.Int64ObservableCounter].
//
Expand Down
2 changes: 2 additions & 0 deletions metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Meter interface {
// synchronously record the distribution of int64 measurements during a
// computational operation.
Int64Histogram(name string, options ...Int64HistogramOption) (Int64Histogram, error)
Int64Gauge(name string, options ...Int64GaugeOption) (Int64Gauge, error)
// Int64ObservableCounter returns a new Int64ObservableCounter identified
// by name and configured with options. The instrument is used to
// asynchronously record increasing int64 measurements once per a
Expand Down Expand Up @@ -104,6 +105,7 @@ type Meter interface {
// synchronously record the distribution of float64 measurements during a
// computational operation.
Float64Histogram(name string, options ...Float64HistogramOption) (Float64Histogram, error)
Float64Gauge(name string, options ...Float64GaugeOption) (Float64Gauge, error)
// Float64ObservableCounter returns a new Float64ObservableCounter
// instrument identified by name and configured with options. The
// instrument is used to asynchronously record increasing float64
Expand Down
16 changes: 16 additions & 0 deletions metric/noop/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (Meter) Int64UpDownCounter(string, ...metric.Int64UpDownCounterOption) (met
return Int64UpDownCounter{}, nil
}

func (Meter) Int64Gauge(string, ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
return Int64Gauge{}, nil
}

// Int64Histogram returns a Histogram used to record int64 measurements that
// produces no telemetry.
func (Meter) Int64Histogram(string, ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
Expand Down Expand Up @@ -106,6 +110,10 @@ func (Meter) Float64UpDownCounter(string, ...metric.Float64UpDownCounterOption)
return Float64UpDownCounter{}, nil
}

func (Meter) Float64Gauge(string, ...metric.Float64GaugeOption) (metric.Float64Gauge, error) {
return Float64Gauge{}, nil
}

// Float64Histogram returns a Histogram used to record int64 measurements that
// produces no telemetry.
func (Meter) Float64Histogram(string, ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
Expand Down Expand Up @@ -180,6 +188,14 @@ func (Int64UpDownCounter) Add(context.Context, int64, ...metric.AddOption) {}
// float64 measurements. It produces no telemetry.
type Float64UpDownCounter struct{ embedded.Float64UpDownCounter }

type Int64Gauge struct{ embedded.Int64Gauge }

func (Int64Gauge) Record(context.Context, int64, ...metric.RecordOption) {}

type Float64Gauge struct{ embedded.Float64Gauge }

func (Float64Gauge) Record(context.Context, float64, ...metric.RecordOption) {}

// Add performs no operation.
func (Float64UpDownCounter) Add(context.Context, float64, ...metric.AddOption) {}

Expand Down
33 changes: 33 additions & 0 deletions metric/syncfloat64.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ type Float64UpDownCounterOption interface {
applyFloat64UpDownCounter(Float64UpDownCounterConfig) Float64UpDownCounterConfig
}

type Float64GaugeOption interface {
applyFloat64Gauge(Float64GaugeConfig) Float64GaugeConfig
}

type Float64Gauge interface {
embedded.Float64Gauge

Record(ctx context.Context, value float64, options ...RecordOption)
}

type Float64GaugeConfig struct {
description string
unit string
}

func NewFloat64GaugeConfig(opts ...Float64GaugeOption) Float64GaugeConfig {
var config Float64GaugeConfig
for _, o := range opts {
config = o.applyFloat64Gauge(config)
}
return config
}

// Description returns the configured description.
func (c Float64GaugeConfig) Description() string {
return c.description
}

// Unit returns the configured unit.
func (c Float64GaugeConfig) Unit() string {
return c.unit
}

// Float64Histogram is an instrument that records a distribution of float64
// values.
//
Expand Down
33 changes: 33 additions & 0 deletions metric/syncint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ type Int64UpDownCounterOption interface {
applyInt64UpDownCounter(Int64UpDownCounterConfig) Int64UpDownCounterConfig
}

type Int64GaugeOption interface {
applyInt64Gauge(Int64GaugeConfig) Int64GaugeConfig
}

type Int64Gauge interface {
embedded.Int64Gauge

Record(ctx context.Context, value int64, options ...RecordOption)
}

type Int64GaugeConfig struct {
description string
unit string
}

func NewInt64GaugeConfig(opts ...Int64GaugeOption) Int64GaugeConfig {
var config Int64GaugeConfig
for _, o := range opts {
config = o.applyInt64Gauge(config)
}
return config
}

// Description returns the configured description.
func (c Int64GaugeConfig) Description() string {
return c.description
}

// Unit returns the configured unit.
func (c Int64GaugeConfig) Unit() string {
return c.unit
}

// Int64Histogram is an instrument that records a distribution of int64
// values.
//
Expand Down
6 changes: 6 additions & 0 deletions sdk/metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const (
// InstrumentKindObservableGauge identifies a group of instruments that
// record current values in an asynchronous callback.
InstrumentKindObservableGauge

InstrumentKindGauge
)

type nonComparable [0]func() // nolint: unused // This is indeed used.
Expand Down Expand Up @@ -174,12 +176,14 @@ type int64Inst struct {

embedded.Int64Counter
embedded.Int64UpDownCounter
embedded.Int64Gauge
embedded.Int64Histogram
}

var (
_ metric.Int64Counter = (*int64Inst)(nil)
_ metric.Int64UpDownCounter = (*int64Inst)(nil)
_ metric.Int64Gauge = (*int64Inst)(nil)
_ metric.Int64Histogram = (*int64Inst)(nil)
)

Expand All @@ -204,12 +208,14 @@ type float64Inst struct {

embedded.Float64Counter
embedded.Float64UpDownCounter
embedded.Float64Gauge
embedded.Float64Histogram
}

var (
_ metric.Float64Counter = (*float64Inst)(nil)
_ metric.Float64UpDownCounter = (*float64Inst)(nil)
_ metric.Float64Gauge = (*float64Inst)(nil)
_ metric.Float64Histogram = (*float64Inst)(nil)
)

Expand Down
5 changes: 3 additions & 2 deletions sdk/metric/instrumentkind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading