Skip to content
This repository was archived by the owner on Nov 7, 2022. It is now read-only.

Commit d62d215

Browse files
author
Bogdan Drutu
authored
Move observability, add test helper for observability and record metrics in loggingexporter. (#459)
1 parent ff3e303 commit d62d215

19 files changed

Lines changed: 239 additions & 70 deletions

File tree

cmd/ocagent/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ import (
3434
"go.uber.org/zap"
3535
"go.uber.org/zap/zapcore"
3636

37-
"github.com/census-instrumentation/opencensus-service/internal"
3837
"github.com/census-instrumentation/opencensus-service/internal/config"
3938
"github.com/census-instrumentation/opencensus-service/internal/config/viperutils"
4039
"github.com/census-instrumentation/opencensus-service/internal/pprofserver"
4140
"github.com/census-instrumentation/opencensus-service/internal/version"
41+
"github.com/census-instrumentation/opencensus-service/observability"
4242
"github.com/census-instrumentation/opencensus-service/processor"
4343
"github.com/census-instrumentation/opencensus-service/receiver/jaegerreceiver"
4444
"github.com/census-instrumentation/opencensus-service/receiver/opencensusreceiver"
@@ -230,7 +230,7 @@ func runOCReceiver(logger *zap.Logger, acfg *config.Config, tdp processor.TraceD
230230
if err != nil {
231231
return nil, fmt.Errorf("Failed to create the OpenCensus receiver on address %q: error %v", addr, err)
232232
}
233-
if err := view.Register(internal.AllViews...); err != nil {
233+
if err := view.Register(observability.AllViews...); err != nil {
234234
return nil, fmt.Errorf("Failed to register internal.AllViews: %v", err)
235235
}
236236

cmd/occollector/app/collector/telemetry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ import (
2525
"go.opencensus.io/stats/view"
2626
"go.uber.org/zap"
2727

28-
"github.com/census-instrumentation/opencensus-service/internal"
2928
"github.com/census-instrumentation/opencensus-service/internal/collector/processor"
3029
"github.com/census-instrumentation/opencensus-service/internal/collector/processor/nodebatcher"
3130
"github.com/census-instrumentation/opencensus-service/internal/collector/processor/queued"
3231
"github.com/census-instrumentation/opencensus-service/internal/collector/processor/tailsampling"
3332
"github.com/census-instrumentation/opencensus-service/internal/collector/telemetry"
33+
"github.com/census-instrumentation/opencensus-service/observability"
3434
)
3535

3636
const (
@@ -59,7 +59,7 @@ func initTelemetry(asyncErrorChannel chan<- error, v *viper.Viper, logger *zap.L
5959
views := processor.MetricViews(level)
6060
views = append(views, queued.MetricViews(level)...)
6161
views = append(views, nodebatcher.MetricViews(level)...)
62-
views = append(views, internal.AllViews...)
62+
views = append(views, observability.AllViews...)
6363
views = append(views, tailsampling.SamplingProcessorMetricViews(level)...)
6464
processMetricsViews := telemetry.NewProcessMetricsViews()
6565
views = append(views, processMetricsViews.Views()...)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exportertest
16+
17+
import (
18+
"encoding/json"
19+
)
20+
21+
// ToJSON marshals a generic interface to JSON to enable easy comparisons.
22+
func ToJSON(v interface{}) []byte {
23+
b, _ := json.MarshalIndent(v, "", " ")
24+
return b
25+
}

exporter/exportertest/sink_exporter.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package exportertest
1616

1717
import (
1818
"context"
19-
"encoding/json"
2019
"sync"
2120

2221
"github.com/census-instrumentation/opencensus-service/data"
@@ -86,9 +85,3 @@ func (sme *SinkMetricsExporter) AllMetrics() []data.MetricsData {
8685

8786
return sme.metrics[:]
8887
}
89-
90-
// ToJSON marshals a generic interface to JSON to enable easy comparisons.
91-
func ToJSON(v interface{}) []byte {
92-
b, _ := json.MarshalIndent(v, "", " ")
93-
return b
94-
}

exporter/loggingexporter/logging_exporter.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/census-instrumentation/opencensus-service/data"
2121
"github.com/census-instrumentation/opencensus-service/exporter"
22+
"github.com/census-instrumentation/opencensus-service/observability"
2223
"go.uber.org/zap"
2324
)
2425

@@ -30,21 +31,23 @@ type loggingExporter struct{ logger *zap.Logger }
3031
var _ exporter.TraceExporter = (*loggingExporter)(nil)
3132
var _ exporter.MetricsExporter = (*loggingExporter)(nil)
3233

33-
func (sp *loggingExporter) ProcessTraceData(ctx context.Context, td data.TraceData) error {
34-
// TODO: Record metrics
34+
func (le *loggingExporter) ProcessTraceData(ctx context.Context, td data.TraceData) error {
35+
le.logger.Debug("loggingTraceExporter", zap.Int("#spans", len(td.Spans)))
3536
// TODO: Add ability to record the received data
36-
sp.logger.Debug("loggingTraceExporter", zap.Int("#spans", len(td.Spans)))
37+
38+
// Even though we just log all the spans, we record 0 dropped spans.
39+
observability.RecordTraceExporterMetrics(observability.ContextWithExporterName(ctx, "logging_trace"), len(td.Spans), 0)
3740
return nil
3841
}
3942

40-
func (sp *loggingExporter) ProcessMetricsData(ctx context.Context, md data.MetricsData) error {
41-
sp.logger.Debug("loggingMetricsExporter", zap.Int("#metrics", len(md.Metrics)))
42-
// TODO: Record metrics
43+
func (le *loggingExporter) ProcessMetricsData(ctx context.Context, md data.MetricsData) error {
44+
le.logger.Debug("loggingMetricsExporter", zap.Int("#metrics", len(md.Metrics)))
4345
// TODO: Add ability to record the received data
46+
// TODO: Record metrics
4447
return nil
4548
}
4649

47-
func (sp *loggingExporter) ExportFormat() string {
50+
func (le *loggingExporter) ExportFormat() string {
4851
return exportFormat
4952
}
5053

exporter/loggingexporter/logging_exporter_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,39 @@ import (
2020
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
2121
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
2222
"github.com/census-instrumentation/opencensus-service/data"
23+
"github.com/census-instrumentation/opencensus-service/observability/observabilitytest"
2324
"go.uber.org/zap"
2425
)
2526

2627
func TestLoggingTraceExporterNoErrors(t *testing.T) {
27-
dtdp := NewTraceExporter(zap.NewNop())
28+
lte := NewTraceExporter(zap.NewNop())
2829
td := data.TraceData{
2930
Spans: make([]*tracepb.Span, 7),
3031
}
31-
if err := dtdp.ProcessTraceData(context.Background(), td); err != nil {
32+
if err := lte.ProcessTraceData(context.Background(), td); err != nil {
3233
t.Errorf("Wanted nil got error")
3334
return
3435
}
35-
if "LoggingExporter" != dtdp.ExportFormat() {
36-
t.Errorf("Wanted LoggingExporter got %v", dtdp.ExportFormat())
36+
if "LoggingExporter" != lte.ExportFormat() {
37+
t.Errorf("Wanted LoggingExporter got %v", lte.ExportFormat())
3738
}
3839
}
3940

41+
func TestLoggingTraceExporterRecordMetrics(t *testing.T) {
42+
lte := NewTraceExporter(zap.NewNop())
43+
observabilitytest.CheckRecordedMetricsForTraceExporter(t, lte, "logging_trace")
44+
}
45+
4046
func TestLoggingMetricsExporterNoErrors(t *testing.T) {
41-
dmdp := NewMetricsExporter(zap.NewNop())
47+
lme := NewMetricsExporter(zap.NewNop())
4248
md := data.MetricsData{
4349
Metrics: make([]*metricspb.Metric, 7),
4450
}
45-
if err := dmdp.ProcessMetricsData(context.Background(), md); err != nil {
51+
if err := lme.ProcessMetricsData(context.Background(), md); err != nil {
4652
t.Errorf("Wanted nil got error")
4753
return
4854
}
49-
if "LoggingExporter" != dmdp.ExportFormat() {
50-
t.Errorf("Wanted LoggingExporter got %v", dmdp.ExportFormat())
55+
if "LoggingExporter" != lme.ExportFormat() {
56+
t.Errorf("Wanted LoggingExporter got %v", lme.ExportFormat())
5157
}
5258
}

exporter/opencensusexporter/opencensus.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424

2525
agenttracepb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/trace/v1"
2626
"github.com/census-instrumentation/opencensus-service/data"
27-
"github.com/census-instrumentation/opencensus-service/internal"
2827
"github.com/census-instrumentation/opencensus-service/internal/compression"
2928
"github.com/census-instrumentation/opencensus-service/internal/compression/grpc"
29+
"github.com/census-instrumentation/opencensus-service/observability"
3030
"github.com/census-instrumentation/opencensus-service/processor"
3131
)
3232

@@ -117,14 +117,14 @@ func (oce *ocagentExporter) ProcessTraceData(ctx context.Context, td data.TraceD
117117
Node: td.Node,
118118
},
119119
)
120-
ctxWithExporterName := internal.ContextWithExporterName(ctx, exporterTagValue)
120+
ctxWithExporterName := observability.ContextWithExporterName(ctx, exporterTagValue)
121121
if err != nil {
122122
// TODO: If failed to send all maybe record a different metric. Failed to "Sent", but
123123
// this may not be accurate if we have retry outside of this exporter. Maybe the retry
124124
// processor should record these metrics. For the moment we assume no retry.
125-
internal.RecordTraceExporterMetrics(ctxWithExporterName, len(td.Spans), len(td.Spans))
125+
observability.RecordTraceExporterMetrics(ctxWithExporterName, len(td.Spans), len(td.Spans))
126126
return err
127127
}
128-
internal.RecordTraceExporterMetrics(ctxWithExporterName, len(td.Spans), 0)
128+
observability.RecordTraceExporterMetrics(ctxWithExporterName, len(td.Spans), 0)
129129
return nil
130130
}

exporter/zipkinexporter/zipkin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131

3232
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
3333
"github.com/census-instrumentation/opencensus-service/data"
34-
"github.com/census-instrumentation/opencensus-service/internal"
34+
"github.com/census-instrumentation/opencensus-service/observability"
3535
"github.com/census-instrumentation/opencensus-service/processor"
3636
spandatatranslator "github.com/census-instrumentation/opencensus-service/translator/trace/spandata"
3737
)
@@ -221,7 +221,7 @@ func (ze *zipkinExporter) ProcessTraceData(ctx context.Context, td data.TraceDat
221221
}
222222

223223
// And finally record metrics on the number of exported spans.
224-
internal.RecordTraceExporterMetrics(internal.ContextWithExporterName(ctx, "zipkin"), len(td.Spans), len(td.Spans)-goodSpans)
224+
observability.RecordTraceExporterMetrics(observability.ContextWithExporterName(ctx, "zipkin"), len(td.Spans), len(td.Spans)-goodSpans)
225225

226226
return nil
227227
}
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package internal
15+
package observability
1616

1717
// This file contains helpers that are useful to add observability
1818
// with metrics and tracing using OpenCensus to the various pieces
@@ -31,22 +31,26 @@ import (
3131
)
3232

3333
var (
34-
tagKeyReceiver, _ = tag.NewKey("oc_receiver")
3534
mReceiverReceivedSpans = stats.Int64("oc.io/receiver/received_spans", "Counts the number of spans received by the receiver", "1")
3635
mReceiverDroppedSpans = stats.Int64("oc.io/receiver/dropped_spans", "Counts the number of spans dropped by the receiver", "1")
3736

38-
tagKeyExporter, _ = tag.NewKey("oc_exporter")
3937
mExporterReceivedSpans = stats.Int64("oc.io/exporter/received_spans", "Counts the number of spans received by the exporter", "1")
4038
mExporterDroppedSpans = stats.Int64("oc.io/exporter/dropped_spans", "Counts the number of spans received by the exporter", "1")
4139
)
4240

41+
// TagKeyReceiver defines tag key for Receiver.
42+
var TagKeyReceiver, _ = tag.NewKey("oc_receiver")
43+
44+
// TagKeyExporter defines tag key for Exporter.
45+
var TagKeyExporter, _ = tag.NewKey("oc_exporter")
46+
4347
// ViewReceiverReceivedSpans defines the view for the receiver received spans metric.
4448
var ViewReceiverReceivedSpans = &view.View{
4549
Name: mReceiverReceivedSpans.Name(),
4650
Description: mReceiverReceivedSpans.Description(),
4751
Measure: mReceiverReceivedSpans,
4852
Aggregation: view.Sum(),
49-
TagKeys: []tag.Key{tagKeyReceiver},
53+
TagKeys: []tag.Key{TagKeyReceiver},
5054
}
5155

5256
// ViewReceiverDroppedSpans defines the view for the receiver dropped spans metric.
@@ -55,7 +59,7 @@ var ViewReceiverDroppedSpans = &view.View{
5559
Description: mReceiverDroppedSpans.Description(),
5660
Measure: mReceiverDroppedSpans,
5761
Aggregation: view.Sum(),
58-
TagKeys: []tag.Key{tagKeyReceiver},
62+
TagKeys: []tag.Key{TagKeyReceiver},
5963
}
6064

6165
// ViewExporterReceivedSpans defines the view for the exporter received spans metric.
@@ -64,7 +68,7 @@ var ViewExporterReceivedSpans = &view.View{
6468
Description: mExporterReceivedSpans.Description(),
6569
Measure: mExporterReceivedSpans,
6670
Aggregation: view.Sum(),
67-
TagKeys: []tag.Key{tagKeyReceiver, tagKeyExporter},
71+
TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter},
6872
}
6973

7074
// ViewExporterDroppedSpans defines the view for the exporter dropped spans metric.
@@ -73,7 +77,7 @@ var ViewExporterDroppedSpans = &view.View{
7377
Description: mExporterDroppedSpans.Description(),
7478
Measure: mExporterDroppedSpans,
7579
Aggregation: view.Sum(),
76-
TagKeys: []tag.Key{tagKeyReceiver, tagKeyExporter},
80+
TagKeys: []tag.Key{TagKeyReceiver, TagKeyExporter},
7781
}
7882

7983
// AllViews has the views for the metrics provided by the agent.
@@ -88,7 +92,7 @@ var AllViews = []*view.View{
8892
// and returns the newly created context. For receivers that can receive multiple signals it is
8993
// recommended to encode the signal as suffix (e.g. "oc_trace" and "oc_metrics").
9094
func ContextWithReceiverName(ctx context.Context, receiverName string) context.Context {
91-
ctx, _ = tag.New(ctx, tag.Upsert(tagKeyReceiver, receiverName))
95+
ctx, _ = tag.New(ctx, tag.Upsert(TagKeyReceiver, receiverName))
9296
return ctx
9397
}
9498

@@ -102,7 +106,7 @@ func RecordTraceReceiverMetrics(ctxWithTraceReceiverName context.Context, receiv
102106
// and returns the newly created context. For exporters that can export multiple signals it is
103107
// recommended to encode the signal as suffix (e.g. "oc_trace" and "oc_metrics").
104108
func ContextWithExporterName(ctx context.Context, exporterName string) context.Context {
105-
ctx, _ = tag.New(ctx, tag.Upsert(tagKeyExporter, exporterName))
109+
ctx, _ = tag.New(ctx, tag.Upsert(TagKeyExporter, exporterName))
106110
return ctx
107111
}
108112

0 commit comments

Comments
 (0)