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

Change Instrument Library to Instrument Scope #3016

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 11 additions & 11 deletions sdk/metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

// meterRegistry keeps a record of initialized meters for instrumentation
// libraries. A meter is unique to an instrumentation library and if multiple
// scopes. A meter is unique to an instrumentation scope and if multiple
// requests for that meter are made a meterRegistry ensure the same instance
// is used.
//
Expand All @@ -43,31 +43,31 @@ import (
type meterRegistry struct {
sync.Mutex

meters map[instrumentation.Library]*meter
meters map[instrumentation.Scope]*meter
}

// Get returns a registered meter matching the instrumentation library if it
// Get returns a registered meter matching the instrumentation scope if it
// exists in the meterRegistry. Otherwise, a new meter configured for the
// instrumentation library is registered and then returned.
// instrumentation scope is registered and then returned.
//
// Get is safe to call concurrently.
func (r *meterRegistry) Get(l instrumentation.Library) *meter {
func (r *meterRegistry) Get(s instrumentation.Scope) *meter {
r.Lock()
defer r.Unlock()

if r.meters == nil {
m := &meter{Library: l}
r.meters = map[instrumentation.Library]*meter{l: m}
m := &meter{Scope: s}
r.meters = map[instrumentation.Scope]*meter{s: m}
return m
}

m, ok := r.meters[l]
m, ok := r.meters[s]
if ok {
return m
}

m = &meter{Library: l}
r.meters[l] = m
m = &meter{Scope: s}
r.meters[s] = m
return m
}

Expand All @@ -91,7 +91,7 @@ func (r *meterRegistry) Range(f func(*meter) bool) {
// produced by an instrumentation scope will use metric instruments from a
// single meter.
type meter struct {
instrumentation.Library
instrumentation.Scope

// TODO (#2815, 2814): implement.
}
Expand Down
16 changes: 8 additions & 8 deletions sdk/metric/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ import (
)

func TestMeterRegistry(t *testing.T) {
il0 := instrumentation.Library{Name: "zero"}
il1 := instrumentation.Library{Name: "one"}
is0 := instrumentation.Scope{Name: "zero"}
is1 := instrumentation.Scope{Name: "one"}

r := meterRegistry{}
var m0 *meter
t.Run("ZeroValueGetDoesNotPanic", func(t *testing.T) {
assert.NotPanics(t, func() { m0 = r.Get(il0) })
assert.Equal(t, il0, m0.Library, "uninitialized meter returned")
assert.NotPanics(t, func() { m0 = r.Get(is0) })
assert.Equal(t, is0, m0.Scope, "uninitialized meter returned")
})

m01 := r.Get(il0)
m01 := r.Get(is0)
t.Run("GetSameMeter", func(t *testing.T) {
assert.Samef(t, m0, m01, "returned different meters: %v", il0)
assert.Samef(t, m0, m01, "returned different meters: %v", is0)
})

m1 := r.Get(il1)
m1 := r.Get(is1)
t.Run("GetDifferentMeter", func(t *testing.T) {
assert.NotSamef(t, m0, m1, "returned same meters: %v", il1)
assert.NotSamef(t, m0, m1, "returned same meters: %v", is1)
})

t.Run("RangeComplete", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func NewMeterProvider(options ...Option) *MeterProvider {
// This method is safe to call concurrently.
func (mp *MeterProvider) Meter(name string, options ...metric.MeterOption) metric.Meter {
c := metric.NewMeterConfig(options...)
return mp.meters.Get(instrumentation.Library{
return mp.meters.Get(instrumentation.Scope{
Name: name,
Version: c.InstrumentationVersion(),
SchemaURL: c.SchemaURL(),
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/view/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// Instrument uniquely identifies an instrument within a meter.
type Instrument struct {
Scope instrumentation.Library
Scope instrumentation.Scope

Name string
Description string
Expand Down
24 changes: 12 additions & 12 deletions sdk/metric/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
type View struct {
instrumentName *regexp.Regexp
hasWildcard bool
scope instrumentation.Library
scope instrumentation.Scope

filter attribute.Filter
name string
Expand All @@ -57,9 +57,9 @@ func New(opts ...Option) (View, error) {
v = opt.apply(v)
}

emptyLibrary := instrumentation.Library{}
emptyScope := instrumentation.Scope{}
if v.instrumentName == nil &&
v.scope == emptyLibrary {
v.scope == emptyScope {
return View{}, fmt.Errorf("must provide at least 1 match option")
}

Expand Down Expand Up @@ -104,23 +104,23 @@ func (v View) matchName(name string) bool {
return v.instrumentName == nil || v.instrumentName.MatchString(name)
}

func (v View) matchLibraryName(name string) bool {
func (v View) matchScopeName(name string) bool {
return v.scope.Name == "" || name == v.scope.Name
}

func (v View) matchLibraryVersion(version string) bool {
func (v View) matchScopeVersion(version string) bool {
return v.scope.Version == "" || version == v.scope.Version
}

func (v View) matchLibrarySchemaURL(schemaURL string) bool {
func (v View) matchScopeSchemaURL(schemaURL string) bool {
return v.scope.SchemaURL == "" || schemaURL == v.scope.SchemaURL
}

func (v View) match(i Instrument) bool {
return v.matchName(i.Name) &&
v.matchLibraryName(i.Scope.Name) &&
v.matchLibrarySchemaURL(i.Scope.SchemaURL) &&
v.matchLibraryVersion(i.Scope.Version)
v.matchScopeName(i.Scope.Name) &&
v.matchScopeSchemaURL(i.Scope.SchemaURL) &&
v.matchScopeVersion(i.Scope.Version)
}

// Option applies a Configuration option value to a View. All options
Expand Down Expand Up @@ -155,12 +155,12 @@ func MatchInstrumentName(name string) Option {
// TODO (#2813): Implement MatchInstrumentKind when InstrumentKind is defined.
// TODO (#2813): Implement MatchNumberKind when NumberKind is defined.

// MatchInstrumentationLibrary will do an exact match on any
// MatchInstrumentationScope will do an exact match on any
// instrumentation.Scope field that is non-empty (""). The default is to match all
// instrumentation scopes.
func MatchInstrumentationLibrary(lib instrumentation.Library) Option {
func MatchInstrumentationScope(scope instrumentation.Scope) Option {
return optionFunc(func(v View) View {
v.scope = lib
v.scope = scope
return v
})
}
Expand Down
22 changes: 11 additions & 11 deletions sdk/metric/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

var matchInstrument = Instrument{
Scope: instrumentation.Library{
Scope: instrumentation.Scope{
Name: "bar",
Version: "v1.0.0",
SchemaURL: "stuff.test/",
Expand All @@ -38,7 +38,7 @@ var matchInstrument = Instrument{
}

var noMatchInstrument = Instrument{
Scope: instrumentation.Library{
Scope: instrumentation.Scope{
Name: "notfoo",
Version: "v0.x.0",
SchemaURL: "notstuff.test/",
Expand All @@ -65,19 +65,19 @@ func TestViewTransformInstrument(t *testing.T) {
notMatch: emptyDescription,
},
{
name: "Library name",
name: "Scope name",
options: []Option{
MatchInstrumentationLibrary(instrumentation.Library{
MatchInstrumentationScope(instrumentation.Scope{
Name: "bar",
}),
},
match: matchInstrument,
notMatch: emptyDescription,
},
{
name: "Library version",
name: "Scope version",
options: []Option{
MatchInstrumentationLibrary(instrumentation.Library{
MatchInstrumentationScope(instrumentation.Scope{
Version: "v1.0.0",
}),
},
Expand All @@ -86,9 +86,9 @@ func TestViewTransformInstrument(t *testing.T) {
notMatch: emptyDescription,
},
{
name: "Library SchemaURL",
name: "Scope SchemaURL",
options: []Option{
MatchInstrumentationLibrary(instrumentation.Library{
MatchInstrumentationScope(instrumentation.Scope{
SchemaURL: "stuff.test/",
}),
},
Expand All @@ -107,7 +107,7 @@ func TestViewTransformInstrument(t *testing.T) {
name: "composite literal name",
options: []Option{
MatchInstrumentName("foo"),
MatchInstrumentationLibrary(instrumentation.Library{
MatchInstrumentationScope(instrumentation.Scope{
Name: "bar",
Version: "v1.0.0",
SchemaURL: "stuff.test/",
Expand All @@ -123,7 +123,7 @@ func TestViewTransformInstrument(t *testing.T) {
WithRename("baz"),
},
match: Instrument{
Scope: instrumentation.Library{
Scope: instrumentation.Scope{
Name: "bar",
Version: "v1.0.0",
SchemaURL: "stuff.test/",
Expand All @@ -140,7 +140,7 @@ func TestViewTransformInstrument(t *testing.T) {
WithSetDescription("descriptive stuff"),
},
match: Instrument{
Scope: instrumentation.Library{
Scope: instrumentation.Scope{
Name: "bar",
Version: "v1.0.0",
SchemaURL: "stuff.test/",
Expand Down