Skip to content

Commit

Permalink
Use scope in addSync idempotent check
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Feb 14, 2023
1 parent 89e2848 commit dee4c2a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
17 changes: 12 additions & 5 deletions sdk/metric/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newPipeline(res *resource.Resource, reader Reader, views []View) *pipeline
resource: res,
reader: reader,
views: views,
seen: make(map[instrumentID]struct{}),
seen: make(map[streamKey]struct{}),
aggregations: make(map[instrumentation.Scope][]instrumentSync),
}
}
Expand All @@ -77,22 +77,29 @@ type pipeline struct {
views []View

sync.Mutex
seen map[instrumentID]struct{}
seen map[streamKey]struct{}
aggregations map[instrumentation.Scope][]instrumentSync
callbacks []func(context.Context) error
multiCallbacks list.List
}

// streamKey is used to ensure the uniqueness of aggregations for an
// instrumentID being added to a pipeline within a scope.
type streamKey struct {
scope instrumentation.Scope
inst instrumentID
}

// addSync adds iSync to p with scope. The id is used to ensure this method is
// idempotent. Multiple calls for the same id will result in the latter calls
// being dropped.
func (p *pipeline) addSync(id instrumentID, scope instrumentation.Scope, iSync instrumentSync) {
p.Lock()
defer p.Unlock()
if p.seen == nil {
p.seen = make(map[instrumentID]struct{})
p.seen = make(map[streamKey]struct{})
} else {
if _, ok := p.seen[id]; ok {
if _, ok := p.seen[streamKey{scope, id}]; ok {
return
}
}
Expand All @@ -101,7 +108,7 @@ func (p *pipeline) addSync(id instrumentID, scope instrumentation.Scope, iSync i
scope: make([]instrumentSync, 0, 1),
}
}
p.seen[id] = struct{}{}
p.seen[streamKey{scope, id}] = struct{}{}
p.aggregations[scope] = append(p.aggregations[scope], iSync)
}

Expand Down
13 changes: 9 additions & 4 deletions sdk/metric/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,22 @@ func TestPipelineAddSyncIdempotentcy(t *testing.T) {

p := newPipeline(resource.Empty(), NewManualReader(), nil)
p.addSync(id0, scope, i0)

assert.Contains(t, p.seen, id0, "id not tracked")

k := streamKey{scope: scope, inst: id0}
assert.Contains(t, p.seen, k, "id not tracked")
require.Len(t, p.aggregations[scope], 1, "instrumentSync in pipeline")
require.Equal(t, name0, p.aggregations[scope][0].name, "wrong instrumentSync added")

i1 := instrumentSync{name: "i1"}
p.addSync(id0, scope, i1)

require.Len(t, p.aggregations[scope], 1, "additional instrumentSync in pipeline")
assert.Equal(t, name0, p.aggregations[scope][0].name, "instrumentSync modified")

altScope := instrumentation.Scope{}
p.addSync(id0, altScope, i0)
require.Len(t, p.aggregations[scope], 1, "alt scope added to orig")
assert.Equal(t, name0, p.aggregations[scope][0].name, "instrumentSync modified by scope")
require.Len(t, p.aggregations[altScope], 1, "alt scope not added")
assert.Equal(t, name0, p.aggregations[altScope][0].name, "instrumentSync not added to alt scope")
}

func TestDefaultViewImplicit(t *testing.T) {
Expand Down

0 comments on commit dee4c2a

Please sign in to comment.