From 3362421c9b41feb586ab003857894d470be57169 Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Thu, 19 Sep 2019 20:20:02 +0200 Subject: [PATCH] Drop the registry package (#130) This is to shrink the PR #100. The only place where the registry.Variable type was used was metrics, so just inline that type into its only user. The use of the registry.Variable type in core.Key was limited to the Name field. The stats package also used the registry.Variable type, but seems that also only the Name field was used and the package is going to be dropped anyway. --- api/core/key.go | 6 +- api/core/key_test.go | 7 +- api/key/key.go | 16 +---- api/metric/api.go | 17 ++--- api/metric/common.go | 10 +-- api/registry/doc.go | 15 ---- api/registry/registry.go | 69 ------------------- api/stats/stats.go | 28 +++----- api/tag/context.go | 2 +- example/basic/main.go | 7 +- .../exporter/reader/format/format.go | 8 +-- experimental/streaming/sdk/trace.go | 4 +- exporter/trace/jaeger/jaeger.go | 2 +- plugin/httptrace/clienttrace.go | 4 +- plugin/httptrace/httptrace.go | 2 +- sdk/trace/span.go | 2 +- 16 files changed, 38 insertions(+), 161 deletions(-) delete mode 100644 api/registry/doc.go delete mode 100644 api/registry/registry.go diff --git a/api/core/key.go b/api/core/key.go index 5a23d2da3..9b09b3cee 100644 --- a/api/core/key.go +++ b/api/core/key.go @@ -3,12 +3,10 @@ package core import ( "fmt" "unsafe" - - "go.opentelemetry.io/api/registry" ) type Key struct { - Variable registry.Variable + Name string } type KeyValue struct { @@ -148,7 +146,7 @@ func (k Key) Uint(v uint) KeyValue { } func (k Key) Defined() bool { - return k.Variable.Defined() + return len(k.Name) != 0 } // TODO make this a lazy one-time conversion. diff --git a/api/core/key_test.go b/api/core/key_test.go index 647e2d764..a0be400b3 100644 --- a/api/core/key_test.go +++ b/api/core/key_test.go @@ -7,7 +7,6 @@ import ( "github.com/google/go-cmp/cmp" "go.opentelemetry.io/api/core" - "go.opentelemetry.io/api/registry" ) func TestBool(t *testing.T) { @@ -306,15 +305,13 @@ func TestDefined(t *testing.T) { { name: "Key.Defined() returns true when len(v.Name) != 0", k: core.Key{ - registry.Variable{ - Name: "foo", - }, + Name: "foo", }, want: true, }, { name: "Key.Defined() returns false when len(v.Name) == 0", - k: core.Key{registry.Variable{}}, + k: core.Key{}, want: false, }, } { diff --git a/api/key/key.go b/api/key/key.go index 1c57e2db4..0dc4665b0 100644 --- a/api/key/key.go +++ b/api/key/key.go @@ -2,22 +2,10 @@ package key import ( "go.opentelemetry.io/api/core" - "go.opentelemetry.io/api/registry" ) -type AnyValue struct{} - -func (AnyValue) String() string { - return "AnyValue" -} - -func New(name string, opts ...registry.Option) core.Key { +func New(name string) core.Key { return core.Key{ - Variable: registry.Register(name, AnyValue{}, opts...), + Name: name, } } - -var ( - WithDescription = registry.WithDescription - WithUnit = registry.WithUnit -) diff --git a/api/metric/api.go b/api/metric/api.go index c96a9d0e9..37e56eae5 100644 --- a/api/metric/api.go +++ b/api/metric/api.go @@ -18,7 +18,6 @@ import ( "context" "go.opentelemetry.io/api/core" - "go.opentelemetry.io/api/registry" "go.opentelemetry.io/api/unit" ) @@ -40,31 +39,33 @@ type Float64Gauge interface { } type Handle struct { - Variable registry.Variable + Name string + Description string + Unit unit.Unit Type MetricType Keys []core.Key } -type Option func(*Handle, *[]registry.Option) +type Option func(*Handle) // WithDescription applies provided description. func WithDescription(desc string) Option { - return func(_ *Handle, to *[]registry.Option) { - *to = append(*to, registry.WithDescription(desc)) + return func(m *Handle) { + m.Description = desc } } // WithUnit applies provided unit. func WithUnit(unit unit.Unit) Option { - return func(_ *Handle, to *[]registry.Option) { - *to = append(*to, registry.WithUnit(unit)) + return func(m *Handle) { + m.Unit = unit } } // WithKeys applies the provided dimension keys. func WithKeys(keys ...core.Key) Option { - return func(m *Handle, _ *[]registry.Option) { + return func(m *Handle) { m.Keys = keys } } diff --git a/api/metric/common.go b/api/metric/common.go index 0ccad41e0..93d85ad7c 100644 --- a/api/metric/common.go +++ b/api/metric/common.go @@ -14,17 +14,11 @@ package metric -import ( - "go.opentelemetry.io/api/registry" -) - func registerMetric(name string, mtype MetricType, opts []Option, metric *Handle) { - var varOpts []registry.Option - for _, opt := range opts { - opt(metric, &varOpts) + opt(metric) } - metric.Variable = registry.Register(name, mtype, varOpts...) + metric.Name = name metric.Type = mtype } diff --git a/api/registry/doc.go b/api/registry/doc.go deleted file mode 100644 index 011326079..000000000 --- a/api/registry/doc.go +++ /dev/null @@ -1,15 +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 registry // import "go.opentelemetry.io/api/registry" diff --git a/api/registry/registry.go b/api/registry/registry.go deleted file mode 100644 index 094345bba..000000000 --- a/api/registry/registry.go +++ /dev/null @@ -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 registry - -import ( - "go.opentelemetry.io/api/unit" -) - -type Sequence uint64 - -type Option func(Variable) Variable - -type Variable struct { - Name string - Description string - Unit unit.Unit - Type Type -} - -type Type interface { - String() string -} - -func Register(name string, vtype Type, opts ...Option) Variable { - return newVar(name, vtype, opts...) -} - -func newVar(name string, vtype Type, opts ...Option) Variable { - v := Variable{ - Name: name, - Type: vtype, - } - for _, o := range opts { - v = o(v) - } - return v -} - -func (v *Variable) Defined() bool { - return len(v.Name) != 0 -} - -// WithDescription applies the provided description. -func WithDescription(desc string) Option { - return func(v Variable) Variable { - v.Description = desc - return v - } -} - -// WithUnit applies the provided unit. -func WithUnit(unit unit.Unit) Option { - return func(v Variable) Variable { - v.Unit = unit - return v - } -} diff --git a/api/stats/stats.go b/api/stats/stats.go index ac295ac87..df7f0de2f 100644 --- a/api/stats/stats.go +++ b/api/stats/stats.go @@ -19,15 +19,14 @@ import ( "sync/atomic" "go.opentelemetry.io/api/core" - "go.opentelemetry.io/api/registry" ) type MeasureHandle struct { - Variable registry.Variable + Name string } type Measure interface { - V() registry.Variable + N() string M(value float64) Measurement } @@ -71,20 +70,9 @@ func RecordSingle(ctx context.Context, m Measurement) { GlobalRecorder().RecordSingle(ctx, m) } -type AnyStatistic struct{} - -func (AnyStatistic) String() string { - return "AnyStatistic" -} - -var ( - WithDescription = registry.WithDescription - WithUnit = registry.WithUnit -) - -func NewMeasure(name string, opts ...registry.Option) *MeasureHandle { +func NewMeasure(name string) *MeasureHandle { return &MeasureHandle{ - Variable: registry.Register(name, AnyStatistic{}, opts...), + Name: name, } } @@ -95,8 +83,8 @@ func (m *MeasureHandle) M(value float64) Measurement { } } -func (m *MeasureHandle) V() registry.Variable { - return m.Variable +func (m *MeasureHandle) N() string { + return m.Name } func (noopRecorder) Record(ctx context.Context, m ...Measurement) { @@ -113,6 +101,6 @@ func (noopMeasure) M(float64) Measurement { return Measurement{} } -func (noopMeasure) V() registry.Variable { - return registry.Variable{} +func (noopMeasure) N() string { + return "" } diff --git a/api/tag/context.go b/api/tag/context.go index b178b6fdb..985efce2d 100644 --- a/api/tag/context.go +++ b/api/tag/context.go @@ -48,7 +48,7 @@ func Do(ctx context.Context, f func(ctx context.Context)) { m := FromContext(ctx) keyvals := make([]string, 0, 2*len(m.m)) for k, v := range m.m { - keyvals = append(keyvals, k.Variable.Name, v.value.Emit()) + keyvals = append(keyvals, k.Name, v.value.Emit()) } pprof.Do(ctx, pprof.Labels(keyvals...), f) } diff --git a/example/basic/main.go b/example/basic/main.go index 54bfa6d2f..1a03e88e2 100644 --- a/example/basic/main.go +++ b/example/basic/main.go @@ -19,7 +19,6 @@ import ( "go.opentelemetry.io/api/key" "go.opentelemetry.io/api/metric" - "go.opentelemetry.io/api/registry" "go.opentelemetry.io/api/stats" "go.opentelemetry.io/api/tag" "go.opentelemetry.io/api/trace" @@ -34,9 +33,9 @@ var ( meter = metric.GlobalMeter() // TODO: should share resources ^^^? - fooKey = key.New("ex.com/foo", registry.WithDescription("A Foo var")) - barKey = key.New("ex.com/bar", registry.WithDescription("A Bar var")) - lemonsKey = key.New("ex.com/lemons", registry.WithDescription("A Lemons var")) + fooKey = key.New("ex.com/foo") + barKey = key.New("ex.com/bar") + lemonsKey = key.New("ex.com/lemons") anotherKey = key.New("ex.com/another") oneMetric = metric.NewFloat64Gauge("ex.com/one", diff --git a/experimental/streaming/exporter/reader/format/format.go b/experimental/streaming/exporter/reader/format/format.go index 97d4aa337..c1de80482 100644 --- a/experimental/streaming/exporter/reader/format/format.go +++ b/experimental/streaming/exporter/reader/format/format.go @@ -37,7 +37,7 @@ func AppendEvent(buf *strings.Builder, data reader.Event) { if skipIf && data.Attributes.HasValue(kv.Key) { return true } - buf.WriteString(" " + kv.Key.Variable.Name + "=" + kv.Value.Emit()) + buf.WriteString(" " + kv.Key.Name + "=" + kv.Value.Emit()) return true } } @@ -74,7 +74,7 @@ func AppendEvent(buf *strings.Builder, data reader.Event) { buf.WriteString(data.Message) buf.WriteString(" (") data.Attributes.Foreach(func(kv core.KeyValue) bool { - buf.WriteString(" " + kv.Key.Variable.Name + "=" + kv.Value.Emit()) + buf.WriteString(" " + kv.Key.Name + "=" + kv.Value.Emit()) return true }) buf.WriteString(")") @@ -86,7 +86,7 @@ func AppendEvent(buf *strings.Builder, data reader.Event) { for _, s := range data.Stats { f(false)(core.Key{ - Variable: s.Measure.V(), + Name: s.Measure.N(), }.Float64(s.Value)) buf.WriteString(" {") @@ -96,7 +96,7 @@ func AppendEvent(buf *strings.Builder, data reader.Event) { buf.WriteString(",") } i++ - buf.WriteString(kv.Key.Variable.Name) + buf.WriteString(kv.Key.Name) buf.WriteString("=") buf.WriteString(kv.Value.Emit()) return true diff --git a/experimental/streaming/sdk/trace.go b/experimental/streaming/sdk/trace.go index 04183ef02..40e2a8c5c 100644 --- a/experimental/streaming/sdk/trace.go +++ b/experimental/streaming/sdk/trace.go @@ -37,9 +37,7 @@ var ( ErrorKey = key.New("error") SpanIDKey = key.New("span_id") TraceIDKey = key.New("trace_id") - MessageKey = key.New("message", - key.WithDescription("message text: info, error, etc"), - ) + MessageKey = key.New("message") ) func New() trace.Tracer { diff --git a/exporter/trace/jaeger/jaeger.go b/exporter/trace/jaeger/jaeger.go index e1ee8e7f0..4ddeb3bd7 100644 --- a/exporter/trace/jaeger/jaeger.go +++ b/exporter/trace/jaeger/jaeger.go @@ -187,7 +187,7 @@ func spanDataToThrift(data *trace.SpanData) *gen.Span { for _, a := range data.MessageEvents { fields := make([]*gen.Tag, 0, len(a.Attributes)) for _, kv := range a.Attributes { - tag := attributeToTag(kv.Key.Variable.Name, kv.Value.Emit()) + tag := attributeToTag(kv.Key.Name, kv.Value.Emit()) if tag != nil { fields = append(fields, tag) } diff --git a/plugin/httptrace/clienttrace.go b/plugin/httptrace/clienttrace.go index 2c85e365e..ee748409b 100644 --- a/plugin/httptrace/clienttrace.go +++ b/plugin/httptrace/clienttrace.go @@ -35,9 +35,7 @@ var ( HTTPHeaderMIME = key.New("http.mime") HTTPRemoteAddr = key.New("http.remote") HTTPLocalAddr = key.New("http.local") - MessageKey = key.New("message", - key.WithDescription("message text: info, error, etc"), - ) + MessageKey = key.New("message") ) type clientTracer struct { diff --git a/plugin/httptrace/httptrace.go b/plugin/httptrace/httptrace.go index 64c00597a..c2287c297 100644 --- a/plugin/httptrace/httptrace.go +++ b/plugin/httptrace/httptrace.go @@ -91,7 +91,7 @@ func (h hinjector) Inject(sc core.SpanContext, tags tag.Map) { // TODO: implement MaxHops tc.TraceState = append(tc.TraceState, tracestate.Member{ Vendor: Vendor, - Tenant: kv.Key.Variable.Name, + Tenant: kv.Key.Name, Value: kv.Value.Emit(), }) return true diff --git a/sdk/trace/span.go b/sdk/trace/span.go index 44179a79d..77e8588e9 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -227,7 +227,7 @@ func (s *span) lruAttributesToAttributeMap() map[string]interface{} { value, ok := s.lruAttributes.simpleLruMap.Get(key) if ok { key := key.(core.Key) - attributes[key.Variable.Name] = value + attributes[key.Name] = value } } return attributes