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

Commit a4e04da

Browse files
author
Bogdan Drutu
authored
Rename noop to nop, add nop exporter, rename TraceDataExporter to TraceExporter. (#447)
* Rename noop to nop, add nop exporter, rename TraceDataExporter to TraceExporter. * Rename pointers to match the new struct name. * Change nop_processor comments. * Cleanup more names and add tests.
1 parent 172e98a commit a4e04da

15 files changed

Lines changed: 378 additions & 180 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
"context"
19+
20+
"github.com/census-instrumentation/opencensus-service/data"
21+
"github.com/census-instrumentation/opencensus-service/exporter"
22+
)
23+
24+
type nopExporter int
25+
26+
var _ exporter.TraceExporter = (*nopExporter)(nil)
27+
var _ exporter.MetricsExporter = (*nopExporter)(nil)
28+
29+
func (ne *nopExporter) ProcessTraceData(ctx context.Context, td data.TraceData) error {
30+
return nil
31+
}
32+
33+
func (ne *nopExporter) ProcessMetricsData(ctx context.Context, md data.MetricsData) error {
34+
return nil
35+
}
36+
37+
const nopExportFormat = "NopExporter"
38+
39+
func (ne *nopExporter) ExportFormat() string {
40+
return nopExportFormat
41+
}
42+
43+
// NewNopTraceExporter creates an TraceExporter that just drops the received data.
44+
func NewNopTraceExporter() exporter.TraceExporter {
45+
return new(nopExporter)
46+
}
47+
48+
// NewNopMetricsExporter creates an MetricsExporter that just drops the received data.
49+
func NewNopMetricsExporter() exporter.MetricsExporter {
50+
return new(nopExporter)
51+
}

processor/processortest/noop_processor_test.go renamed to exporter/exportertest/nop_exporter_test.go

Lines changed: 15 additions & 7 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 processortest
14+
package exportertest
1515

