-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
otel: conceal unwrapping for global async instrument registration #5881
Open
jmacd
wants to merge
13
commits into
open-telemetry:main
Choose a base branch
from
jmacd:jmacd/unwrap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
76bbad8
internal/global/meter.go: Use the proper unwrap method
jmacd 6866675
unwrap callback instrument arguments for global async instruments
jmacd 62c6889
new test
jmacd a577bf4
let unwrap be private
jmacd 9e49a96
chlog
jmacd 9419fd1
lint
jmacd c4fffde
format
jmacd 620bb87
Merge branch 'main' into jmacd/unwrap
dashpole 2271c1a
Update CHANGELOG.md
dashpole 8927666
Update CHANGELOG.md
dashpole 8b24afb
Merge branch 'main' into jmacd/unwrap
dashpole abed306
Merge branch 'main' into jmacd/unwrap
dashpole 2a906fb
Merge branch 'main' into jmacd/unwrap
pellared File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package global // import "go.opentelemetry.io/otel/internal/global" | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/otel/metric" | ||
"go.opentelemetry.io/otel/metric/embedded" | ||
) | ||
|
||
type altMeterProvider struct { | ||
t *testing.T | ||
meters []*altMeter | ||
embedded.MeterProvider | ||
} | ||
|
||
var _ metric.MeterProvider = &altMeterProvider{} | ||
|
||
func (amp *altMeterProvider) Meter(name string, opts ...metric.MeterOption) metric.Meter { | ||
am := &altMeter{ | ||
provider: amp, | ||
} | ||
amp.meters = append(amp.meters, am) | ||
return am | ||
} | ||
|
||
type altMeter struct { | ||
provider *altMeterProvider | ||
cbs []metric.Callback | ||
embedded.Meter | ||
} | ||
|
||
var _ metric.Meter = &altMeter{} | ||
|
||
type testAsyncCounter struct { | ||
meter *altMeter | ||
embedded.Int64ObservableCounter | ||
metric.Int64Observable | ||
} | ||
|
||
var _ metric.Int64ObservableCounter = &testAsyncCounter{} | ||
|
||
type altRegistration struct { | ||
cb metric.Callback | ||
embedded.Registration | ||
} | ||
|
||
type altObserver struct { | ||
t *testing.T | ||
embedded.Observer | ||
} | ||
|
||
func (*altRegistration) Unregister() error { | ||
return nil | ||
} | ||
|
||
func (am *altMeter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Int64ObservableCounter(name string, options ...metric.Int64ObservableCounterOption) (metric.Int64ObservableCounter, error) { | ||
return &testAsyncCounter{ | ||
meter: am, | ||
}, nil | ||
} | ||
|
||
func (am *altMeter) Int64ObservableUpDownCounter(name string, options ...metric.Int64ObservableUpDownCounterOption) (metric.Int64ObservableUpDownCounter, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Int64ObservableGauge(name string, options ...metric.Int64ObservableGaugeOption) (metric.Int64ObservableGauge, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Float64UpDownCounter(name string, options ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Float64ObservableCounter(name string, options ...metric.Float64ObservableCounterOption) (metric.Float64ObservableCounter, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Float64ObservableUpDownCounter(name string, options ...metric.Float64ObservableUpDownCounterOption) (metric.Float64ObservableUpDownCounter, error) { | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) Float64ObservableGauge(name string, options ...metric.Float64ObservableGaugeOption) (metric.Float64ObservableGauge, error) { | ||
// Note: The global delegation also breaks when we return nil in one of these! | ||
return nil, nil | ||
} | ||
|
||
func (am *altMeter) RegisterCallback(f metric.Callback, instruments ...metric.Observable) (metric.Registration, error) { | ||
for _, inst := range instruments { | ||
switch inst.(type) { | ||
case *testAsyncCounter: | ||
// OK! | ||
default: | ||
am.provider.t.Errorf("unexpected type %T", inst) | ||
} | ||
} | ||
am.cbs = append(am.cbs, f) | ||
return &altRegistration{cb: f}, nil | ||
} | ||
|
||
func (ao *altObserver) ObserveFloat64(inst metric.Float64Observable, _ float64, _ ...metric.ObserveOption) { | ||
ao.observe(inst) | ||
} | ||
|
||
func (ao *altObserver) ObserveInt64(inst metric.Int64Observable, _ int64, _ ...metric.ObserveOption) { | ||
ao.observe(inst) | ||
} | ||
|
||
func (ao *altObserver) observe(inst any) { | ||
switch inst.(type) { | ||
case *testAsyncCounter: | ||
// OK! | ||
default: | ||
ao.t.Errorf("unexpected type %T", inst) | ||
} | ||
} | ||
|
||
func TestMeterDelegation(t *testing.T) { | ||
ResetForTest(t) | ||
|
||
amp := &altMeterProvider{t: t} | ||
|
||
gm := MeterProvider().Meter("test") | ||
ai, err := gm.Int64ObservableCounter("test_counter") | ||
require.NoError(t, err) | ||
|
||
_, err = gm.RegisterCallback(func(_ context.Context, obs metric.Observer) error { | ||
obs.ObserveInt64(ai, 10) | ||
return nil | ||
}, ai) | ||
require.NoError(t, err) | ||
|
||
SetMeterProvider(amp) | ||
|
||
ctx := context.Background() | ||
ao := &altObserver{t: t} | ||
for _, meter := range amp.meters { | ||
for _, cb := range meter.cbs { | ||
require.NoError(t, cb(ctx, ao)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will panic if this is an invalid type conversion, whereas before it would be logged as an error.
Can we log this as an error? Or, better yet, include a specific
unwrappableFloat64Observable
type?and similar for
Int64Observable
?