|
14 | 14 |
|
15 | 15 | package stackdriverexporter |
16 | 16 |
|
17 | | -// TODO: Add tests. |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "reflect" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "contrib.go.opencensus.io/exporter/stackdriver" |
| 24 | + commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1" |
| 25 | + metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1" |
| 26 | + resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1" |
| 27 | + tracepb "github.com/census-instrumentation/opencensus-proto/gen-go/trace/v1" |
| 28 | + "github.com/golang/protobuf/ptypes/timestamp" |
| 29 | + "github.com/spf13/viper" |
| 30 | + "go.opencensus.io/trace" |
| 31 | + |
| 32 | + "github.com/census-instrumentation/opencensus-service/data" |
| 33 | + "github.com/census-instrumentation/opencensus-service/exporter/exportertest" |
| 34 | + "github.com/census-instrumentation/opencensus-service/internal/config/viperutils" |
| 35 | +) |
| 36 | + |
| 37 | +type fakeStackdriverExporter struct { |
| 38 | + spanData []*trace.SpanData |
| 39 | +} |
| 40 | + |
| 41 | +var _ stackdriverExporterInterface = (*fakeStackdriverExporter)(nil) |
| 42 | + |
| 43 | +func (*fakeStackdriverExporter) ExportMetricsProto(ctx context.Context, node *commonpb.Node, rsc *resourcepb.Resource, metrics []*metricspb.Metric) error { |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (exp *fakeStackdriverExporter) ExportSpan(sd *trace.SpanData) { |
| 48 | + exp.spanData = append(exp.spanData, sd) |
| 49 | +} |
| 50 | + |
| 51 | +func (*fakeStackdriverExporter) Flush() { |
| 52 | +} |
| 53 | + |
| 54 | +func fakeStackdriverNewExporter(opts stackdriver.Options) (*stackdriver.Exporter, error) { |
| 55 | + return nil, nil |
| 56 | +} |
| 57 | + |
| 58 | +func TestStackriverExporter(t *testing.T) { |
| 59 | + tests := []struct { |
| 60 | + name string |
| 61 | + traceData data.TraceData |
| 62 | + want []*trace.SpanData |
| 63 | + }{ |
| 64 | + {name: "full span with node and library info, check that agentLabel gets set", |
| 65 | + traceData: data.TraceData{ |
| 66 | + Node: &commonpb.Node{ |
| 67 | + LibraryInfo: &commonpb.LibraryInfo{ |
| 68 | + Language: commonpb.LibraryInfo_PYTHON, |
| 69 | + ExporterVersion: "0.4.1", |
| 70 | + CoreLibraryVersion: "0.3.2", |
| 71 | + }, |
| 72 | + }, |
| 73 | + Spans: []*tracepb.Span{ |
| 74 | + { |
| 75 | + TraceId: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x80}, |
| 76 | + SpanId: []byte{0xAF, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9, 0xA8}, |
| 77 | + Name: &tracepb.TruncatableString{Value: "DBSearch"}, |
| 78 | + StartTime: ×tamp.Timestamp{Seconds: 1550000001, Nanos: 1}, |
| 79 | + EndTime: ×tamp.Timestamp{Seconds: 1550000002, Nanos: 1}, |
| 80 | + Attributes: &tracepb.Span_Attributes{ |
| 81 | + AttributeMap: map[string]*tracepb.AttributeValue{ |
| 82 | + "cache_hit": {Value: &tracepb.AttributeValue_BoolValue{BoolValue: true}}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + SourceFormat: "oc_trace", |
| 88 | + }, |
| 89 | + want: []*trace.SpanData{ |
| 90 | + { |
| 91 | + Name: "DBSearch", |
| 92 | + SpanContext: trace.SpanContext{ |
| 93 | + TraceID: trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 128}, |
| 94 | + SpanID: trace.SpanID{175, 174, 173, 172, 171, 170, 169, 168}, |
| 95 | + }, |
| 96 | + StartTime: time.Unix(1550000001, 1), |
| 97 | + EndTime: time.Unix(1550000002, 1), |
| 98 | + // Check that the agent label is correctly formed based on the library |
| 99 | + // info given in the Node structure above. |
| 100 | + Attributes: map[string]interface{}{ |
| 101 | + "cache_hit": true, |
| 102 | + agentLabel: "opencensus-python 0.3.2; ocagent-exporter 0.4.1", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + {name: "no node specified, so agentLabel not set", |
| 108 | + traceData: data.TraceData{ |
| 109 | + Spans: []*tracepb.Span{ |
| 110 | + { |
| 111 | + Attributes: &tracepb.Span_Attributes{ |
| 112 | + AttributeMap: map[string]*tracepb.AttributeValue{ |
| 113 | + "cache_hit": {Value: &tracepb.AttributeValue_BoolValue{BoolValue: true}}, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + want: []*trace.SpanData{{Attributes: map[string]interface{}{"cache_hit": true}}}, |
| 120 | + }, |
| 121 | + {name: "no library info specified, so agentLabel not set", |
| 122 | + traceData: data.TraceData{ |
| 123 | + Node: &commonpb.Node{ |
| 124 | + Attributes: map[string]string{"attr1": "val1"}, |
| 125 | + }, |
| 126 | + Spans: []*tracepb.Span{ |
| 127 | + { |
| 128 | + Attributes: &tracepb.Span_Attributes{ |
| 129 | + AttributeMap: map[string]*tracepb.AttributeValue{ |
| 130 | + "cache_hit": {Value: &tracepb.AttributeValue_BoolValue{BoolValue: true}}, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + want: []*trace.SpanData{{Attributes: map[string]interface{}{"cache_hit": true}}}, |
| 137 | + }, |
| 138 | + {name: "empty library info, so agentLabel gets empty default", |
| 139 | + traceData: data.TraceData{ |
| 140 | + Node: &commonpb.Node{ |
| 141 | + LibraryInfo: &commonpb.LibraryInfo{}, |
| 142 | + }, |
| 143 | + Spans: []*tracepb.Span{ |
| 144 | + { |
| 145 | + Attributes: &tracepb.Span_Attributes{ |
| 146 | + AttributeMap: map[string]*tracepb.AttributeValue{ |
| 147 | + "cache_hit": {Value: &tracepb.AttributeValue_BoolValue{BoolValue: true}}, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + want: []*trace.SpanData{{Attributes: map[string]interface{}{ |
| 154 | + "cache_hit": true, |
| 155 | + agentLabel: "opencensus-language_unspecified ; ocagent-exporter ", |
| 156 | + }}}, |
| 157 | + }, |
| 158 | + {name: "no attributes set, still assigns agentLabel", |
| 159 | + traceData: data.TraceData{ |
| 160 | + Node: &commonpb.Node{ |
| 161 | + LibraryInfo: &commonpb.LibraryInfo{ |
| 162 | + Language: commonpb.LibraryInfo_WEB_JS, |
| 163 | + CoreLibraryVersion: "0.0.2", |
| 164 | + ExporterVersion: "0.0.3", |
| 165 | + }, |
| 166 | + }, |
| 167 | + Spans: []*tracepb.Span{{}}, |
| 168 | + }, |
| 169 | + want: []*trace.SpanData{{Attributes: map[string]interface{}{ |
| 170 | + agentLabel: "opencensus-web_js 0.0.2; ocagent-exporter 0.0.3", |
| 171 | + }}}, |
| 172 | + }, |
| 173 | + {name: "no attributes map set, still assigns agentLabel", |
| 174 | + traceData: data.TraceData{ |
| 175 | + Node: &commonpb.Node{ |
| 176 | + LibraryInfo: &commonpb.LibraryInfo{ |
| 177 | + Language: commonpb.LibraryInfo_WEB_JS, |
| 178 | + CoreLibraryVersion: "0.0.2", |
| 179 | + ExporterVersion: "0.0.3", |
| 180 | + }, |
| 181 | + }, |
| 182 | + Spans: []*tracepb.Span{{Attributes: &tracepb.Span_Attributes{}}}, |
| 183 | + }, |
| 184 | + want: []*trace.SpanData{{Attributes: map[string]interface{}{ |
| 185 | + agentLabel: "opencensus-web_js 0.0.2; ocagent-exporter 0.0.3", |
| 186 | + }}}, |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + for _, tt := range tests { |
| 191 | + v := viper.New() |
| 192 | + configYAML := []byte(` |
| 193 | +stackdriver: |
| 194 | + project: 'test-project' |
| 195 | + enable_tracing: true |
| 196 | + enable_metrics: true |
| 197 | + metric_prefix: 'test-metric-prefix'`) |
| 198 | + err := viperutils.LoadYAMLBytes(v, configYAML) |
| 199 | + exp := fakeStackdriverExporter{} |
| 200 | + tps, mps, doneFns, err := stackdriverTraceExportersFromViperInternal(v, func(opts stackdriver.Options) (stackdriverExporterInterface, error) { |
| 201 | + if opts.ProjectID != "test-project" { |
| 202 | + t.Errorf("Unexpected ProjectID: %v", opts.ProjectID) |
| 203 | + } |
| 204 | + if opts.MetricPrefix != "test-metric-prefix" { |
| 205 | + t.Errorf("Unexpected MetricPrefix: %v", opts.MetricPrefix) |
| 206 | + } |
| 207 | + return &exp, nil |
| 208 | + }) |
| 209 | + if len(tps) != 1 { |
| 210 | + t.Errorf("Unexpected TraceConsumer count: %v", len(tps)) |
| 211 | + } |
| 212 | + if len(mps) != 1 { |
| 213 | + t.Errorf("Unexpected MetricsConsumer count: %v", len(mps)) |
| 214 | + } |
| 215 | + if len(doneFns) != 1 { |
| 216 | + t.Errorf("Unexpected doneFns count: %v", len(doneFns)) |
| 217 | + } |
| 218 | + if err != nil { |
| 219 | + t.Errorf("Expected nil erorr, got %v", err) |
| 220 | + } |
| 221 | + |
| 222 | + tps[0].ConsumeTraceData(context.Background(), tt.traceData) |
| 223 | + |
| 224 | + got := exp.spanData |
| 225 | + if !reflect.DeepEqual(got, tt.want) { |
| 226 | + gj, wj := exportertest.ToJSON(got), exportertest.ToJSON(tt.want) |
| 227 | + t.Errorf("Test %s: Incorrect exported SpanData\nGot:\n\t%s\nWant:\n\t%s", tt.name, gj, wj) |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments