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

Commit a51ae01

Browse files
author
Bogdan Drutu
authored
Move debug processor to exporters, because no next. (#443)
* Move debug processor to exporters, because no next. * Remove another hardcoded debug exporter. * Add tests for ExportFormat. * Renmae debug to logging. * Renmae New[Trace|Metrics]DataExporter to New[Trace|Metrics]Exporter.
1 parent 3ac4886 commit a51ae01

7 files changed

Lines changed: 86 additions & 102 deletions

File tree

cmd/occollector/app/builder/builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
ocReceiverFlg = "receive-oc-trace"
3737
zipkinReceiverFlg = "receive-zipkin"
3838
zipkinScribeReceiverFlg = "receive-zipkin-scribe"
39-
debugProcessorFlg = "debug-processor"
39+
loggingExporterFlg = "logging-exporter"
4040
useTailSamplingAlwaysSample = "tail-sampling-always-sample"
4141
)
4242

@@ -51,7 +51,7 @@ func Flags(flags *flag.FlagSet) {
5151
fmt.Sprintf("Flag to run the Zipkin receiver, default settings: %+v", *NewDefaultZipkinReceiverCfg()))
5252
flags.Bool(zipkinScribeReceiverFlg, false,
5353
fmt.Sprintf("Flag to run the Zipkin Scribe receiver, default settings: %+v", *NewDefaultZipkinScribeReceiverCfg()))
54-
flags.Bool(debugProcessorFlg, false, "Flag to add a debug processor (combine with log level DEBUG to log incoming spans)")
54+
flags.Bool(loggingExporterFlg, false, "Flag to add a logging exporter (combine with log level DEBUG to log incoming spans)")
5555
flags.Bool(useTailSamplingAlwaysSample, false, "Flag to use a tail-based sampling processor with an always sample policy, "+
5656
"unless tail sampling setting is present on configuration file.")
5757
}
@@ -61,9 +61,9 @@ func GetConfigFile(v *viper.Viper) string {
6161
return v.GetString(configCfg)
6262
}
6363

