1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-11-30 08:46:54 +02:00

Remove the distributedctx.Mutator (#252)

* Remove the distributedctx.Mutator

* Remove unused field

* Update examples
This commit is contained in:
Joshua MacDonald 2019-10-30 13:21:13 -07:00 committed by GitHub
parent 25e97e56a4
commit de6715fca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 28 additions and 244 deletions

View File

@ -17,6 +17,8 @@ package distributedcontext
import (
"context"
"runtime/pprof"
"go.opentelemetry.io/api/core"
)
type ctxEntriesType struct{}
@ -29,9 +31,9 @@ func WithMap(ctx context.Context, m Map) context.Context {
return context.WithValue(ctx, ctxEntriesKey, m)
}
func NewContext(ctx context.Context, mutators ...Mutator) context.Context {
func NewContext(ctx context.Context, keyvalues ...core.KeyValue) context.Context {
return WithMap(ctx, FromContext(ctx).Apply(MapUpdate{
MultiMutator: mutators,
MultiKV: keyvalues,
}))
}

View File

@ -18,13 +18,8 @@ import (
"go.opentelemetry.io/api/core"
)
type MeasureMetadata struct {
TTL int // -1 == infinite, 0 == do not propagate
}
type entry struct {
value core.Value
meta MeasureMetadata
}
type rawMap map[core.Key]entry
@ -34,10 +29,8 @@ type Map struct {
}
type MapUpdate struct {
SingleKV core.KeyValue
MultiKV []core.KeyValue
SingleMutator Mutator
MultiMutator []Mutator
SingleKV core.KeyValue
MultiKV []core.KeyValue
}
func newMap(raw rawMap) Map {
@ -55,7 +48,7 @@ func NewMap(update MapUpdate) Map {
}
func (m Map) Apply(update MapUpdate) Map {
r := make(rawMap, len(m.m)+len(update.MultiKV)+len(update.MultiMutator))
r := make(rawMap, len(m.m)+len(update.MultiKV))
for k, v := range m.m {
r[k] = v
}
@ -69,12 +62,6 @@ func (m Map) Apply(update MapUpdate) Map {
value: kv.Value,
}
}
if update.SingleMutator.Key.Defined() {
r.apply(update.SingleMutator)
}
for _, mutator := range update.MultiMutator {
r.apply(mutator)
}
if len(r) == 0 {
r = nil
}
@ -108,25 +95,3 @@ func (m Map) Foreach(f func(kv core.KeyValue) bool) {
}
}
}
func (r rawMap) apply(mutator Mutator) {
key := mutator.KeyValue.Key
content := entry{
value: mutator.KeyValue.Value,
meta: mutator.MeasureMetadata,
}
switch mutator.MutatorOp {
case INSERT:
if _, ok := r[key]; !ok {
r[key] = content
}
case UPDATE:
if _, ok := r[key]; ok {
r[key] = content
}
case UPSERT:
r[key] = content
case DELETE:
delete(r, key)
}
}

View File

@ -1,69 +0,0 @@
// Copyright 2019, OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package distributedcontext
import (
"go.opentelemetry.io/api/core"
)
type MutatorOp int
const (
INSERT MutatorOp = iota
UPDATE
UPSERT
DELETE
)
type Mutator struct {
MutatorOp
core.KeyValue
MeasureMetadata
}
func (m Mutator) WithTTL(hops int) Mutator {
m.TTL = hops
return m
}
func Insert(kv core.KeyValue) Mutator {
return Mutator{
MutatorOp: INSERT,
KeyValue: kv,
}
}
func Update(kv core.KeyValue) Mutator {
return Mutator{
MutatorOp: UPDATE,
KeyValue: kv,
}
}
func Upsert(kv core.KeyValue) Mutator {
return Mutator{
MutatorOp: UPSERT,
KeyValue: kv,
}
}
func Delete(k core.Key) Mutator {
return Mutator{
MutatorOp: DELETE,
KeyValue: core.KeyValue{
Key: k,
},
}
}

View File

@ -24,7 +24,6 @@ import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/distributedcontext"
"go.opentelemetry.io/api/trace"
"go.opentelemetry.io/internal/matchers"
)
@ -323,25 +322,6 @@ func (h *Harness) testSpan(tracerFactory func() trace.Tracer) {
"#SetAttributes": func(span trace.Span) {
span.SetAttributes(core.Key("key1").String("value"), core.Key("key2").Int(123))
},
"#ModifyAttribute": func(span trace.Span) {
span.SetAttribute(core.Key("key").String("original value"))
span.ModifyAttribute(distributedcontext.Mutator{
MutatorOp: distributedcontext.UPSERT,
KeyValue: core.Key("key").String("new value"),
})
},
"#ModifyAttributes": func(span trace.Span) {
span.SetAttribute(core.Key("key1").String("original value"))
span.ModifyAttributes(distributedcontext.Mutator{
MutatorOp: distributedcontext.UPSERT,
KeyValue: core.Key("key1").String("new value"),
}, distributedcontext.Mutator{
MutatorOp: distributedcontext.INSERT,
KeyValue: core.Key("key2").Int(123),
})
},
}
var mechanisms = map[string]func() trace.Span{
"Span created via Tracer#Start": func() trace.Span {

View File

@ -21,7 +21,6 @@ import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/distributedcontext"
)
type Provider interface {
@ -94,10 +93,6 @@ type Span interface {
// Set span attributes
SetAttribute(core.KeyValue)
SetAttributes(...core.KeyValue)
// Modify and delete span attributes
ModifyAttribute(distributedcontext.Mutator)
ModifyAttributes(...distributedcontext.Mutator)
}
// SpanOption apply changes to SpanOptions.

View File

@ -8,7 +8,6 @@ import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/distributedcontext"
"go.opentelemetry.io/api/trace"
)
@ -88,14 +87,6 @@ func (mockSpan) SetAttribute(attribute core.KeyValue) {
func (mockSpan) SetAttributes(attributes ...core.KeyValue) {
}
// ModifyAttribute does nothing.
func (mockSpan) ModifyAttribute(mutator distributedcontext.Mutator) {
}
// ModifyAttributes does nothing.
func (mockSpan) ModifyAttributes(mutators ...distributedcontext.Mutator) {
}
// End does nothing.
func (mockSpan) End(options ...trace.EndOption) {
}

View File

@ -21,7 +21,6 @@ import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/distributedcontext"
)
type NoopSpan struct {
@ -55,14 +54,6 @@ func (NoopSpan) SetAttribute(attribute core.KeyValue) {
func (NoopSpan) SetAttributes(attributes ...core.KeyValue) {
}
// ModifyAttribute does nothing.
func (NoopSpan) ModifyAttribute(mutator distributedcontext.Mutator) {
}
// ModifyAttributes does nothing.
func (NoopSpan) ModifyAttributes(mutators ...distributedcontext.Mutator) {
}
// End does nothing.
func (NoopSpan) End(options ...EndOption) {
}

View File

@ -69,19 +69,6 @@ func NewMockTracer() *MockTracer {
}
}
func (t *MockTracer) WithResources(attributes ...otelcore.KeyValue) oteltrace.Tracer {
t.Resources = t.Resources.Apply(upsertMultiMapUpdate(attributes...))
return t
}
func (t *MockTracer) WithComponent(name string) oteltrace.Tracer {
return t.WithResources(otelkey.New("component").String(name))
}
func (t *MockTracer) WithService(name string) oteltrace.Tracer {
return t.WithResources(otelkey.New("service").String(name))
}
func (t *MockTracer) WithSpan(ctx context.Context, name string, body func(context.Context) error) error {
ctx, span := t.Start(ctx, name)
defer span.End()
@ -111,12 +98,14 @@ func (t *MockTracer) Start(ctx context.Context, name string, opts ...oteltrace.S
officialTracer: t,
spanContext: spanContext,
recording: spanOpts.Record,
Attributes: oteldctx.NewMap(upsertMultiMapUpdate(spanOpts.Attributes...)),
StartTime: startTime,
EndTime: time.Time{},
ParentSpanID: t.getParentSpanID(ctx, &spanOpts),
Events: nil,
SpanKind: spanKind,
Attributes: oteldctx.NewMap(oteldctx.MapUpdate{
MultiKV: spanOpts.Attributes,
}),
StartTime: startTime,
EndTime: time.Time{},
ParentSpanID: t.getParentSpanID(ctx, &spanOpts),
Events: nil,
SpanKind: spanKind,
}
if !migration.SkipContextSetup(ctx) {
ctx = oteltrace.SetCurrentSpan(ctx, span)
@ -252,22 +241,14 @@ func (s *MockSpan) SetError(v bool) {
}
func (s *MockSpan) SetAttribute(attribute otelcore.KeyValue) {
s.applyUpdate(upsertMapUpdate(attribute))
}
func (s *MockSpan) SetAttributes(attributes ...otelcore.KeyValue) {
s.applyUpdate(upsertMultiMapUpdate(attributes...))
}
func (s *MockSpan) ModifyAttribute(mutator oteldctx.Mutator) {
s.applyUpdate(oteldctx.MapUpdate{
SingleMutator: mutator,
SingleKV: attribute,
})
}
func (s *MockSpan) ModifyAttributes(mutators ...oteldctx.Mutator) {
func (s *MockSpan) SetAttributes(attributes ...otelcore.KeyValue) {
s.applyUpdate(oteldctx.MapUpdate{
MultiMutator: mutators,
MultiKV: attributes,
})
}
@ -306,7 +287,9 @@ func (s *MockSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Tim
CtxAttributes: oteldctx.FromContext(ctx),
Timestamp: timestamp,
Msg: msg,
Attributes: oteldctx.NewMap(upsertMultiMapUpdate(attrs...)),
Attributes: oteldctx.NewMap(oteldctx.MapUpdate{
MultiKV: attrs,
}),
})
}
@ -321,38 +304,3 @@ func (s *MockSpan) Link(sc otelcore.SpanContext, attrs ...otelcore.KeyValue) {
func (s *MockSpan) OverrideTracer(tracer oteltrace.Tracer) {
s.officialTracer = tracer
}
func upsertMapUpdate(kv otelcore.KeyValue) oteldctx.MapUpdate {
return singleMutatorMapUpdate(oteldctx.UPSERT, kv)
}
func upsertMultiMapUpdate(kvs ...otelcore.KeyValue) oteldctx.MapUpdate {
return multiMutatorMapUpdate(oteldctx.UPSERT, kvs...)
}
func singleMutatorMapUpdate(op oteldctx.MutatorOp, kv otelcore.KeyValue) oteldctx.MapUpdate {
return oteldctx.MapUpdate{
SingleMutator: keyValueToMutator(op, kv),
}
}
func multiMutatorMapUpdate(op oteldctx.MutatorOp, kvs ...otelcore.KeyValue) oteldctx.MapUpdate {
return oteldctx.MapUpdate{
MultiMutator: keyValuesToMutators(op, kvs...),
}
}
func keyValuesToMutators(op oteldctx.MutatorOp, kvs ...otelcore.KeyValue) []oteldctx.Mutator {
var mutators []oteldctx.Mutator
for _, kv := range kvs {
mutators = append(mutators, keyValueToMutator(op, kv))
}
return mutators
}
func keyValueToMutator(op oteldctx.MutatorOp, kv otelcore.KeyValue) oteldctx.Mutator {
return oteldctx.Mutator{
MutatorOp: op,
KeyValue: kv,
}
}

View File

@ -46,8 +46,8 @@ func main() {
ctx := context.Background()
ctx = distributedcontext.NewContext(ctx,
distributedcontext.Insert(fooKey.String("foo1")),
distributedcontext.Insert(barKey.String("bar1")),
fooKey.String("foo1"),
barKey.String("bar1"),
)
commonLabels := meter.Labels(lemonsKey.Int(10))
@ -68,8 +68,7 @@ func main() {
meter.RecordBatch(
// Note: call-site variables added as context Entries:
distributedcontext.NewContext(ctx,
distributedcontext.Insert(anotherKey.String("xyz"))),
distributedcontext.NewContext(ctx, anotherKey.String("xyz")),
commonLabels,
oneMetric.Measurement(1.0),

View File

@ -63,7 +63,7 @@ func main() {
client := http.DefaultClient
ctx := distributedcontext.NewContext(context.Background(),
distributedcontext.Insert(key.New("username").String("donuts")),
key.String("username", "donuts"),
)
var body []byte

View File

@ -58,7 +58,7 @@ func main() {
client := http.DefaultClient
ctx := distributedcontext.NewContext(context.Background(),
distributedcontext.Insert(key.New("username").String("donuts")),
key.String("username", "donuts"),
)
var body []byte

View File

@ -60,8 +60,8 @@ func main() {
ctx := context.Background()
ctx = distributedcontext.NewContext(ctx,
distributedcontext.Insert(fooKey.String("foo1")),
distributedcontext.Insert(barKey.String("bar1")),
fooKey.String("foo1"),
barKey.String("bar1"),
)
err := tracer.WithSpan(ctx, "operation", func(ctx context.Context) error {

View File

@ -21,7 +21,6 @@ import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/api/core"
"go.opentelemetry.io/api/distributedcontext"
apitrace "go.opentelemetry.io/api/trace"
)
@ -63,14 +62,6 @@ func (ms *MockSpan) SetAttribute(attribute core.KeyValue) {
func (ms *MockSpan) SetAttributes(attributes ...core.KeyValue) {
}
// ModifyAttribute does nothing.
func (ms *MockSpan) ModifyAttribute(mutator distributedcontext.Mutator) {
}
// ModifyAttributes does nothing.
func (ms *MockSpan) ModifyAttributes(mutators ...distributedcontext.Mutator) {
}
// End does nothing.
func (ms *MockSpan) End(options ...apitrace.EndOption) {
}

View File

@ -22,7 +22,6 @@ import (
"google.golang.org/grpc/codes"
"go.opentelemetry.io/api/core"
apidctx "go.opentelemetry.io/api/distributedcontext"
apitrace "go.opentelemetry.io/api/trace"
"go.opentelemetry.io/sdk/export"
"go.opentelemetry.io/sdk/internal"
@ -99,14 +98,6 @@ func (s *span) SetAttributes(attributes ...core.KeyValue) {
s.copyToCappedAttributes(attributes...)
}
// ModifyAttribute does nothing.
func (s *span) ModifyAttribute(mutator apidctx.Mutator) {
}
// ModifyAttributes does nothing.
func (s *span) ModifyAttributes(mutators ...apidctx.Mutator) {
}
func (s *span) End(options ...apitrace.EndOption) {
if s == nil {
return