1616
import (
1717
"context"
@@ -22,24 +22,32 @@ import (
2222
"github.com/census-instrumentation/opencensus-service/data"
2323
)
2424

25-
func TestNoopTraceDataProcessorNoErrors(t *testing.T) {
26-
ntdp := NewNoopTraceDataProcessor()
25+
func TestNopTraceExporterNoErrors(t *testing.T) {
26+
nte := NewNopTraceExporter()
2727
td := data.TraceData{
2828
Spans: make([]*tracepb.Span, 7),
2929
}
30-
if err := ntdp.ProcessTraceData(context.Background(), td); err != nil {
30+
if err := nte.ProcessTraceData(context.Background(), td); err != nil {
3131
t.Errorf("Wanted nil got error")
3232
return
3333
}
34+
if "NopExporter" != nte.ExportFormat() {
35+
t.Errorf("Wanted NopExporter got %s", nte.ExportFormat())
36+
return
37+
}
3438
}
3539

36-
func TestNoopMetricsDataProcessorNoErrors(t *testing.T) {
37-
nmdp := NewNoopMetricsDataProcessor()
40+
func TestNoopMetricsExporterNoErrors(t *testing.T) {
41+
nme := NewNopMetricsExporter()
3842
md := data.MetricsData{
3943
Metrics: make([]*metricspb.Metric, 7),
4044
}
41-
if err := nmdp.ProcessMetricsData(context.Background(), md); err != nil {
45+
if err := nme.ProcessMetricsData(context.Background(), md); err != nil {
4246
t.Errorf("Wanted nil got error")
4347
return
4448
}
49+
if "NopExporter" != nme.ExportFormat() {
50+
t.Errorf("Wanted NopExporter got %s", nme.ExportFormat())
51+
return
52+
}
4553
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2018, 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+
"context"
19+
"encoding/json"
20+
"sync"
21+
22+
"github.com/census-instrumentation/opencensus-service/data"
23+
"github.com/census-instrumentation/opencensus-service/exporter"
24+
)
25+
26+
// SinkTraceExporter acts as a trace receiver for use in tests.
27+
type SinkTraceExporter struct {
28+
mu sync.Mutex
29+
traces []data.TraceData
30+
}
31+
32+
var _ exporter.TraceExporter = (*SinkTraceExporter)(nil)
33+
34+
// ProcessTraceData stores traces for tests.
35+
func (ste *SinkTraceExporter) ProcessTraceData(ctx context.Context, td data.TraceData) error {
36+
ste.mu.Lock()
37+
defer ste.mu.Unlock()
38+
39+
ste.traces = append(ste.traces, td)
40+
41+
return nil
42+
}
43+
44+
const sinkExportFormat = "SinkExporter"
45+
46+
// ExportFormat retruns the name of this TraceExporter
47+
func (ste *SinkTraceExporter) ExportFormat() string {
48+
return sinkExportFormat
49+
}
50+
51+
// AllTraces returns the traces sent to the test sink.
52+
func (ste *SinkTraceExporter) AllTraces() []data.TraceData {
53+
ste.mu.Lock()
54+
defer ste.mu.Unlock()
55+
56+
return ste.traces[:]
57+
}
58+
59+
// SinkMetricsExporter acts as a metrics receiver for use in tests.
60+
type SinkMetricsExporter struct {
61+
mu sync.Mutex
62+
metrics []data.MetricsData
63+
}
64+
65+
var _ exporter.MetricsExporter = (*SinkMetricsExporter)(nil)
66+
67+
// ProcessMetricsData stores traces for tests.
68+
func (sme *SinkMetricsExporter) ProcessMetricsData(ctx context.Context, md data.MetricsData) error {
69+
sme.mu.Lock()
70+
defer sme.mu.Unlock()
71+
72+
sme.metrics = append(sme.metrics, md)
73+
74+
return nil
75+
}
76+
77+
// ExportFormat retruns the name of this TraceExporter
78+
func (sme *SinkMetricsExporter) ExportFormat() string {
79+
return sinkExportFormat
80+
}
81+
82+
// AllMetrics returns the metrics sent to the test sink.
83+
func (sme *SinkMetricsExporter) AllMetrics() []data.MetricsData {
84+
sme.mu.Lock()
85+
defer sme.mu.Unlock()
86+
87+
return sme.metrics[:]
88+
}
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+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
package exportertest
15+
16+
import (
17+
"context"
18+
"reflect"
19+
"testing"
20+
21+
metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
22+
tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1"
23+
"github.com/census-instrumentation/opencensus-service/data"
24+
)
25+
26+
func TestSinkTraceExporter(t *testing.T) {
27+
sink := new(SinkTraceExporter)
28+
td := data.TraceData{
29+
Spans: make([]*tracepb.Span, 7),
30+
}
31+
want := make([]data.TraceData, 0, 7)
32+
for i := 0; i < 7; i++ {
33+
if err := sink.ProcessTraceData(context.Background(), td); err != nil {
34+
t.Errorf("Wanted nil got error")
35+
return
36+
}
37+
want = append(want, td)
38+
}
39+
got := sink.AllTraces()
40+
if !reflect.DeepEqual(got, want) {
41+
t.Errorf("Mismatches responses\nGot:\n\t%v\nWant:\n\t%v\n", got, want)
42+
}
43+
if "SinkExporter" != sink.ExportFormat() {
44+
t.Errorf("Wanted SinkExporter got %s", sink.ExportFormat())
45+
return
46+
}
47+
}
48+
49+
func TestSinkMetricsExporter(t *testing.T) {
50+
sink := new(SinkMetricsExporter)
51+
md := data.MetricsData{
52+
Metrics: make([]*metricspb.Metric, 7),
53+
}
54+
want := make([]data.MetricsData, 0, 7)
55+
for i := 0; i < 7; i++ {
56+
if err := sink.ProcessMetricsData(context.Background(), md); err != nil {
57+
t.Errorf("Wanted nil got error")
58+
return
59+
}
60+
want = append(want, md)
61+
}
62+
got := sink.AllMetrics()
63+
if !reflect.DeepEqual(got, want) {
64+
t.Errorf("Mismatches responses\nGot:\n\t%v\nWant:\n\t%v\n", got, want)
65+
}
66+
if "SinkExporter" != sink.ExportFormat() {
67+
t.Errorf("Wanted SinkExporter got %s", sink.ExportFormat())
68+
return
69+
}
70+
}

exporter/factory.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,48 @@ import (
2020
"github.com/census-instrumentation/opencensus-service/processor"
2121
)
2222

23-
// TraceDataExporter composes TraceDataProcessor with some additional
23+
// TraceExporter composes TraceDataProcessor with some additional
2424
// exporter-specific functions. This helps the service core to identify which
2525
// TraceDataProcessors are Exporters and which are internal processing
2626
// components, so that better validation of pipelines can be done.
27-
type TraceDataExporter interface {
27+
type TraceExporter interface {
2828
processor.TraceDataProcessor
2929

3030
// ExportFormat gets the name of the format in which this exporter sends its data.
3131
ExportFormat() string
3232
}
3333

34-
// TraceDataExporterFactory is an interface that builds a new TraceDataExporter based on
34+
// TraceExporterFactory is an interface that builds a new TraceExporter based on
3535
// some viper.Viper configuration.
36-
type TraceDataExporterFactory interface {
37-
// Type gets the type of the TraceDataExporter created by this factory.
36+
type TraceExporterFactory interface {
37+
// Type gets the type of the TraceExporter created by this factory.
3838
Type() string
39-
// NewFromViper takes a viper.Viper config and creates a new TraceDataExporter.
40-
NewFromViper(cfg *viper.Viper) (TraceDataExporter, error)
41-
// DefaultConfig returns the default configuration for TraceDataExporter
39+
// NewFromViper takes a viper.Viper config and creates a new TraceExporter.
40+
NewFromViper(cfg *viper.Viper) (TraceExporter, error)
41+
// DefaultConfig returns the default configuration for TraceExporter
4242
// created by this factory.
4343
DefaultConfig() *viper.Viper
4444
}
4545

46-
// MetricsDataExporter composes MetricsDataProcessor with some additional
46+
// MetricsExporter composes MetricsDataProcessor with some additional
4747
// exporter-specific functions. This helps the service core to identify which
4848
// MetricsDataProcessors are Exporters and which are internal processing
4949
// components, so that better validation of pipelines can be done.
50-
type MetricsDataExporter interface {
50+
type MetricsExporter interface {
5151
processor.MetricsDataProcessor
5252

5353
// ExportFormat gets the name of the format in which this exporter sends its data.
5454
ExportFormat() string
5555
}
5656

57-
// MetricsDataExporterFactory is an interface that builds a new MetricsDataExporter based on
57+
// MetricsExporterFactory is an interface that builds a new MetricsExporter based on
5858
// some viper.Viper configuration.
59-
type MetricsDataExporterFactory interface {
60-
// Type gets the type of the MetricsDataExporter created by this factory.
59+
type MetricsExporterFactory interface {
60+
// Type gets the type of the MetricsExporter created by this factory.
6161
Type() string
62-
// NewFromViper takes a viper.Viper config and creates a new MetricsDataExporter.
63-
NewFromViper(cfg *viper.Viper) (MetricsDataExporter, error)
64-
// DefaultConfig returns the default configuration for MetricsDataExporter
62+
// NewFromViper takes a viper.Viper config and creates a new MetricsExporter.
63+
NewFromViper(cfg *viper.Viper) (MetricsExporter, error)
64+
// DefaultConfig returns the default configuration for MetricsExporter
6565
// created by this factory.
6666
DefaultConfig() *viper.Viper
6767
}

exporter/loggingexporter/logging_exporter.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ const exportFormat = "LoggingExporter"
2727
// A logging exporter that does not sends the data to any destination but logs debugging messages.
2828
type loggingExporter struct{ logger *zap.Logger }
2929

30-
var _ exporter.TraceDataExporter = (*loggingExporter)(nil)
31-
var _ exporter.MetricsDataExporter = (*loggingExporter)(nil)
30+
var _ exporter.TraceExporter = (*loggingExporter)(nil)
31+
var _ exporter.MetricsExporter = (*loggingExporter)(nil)
3232

3333
func (sp *loggingExporter) ProcessTraceData(ctx context.Context, td data.TraceData) error {
3434
// TODO: Record metrics
3535
// TODO: Add ability to record the received data
36-
sp.logger.Debug("loggingTraceDataExporter", zap.Int("#spans", len(td.Spans)))
36+
sp.logger.Debug("loggingTraceExporter", zap.Int("#spans", len(td.Spans)))
3737
return nil
3838
}
3939

4040
func (sp *loggingExporter) ProcessMetricsData(ctx context.Context, md data.MetricsData) error {
41-
sp.logger.Debug("loggingMetricsDataExporter", zap.Int("#metrics", len(md.Metrics)))
41+
sp.logger.Debug("loggingMetricsExporter", zap.Int("#metrics", len(md.Metrics)))
4242
// TODO: Record metrics
4343
// TODO: Add ability to record the received data
4444
return nil
@@ -48,14 +48,14 @@ func (sp *loggingExporter) ExportFormat() string {
4848
return exportFormat
4949
}
5050

51-
// NewTraceExporter creates an exporter.TraceDataExporter that just drops the
51+
// NewTraceExporter creates an exporter.TraceExporter that just drops the
5252
// received data and logs debugging messages.
53-
func NewTraceExporter(logger *zap.Logger) exporter.TraceDataExporter {
53+
func NewTraceExporter(logger *zap.Logger) exporter.TraceExporter {
5454
return &loggingExporter{logger: logger}
5555
}
5656

57-
// NewMetricsExporter creates an exporter.MetricsDataExporter that just drops the
57+
// NewMetricsExporter creates an exporter.MetricsExporter that just drops the
5858
// received data and logs debugging messages.
59-
func NewMetricsExporter(logger *zap.Logger) exporter.MetricsDataExporter {
59+
func NewMetricsExporter(logger *zap.Logger) exporter.MetricsExporter {
6060
return &loggingExporter{logger: logger}
6161
}

0 commit comments

Comments
 (0)