diff --git a/metric/metricdata/exemplar.go b/metric/metricdata/exemplar.go index 8a89ed073..2428151be 100644 --- a/metric/metricdata/exemplar.go +++ b/metric/metricdata/exemplar.go @@ -15,7 +15,6 @@ package metricdata import ( - "context" "time" ) @@ -38,40 +37,4 @@ type Exemplar struct { } // Attachments is a map of extra values associated with a recorded data point. -// The map should only be mutated from AttachmentExtractor functions. type Attachments map[string]string - -// AttachmentExtractor is a function capable of extracting exemplar attachments -// from the context used to record measurements. -// The map passed to the function should be mutated and returned. It will -// initially be nil: the first AttachmentExtractor that would like to add keys to the -// map is responsible for initializing it. -type AttachmentExtractor func(ctx context.Context, a Attachments) Attachments - -var extractors []AttachmentExtractor - -// RegisterAttachmentExtractor registers the given extractor associated with the exemplar -// type name. -// -// Extractors will be used to attempt to extract exemplars from the context -// associated with each recorded measurement. -// -// Packages that support exemplars should register their extractor functions on -// initialization. -// -// RegisterAttachmentExtractor should not be called after any measurements have -// been recorded. -func RegisterAttachmentExtractor(e AttachmentExtractor) { - extractors = append(extractors, e) -} - -// AttachmentsFromContext extracts exemplars from the given context. -// Each registered AttachmentExtractor (see RegisterAttachmentExtractor) is called in an -// unspecified order to add attachments to the exemplar. -func AttachmentsFromContext(ctx context.Context) Attachments { - var a Attachments - for _, extractor := range extractors { - a = extractor(ctx, a) - } - return a -} diff --git a/stats/record.go b/stats/record.go index 5f063f2ba..a0f0ec4be 100644 --- a/stats/record.go +++ b/stats/record.go @@ -18,7 +18,6 @@ package stats import ( "context" - "go.opencensus.io/metric/metricdata" "go.opencensus.io/stats/internal" "go.opencensus.io/tag" ) @@ -51,7 +50,8 @@ func Record(ctx context.Context, ms ...Measurement) { if !record { return } - recorder(tag.FromContext(ctx), ms, metricdata.AttachmentsFromContext(ctx)) + // TODO(songy23): fix attachments. + recorder(tag.FromContext(ctx), ms, map[string]string{}) } // RecordWithTags records one or multiple measurements at once. diff --git a/stats/view/view_test.go b/stats/view/view_test.go index 7d9b23e8a..10012409f 100644 --- a/stats/view/view_test.go +++ b/stats/view/view_test.go @@ -179,7 +179,7 @@ func Test_View_MeasureFloat64_AggregationDistribution(t *testing.T) { } e := &metricdata.Exemplar{ Value: r.f, - Attachments: metricdata.AttachmentsFromContext(ctx), + Attachments: map[string]string{}, } view.addSample(tag.FromContext(ctx), e) } diff --git a/tag/context.go b/tag/context.go index d42d0f07a..b27d1b26b 100644 --- a/tag/context.go +++ b/tag/context.go @@ -17,8 +17,6 @@ package tag import ( "context" - - "go.opencensus.io/metric/metricdata" ) // FromContext returns the tag map stored in the context. @@ -43,25 +41,3 @@ func NewContext(ctx context.Context, m *Map) context.Context { type ctxKey struct{} var mapCtxKey = ctxKey{} - -func init() { - metricdata.RegisterAttachmentExtractor(extractTagsAttachments) -} - -func extractTagsAttachments(ctx context.Context, a metricdata.Attachments) metricdata.Attachments { - m := FromContext(ctx) - if m == nil { - return a - } - if len(m.m) == 0 { - return a - } - if a == nil { - a = make(map[string]string) - } - - for k, v := range m.m { - a[metricdata.KeyPrefixTag+k.Name()] = v - } - return a -} diff --git a/tag/context_test.go b/tag/context_test.go deleted file mode 100644 index e85b1c40c..000000000 --- a/tag/context_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2018, OpenCensus 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 tag - -import ( - "context" - "testing" -) - -func TestExtractTagsAttachment(t *testing.T) { - // We can't depend on the stats of view package without creating a - // dependency cycle. - - var m map[string]string - ctx := context.Background() - - res := extractTagsAttachments(ctx, m) - if res != nil { - t.Fatalf("res = %v; want nil", res) - } - - k, _ := NewKey("test") - ctx, _ = New(ctx, Insert(k, "test123")) - res = extractTagsAttachments(ctx, m) - if res == nil { - t.Fatal("res = nil") - } - if got, want := res["tag:test"], "test123"; got != want { - t.Fatalf("res[Tags:test] = %v; want %v", got, want) - } -} diff --git a/trace/exemplar.go b/trace/exemplar.go deleted file mode 100644 index 522323730..000000000 --- a/trace/exemplar.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2018, OpenCensus 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 trace - -import ( - "context" - "encoding/hex" - - "go.opencensus.io/metric/metricdata" -) - -func init() { - metricdata.RegisterAttachmentExtractor(attachSpanContext) -} - -func attachSpanContext(ctx context.Context, a metricdata.Attachments) metricdata.Attachments { - span := FromContext(ctx) - if span == nil { - return a - } - sc := span.SpanContext() - if !sc.IsSampled() { - return a - } - if a == nil { - a = make(metricdata.Attachments) - } - a[metricdata.KeyTraceID] = hex.EncodeToString(sc.TraceID[:]) - a[metricdata.KeySpanID] = hex.EncodeToString(sc.SpanID[:]) - return a -} diff --git a/trace/exemplar_test.go b/trace/exemplar_test.go deleted file mode 100644 index 5d6f62d5c..000000000 --- a/trace/exemplar_test.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2018, OpenCensus 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 trace_test - -import ( - "context" - "testing" - - "go.opencensus.io/stats" - "go.opencensus.io/stats/view" - "go.opencensus.io/trace" -) - -func TestTraceExemplar(t *testing.T) { - m := stats.Float64("measure."+t.Name(), "", stats.UnitDimensionless) - v := &view.View{ - Measure: m, - Aggregation: view.Distribution(1, 2, 3), - } - view.Register(v) - ctx := context.Background() - ctx, span := trace.StartSpan(ctx, t.Name(), trace.WithSampler(trace.AlwaysSample())) - stats.Record(ctx, m.M(1.5)) - span.End() - - rows, err := view.RetrieveData(v.Name) - if err != nil { - t.Fatal(err) - } - if len(rows) == 0 { - t.Fatal("len(rows) = 0; want > 0") - } - dd := rows[0].Data.(*view.DistributionData) - if got := len(dd.ExemplarsPerBucket); got < 3 { - t.Fatalf("len(dd.ExemplarsPerBucket) = %d; want >= 2", got) - } - exemplar := dd.ExemplarsPerBucket[1] - if exemplar == nil { - t.Fatal("Expected exemplar") - } - if got, want := exemplar.Value, 1.5; got != want { - t.Fatalf("exemplar.Value = %v; got %v", got, want) - } - if _, ok := exemplar.Attachments["trace_id"]; !ok { - t.Fatalf("exemplar.Attachments = %v; want trace_id key", exemplar.Attachments) - } - if _, ok := exemplar.Attachments["span_id"]; !ok { - t.Fatalf("exemplar.Attachments = %v; want span_id key", exemplar.Attachments) - } -} - -func TestTraceExemplar_notSampled(t *testing.T) { - m := stats.Float64("measure."+t.Name(), "", stats.UnitDimensionless) - v := &view.View{ - Measure: m, - Aggregation: view.Distribution(1, 2, 3), - } - view.Register(v) - ctx := context.Background() - ctx, span := trace.StartSpan(ctx, t.Name(), trace.WithSampler(trace.NeverSample())) - stats.Record(ctx, m.M(1.5)) - span.End() - - rows, err := view.RetrieveData(v.Name) - if err != nil { - t.Fatal(err) - } - if len(rows) == 0 { - t.Fatal("len(rows) = 0; want > 0") - } - dd := rows[0].Data.(*view.DistributionData) - if got := len(dd.ExemplarsPerBucket); got < 3 { - t.Fatalf("len(buckets) = %d; want >= 2", got) - } - exemplar := dd.ExemplarsPerBucket[1] - if exemplar == nil { - t.Fatal("Expected exemplar") - } - if got, want := exemplar.Value, 1.5; got != want { - t.Fatalf("exemplar.Value = %v; got %v", got, want) - } - if _, ok := exemplar.Attachments["trace_id"]; ok { - t.Fatalf("exemplar.Attachments = %v; want no trace_id", exemplar.Attachments) - } - if _, ok := exemplar.Attachments["span_id"]; ok { - t.Fatalf("exemplar.Attachments = %v; want span_id key", exemplar.Attachments) - } -}