2019-09-28 20:27:02 +02:00
|
|
|
// Copyright 2019, OpenTelemetry 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 stdout
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-10-08 20:56:58 +02:00
|
|
|
"context"
|
2019-09-28 20:27:02 +02:00
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/api/core"
|
2019-10-31 00:01:19 +02:00
|
|
|
"go.opentelemetry.io/api/key"
|
2019-10-24 01:25:14 +02:00
|
|
|
"go.opentelemetry.io/api/trace"
|
2019-10-08 20:56:58 +02:00
|
|
|
"go.opentelemetry.io/sdk/export"
|
2019-09-28 20:27:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExporter_ExportSpan(t *testing.T) {
|
2019-10-08 20:56:58 +02:00
|
|
|
ex, err := NewExporter(Options{})
|
2019-09-28 20:27:02 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error constructing stdout exporter %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// override output writer for testing
|
|
|
|
var b bytes.Buffer
|
2019-10-08 20:56:58 +02:00
|
|
|
ex.outputWriter = &b
|
2019-09-28 20:27:02 +02:00
|
|
|
|
|
|
|
// setup test span
|
|
|
|
now := time.Now()
|
2019-10-23 08:01:33 +02:00
|
|
|
traceID, _ := core.TraceIDFromHex("0102030405060708090a0b0c0d0e0f10")
|
2019-10-28 19:05:06 +02:00
|
|
|
spanID, _ := core.SpanIDFromHex("0102030405060708")
|
2019-09-28 20:27:02 +02:00
|
|
|
keyValue := "value"
|
|
|
|
doubleValue := float64(123.456)
|
|
|
|
|
2019-10-08 20:56:58 +02:00
|
|
|
testSpan := &export.SpanData{
|
2019-09-28 20:27:02 +02:00
|
|
|
SpanContext: core.SpanContext{
|
|
|
|
TraceID: traceID,
|
|
|
|
SpanID: spanID,
|
|
|
|
},
|
|
|
|
Name: "/foo",
|
|
|
|
StartTime: now,
|
|
|
|
EndTime: now,
|
|
|
|
Attributes: []core.KeyValue{
|
2019-10-31 00:01:19 +02:00
|
|
|
key.String("key", keyValue),
|
|
|
|
key.Float64("double", doubleValue),
|
2019-09-28 20:27:02 +02:00
|
|
|
},
|
2019-10-24 01:25:14 +02:00
|
|
|
SpanKind: trace.SpanKindInternal,
|
|
|
|
Status: codes.Unknown,
|
2019-09-28 20:27:02 +02:00
|
|
|
}
|
2019-10-08 20:56:58 +02:00
|
|
|
ex.ExportSpan(context.Background(), testSpan)
|
2019-09-28 20:27:02 +02:00
|
|
|
|
|
|
|
expectedSerializedNow, _ := json.Marshal(now)
|
|
|
|
|
|
|
|
got := b.String()
|
|
|
|
expectedOutput := `{"SpanContext":{` +
|
2019-10-23 08:01:33 +02:00
|
|
|
`"TraceID":"0102030405060708090a0b0c0d0e0f10",` +
|
|
|
|
`"SpanID":"0102030405060708","TraceFlags":0},` +
|
2019-10-28 19:05:06 +02:00
|
|
|
`"ParentSpanID":"0000000000000000",` +
|
2019-10-24 01:25:14 +02:00
|
|
|
`"SpanKind":"internal",` +
|
2019-09-28 20:27:02 +02:00
|
|
|
`"Name":"/foo",` +
|
|
|
|
`"StartTime":` + string(expectedSerializedNow) + "," +
|
|
|
|
`"EndTime":` + string(expectedSerializedNow) + "," +
|
|
|
|
`"Attributes":[` +
|
|
|
|
`{` +
|
2019-10-17 07:49:58 +02:00
|
|
|
`"Key":"key",` +
|
2019-10-31 00:01:19 +02:00
|
|
|
`"Value":{"Type":"STRING","Value":"value"}` +
|
2019-09-28 20:27:02 +02:00
|
|
|
`},` +
|
|
|
|
`{` +
|
2019-10-17 07:49:58 +02:00
|
|
|
`"Key":"double",` +
|
2019-10-31 00:01:19 +02:00
|
|
|
`"Value":{"Type":"FLOAT64","Value":123.456}` +
|
2019-09-28 20:27:02 +02:00
|
|
|
`}` +
|
|
|
|
`],` +
|
|
|
|
`"MessageEvents":null,` +
|
|
|
|
`"Links":null,` +
|
|
|
|
`"Status":2,` +
|
|
|
|
`"HasRemoteParent":false,` +
|
|
|
|
`"DroppedAttributeCount":0,` +
|
|
|
|
`"DroppedMessageEventCount":0,` +
|
|
|
|
`"DroppedLinkCount":0,` +
|
|
|
|
`"ChildSpanCount":0}` + "\n"
|
|
|
|
|
|
|
|
if got != expectedOutput {
|
|
|
|
t.Errorf("Want: %v but got: %v", expectedOutput, got)
|
|
|
|
}
|
|
|
|
}
|