64-
// DebugProcessorEnabled returns true if the debug processor is enabled, and false otherwise
65-
func DebugProcessorEnabled(v *viper.Viper) bool {
66-
return v.GetBool(debugProcessorFlg)
64+
// LoggingExporterEnabled returns true if the debug processor is enabled, and false otherwise
65+
func LoggingExporterEnabled(v *viper.Viper) bool {
66+
return v.GetBool(loggingExporterFlg)
6767
}
6868

6969
// DebugTailSamplingEnabled returns true if the debug processor is enabled, and false otherwise

cmd/occollector/app/collector/processors.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/census-instrumentation/opencensus-service/cmd/occollector/app/builder"
2828
"github.com/census-instrumentation/opencensus-service/cmd/occollector/app/sender"
29+
"github.com/census-instrumentation/opencensus-service/exporter/loggingexporter"
2930
"github.com/census-instrumentation/opencensus-service/internal/collector/processor"
3031
"github.com/census-instrumentation/opencensus-service/internal/collector/processor/nodebatcher"
3132
"github.com/census-instrumentation/opencensus-service/internal/collector/processor/queued"
@@ -246,8 +247,9 @@ func startProcessor(v *viper.Viper, logger *zap.Logger) (processor.SpanProcessor
246247
// TODO: (@pjanotti) make use of metrics exporters
247248
_ = metricsExporters
248249

249-
if builder.DebugProcessorEnabled(v) {
250-
dbgProc := processor.NewNoopSpanProcessor(logger)
250+
if builder.LoggingExporterEnabled(v) {
251+
dbgProc := processor.NewTraceExporterProcessor(loggingexporter.NewTraceExporter(logger))
252+
// TODO: Add this to the exporters list and avoid treating it specially. Don't know all the implications.
251253
nameToSpanProcessor["debug"] = dbgProc
252254
spanProcessors = append(spanProcessors, dbgProc)
253255
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 loggingexporter
16+
17+
import (
18+
"context"
19+
20+
"github.com/census-instrumentation/opencensus-service/data"
21+
"github.com/census-instrumentation/opencensus-service/exporter"
22+
"go.uber.org/zap"
23+
)
24+
25+
const exportFormat = "LoggingExporter"
26+
27+
// A logging exporter that does not sends the data to any destination but logs debugging messages.
28+
type loggingExporter struct{ logger *zap.Logger }
29+
30+
var _ exporter.TraceDataExporter = (*loggingExporter)(nil)
31+
var _ exporter.MetricsDataExporter = (*loggingExporter)(nil)
32+
33+
func (sp *loggingExporter) ProcessTraceData(ctx context.Context, td data.TraceData) error {
34+
// TODO: Record metrics
35+
// TODO: Add ability to record the received data
36+
sp.logger.Debug("loggingTraceDataExporter", zap.Int("#spans", len(td.Spans)))
37+
return nil
38+
}
39+
40+
func (sp *loggingExporter) ProcessMetricsData(ctx context.Context, md data.MetricsData) error {
41+
sp.logger.Debug("loggingMetricsDataExporter", zap.Int("#metrics", len(md.Metrics)))
42+
// TODO: Record metrics
43+
// TODO: Add ability to record the received data
44+
return nil
45+
}
46+
47+
func (sp *loggingExporter) ExportFormat() string {
48+
return exportFormat
49+
}
50+
51+
// NewTraceExporter creates an exporter.TraceDataExporter that just drops the
52+
// received data and logs debugging messages.
53+
func NewTraceExporter(logger *zap.Logger) exporter.TraceDataExporter {
54+
return &loggingExporter{logger: logger}
55+
}
56+
57+
// NewMetricsExporter creates an exporter.MetricsDataExporter that just drops the
58+
// received data and logs debugging messages.
59+
func NewMetricsExporter(logger *zap.Logger) exporter.MetricsDataExporter {
60+
return &loggingExporter{logger: logger}
61+
}

processor/debug_processor_test.go renamed to exporter/loggingexporter/logging_exporter_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
package processor
14+
package loggingexporter
1515

1616
import (
1717
"context"
@@ -20,27 +20,33 @@ 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-
"go.uber.org/zap/zaptest"
23+
"go.uber.org/zap"
2424
)
2525

26-
func TestDebugTraceDataProcessorNoErrors(t *testing.T) {
27-
dtdp := NewDebugTraceDataProcessor(zaptest.NewLogger(t))
26+
func TestLoggingTraceExporterNoErrors(t *testing.T) {
27+
dtdp := NewTraceExporter(zap.NewNop())
2828
td := data.TraceData{
2929
Spans: make([]*tracepb.Span, 7),
3030
}
3131
if err := dtdp.ProcessTraceData(context.Background(), td); err != nil {
3232
t.Errorf("Wanted nil got error")
3333
return
3434
}
35+
if "LoggingExporter" != dtdp.ExportFormat() {
36+
t.Errorf("Wanted LoggingExporter got %v", dtdp.ExportFormat())
37+
}
3538
}
3639

37-
func TestDebugMetricsDataProcessorNoErrors(t *testing.T) {
38-
dmdp := NewDebugMetricsDataProcessor(zaptest.NewLogger(t))
40+
func TestLoggingMetricsExporterNoErrors(t *testing.T) {
41+
dmdp := NewMetricsExporter(zap.NewNop())
3942
md := data.MetricsData{
4043
Metrics: make([]*metricspb.Metric, 7),
4144
}
4245
if err := dmdp.ProcessMetricsData(context.Background(), md); err != nil {
4346
t.Errorf("Wanted nil got error")
4447
return
4548
}
49+
if "LoggingExporter" != dmdp.ExportFormat() {
50+
t.Errorf("Wanted LoggingExporter got %v", dmdp.ExportFormat())
51+
}
4652
}

internal/collector/processor/processor.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
package processor
1616

1717
import (
18-
"context"
19-
2018
commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
2119
"go.opencensus.io/stats"
2220
"go.opencensus.io/stats/view"
2321
"go.opencensus.io/tag"
24-
"go.uber.org/zap"
2522

2623
"github.com/census-instrumentation/opencensus-service/data"
2724
"github.com/census-instrumentation/opencensus-service/internal/collector/telemetry"
@@ -34,29 +31,6 @@ type SpanProcessor interface {
3431
// TODO: (@pjanotti) For shutdown improvement, the interface needs a method to attempt that.
3532
}
3633

37-
// An initial processor that does not sends the data to any destination but helps debugging.
38-
type debugSpanProcessor struct{ logger *zap.Logger }
39-
40-
var _ SpanProcessor = (*debugSpanProcessor)(nil)
41-
42-
func (sp *debugSpanProcessor) ProcessSpans(td data.TraceData, spanFormat string) error {
43-
if td.Node == nil {
44-
sp.logger.Warn("Received batch with nil Node", zap.String("format", spanFormat))
45-
}
46-
47-
statsTags := StatsTagsForBatch("debug", ServiceNameForNode(td.Node), spanFormat)
48-
numSpans := len(td.Spans)
49-
stats.RecordWithTags(context.Background(), statsTags, StatReceivedSpanCount.M(int64(numSpans)))
50-
51-
sp.logger.Debug("debugSpanProcessor", zap.String("originalFormat", spanFormat), zap.Int("#spans", numSpans))
52-
return nil
53-
}
54-
55-
// NewNoopSpanProcessor creates an OC SpanProcessor that just drops the received data.
56-
func NewNoopSpanProcessor(logger *zap.Logger) SpanProcessor {
57-
return &debugSpanProcessor{logger: logger}
58-
}
59-
6034
// Keys and stats for telemetry.
6135
var (
6236
TagSourceFormatKey, _ = tag.NewKey("format")

processor/debug_processor.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

receiver/end_to_end_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ package receiver_test
1616

1717
import (
1818
"context"
19-
"encoding/json"
2019
"log"
2120
"time"
2221

22+
"github.com/census-instrumentation/opencensus-service/exporter/loggingexporter"
23+
2324
"contrib.go.opencensus.io/exporter/ocagent"
2425
"go.opencensus.io/trace"
2526

26-
"github.com/census-instrumentation/opencensus-service/data"
27-
"github.com/census-instrumentation/opencensus-service/processor"
2827
"github.com/census-instrumentation/opencensus-service/receiver"
2928
"github.com/census-instrumentation/opencensus-service/receiver/opencensusreceiver"
29+
"go.uber.org/zap"
3030
)
3131

3232
func Example_endToEnd() {
@@ -43,7 +43,7 @@ func Example_endToEnd() {
4343

4444
// Once we have the span receiver which will connect to the
4545
// various exporter pipeline i.e. *tracepb.Span->OpenCensus.SpanData
46-
lsr := new(logSpanSink)
46+
lsr := loggingexporter.NewTraceExporter(zap.NewNop())
4747
for _, tr := range trl {
4848
if err := tr.StartTraceReception(context.Background(), lsr); err != nil {
4949
log.Fatalf("Failed to start trace receiver: %v", err)
@@ -90,14 +90,3 @@ func Example_endToEnd() {
9090
oce.Flush()
9191
<-time.After(5 * time.Second)
9292
}
93-
94-
type logSpanSink int
95-
96-
var _ processor.TraceDataProcessor = (*logSpanSink)(nil)
97-
98-
func (lsr *logSpanSink) ProcessTraceData(ctx context.Context, td data.TraceData) error {
99-
spansBlob, _ := json.MarshalIndent(td.Spans, " ", " ")
100-
log.Printf("\n****\nNode: %#v\nSpans: %s\n****\n", td.Node, spansBlob)
101-
102-
return nil
103-
}

0 commit comments

Comments
 (0)