2019-09-09 23:59:39 +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 jaeger
|
|
|
|
|
|
|
|
import (
|
2019-10-31 19:52:46 +02:00
|
|
|
"context"
|
2019-10-23 08:01:33 +02:00
|
|
|
"encoding/binary"
|
2019-09-09 23:59:39 +02:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-11-26 06:41:24 +02:00
|
|
|
"go.opentelemetry.io/otel/api/global"
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/key"
|
|
|
|
|
|
|
|
apitrace "go.opentelemetry.io/otel/api/trace"
|
2019-10-17 18:59:30 +02:00
|
|
|
|
2019-09-09 23:59:39 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-10-31 19:52:46 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-09-09 23:59:39 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-10-31 19:52:46 +02:00
|
|
|
|
2019-11-01 20:40:29 +02:00
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
|
|
gen "go.opentelemetry.io/otel/exporter/trace/jaeger/internal/gen-go/jaeger"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2019-09-09 23:59:39 +02:00
|
|
|
)
|
|
|
|
|
2019-10-31 19:52:46 +02:00
|
|
|
func TestNewExporter(t *testing.T) {
|
|
|
|
const (
|
|
|
|
collectorEndpoint = "http://localhost"
|
|
|
|
serviceName = "test-service"
|
|
|
|
tagKey = "key"
|
|
|
|
tagVal = "val"
|
|
|
|
)
|
|
|
|
// Create Jaeger Exporter
|
|
|
|
exp, err := NewExporter(
|
|
|
|
WithCollectorEndpoint(collectorEndpoint),
|
|
|
|
WithProcess(Process{
|
|
|
|
ServiceName: serviceName,
|
|
|
|
Tags: []core.KeyValue{
|
|
|
|
key.String(tagKey, tagVal),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, serviceName, exp.process.ServiceName)
|
|
|
|
assert.Len(t, exp.process.Tags, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewExporterShouldFailIfCollectorEndpointEmpty(t *testing.T) {
|
|
|
|
_, err := NewExporter(
|
|
|
|
WithCollectorEndpoint(""),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
type testCollectorEnpoint struct {
|
|
|
|
spansUploaded []*gen.Span
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testCollectorEnpoint) upload(batch *gen.Batch) error {
|
|
|
|
c.spansUploaded = append(c.spansUploaded, batch.Spans...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ batchUploader = (*testCollectorEnpoint)(nil)
|
|
|
|
|
|
|
|
func withTestCollectorEndpoint() func() (batchUploader, error) {
|
|
|
|
return func() (batchUploader, error) {
|
|
|
|
return &testCollectorEnpoint{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExporter_ExportSpan(t *testing.T) {
|
|
|
|
const (
|
|
|
|
serviceName = "test-service"
|
|
|
|
tagKey = "key"
|
|
|
|
tagVal = "val"
|
|
|
|
)
|
|
|
|
// Create Jaeger Exporter
|
|
|
|
exp, err := NewExporter(
|
|
|
|
withTestCollectorEndpoint(),
|
|
|
|
WithProcess(Process{
|
|
|
|
ServiceName: serviceName,
|
|
|
|
Tags: []core.KeyValue{
|
|
|
|
key.String(tagKey, tagVal),
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
tp, err := sdktrace.NewProvider(
|
|
|
|
sdktrace.WithConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}),
|
|
|
|
sdktrace.WithSyncer(exp))
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
global.SetTraceProvider(tp)
|
2019-11-25 19:46:07 +02:00
|
|
|
_, span := global.TraceProvider().Tracer("test-tracer").Start(context.Background(), "test-span")
|
2019-10-31 19:52:46 +02:00
|
|
|
span.End()
|
|
|
|
|
|
|
|
assert.True(t, span.SpanContext().IsValid())
|
|
|
|
|
|
|
|
exp.Flush()
|
|
|
|
tc := exp.uploader.(*testCollectorEnpoint)
|
|
|
|
assert.True(t, len(tc.spansUploaded) == 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewExporterWithAgentEndpoint(t *testing.T) {
|
|
|
|
const agentEndpoint = "localhost:6831"
|
|
|
|
// Create Jaeger Exporter
|
|
|
|
_, err := NewExporter(
|
|
|
|
WithAgentEndpoint(agentEndpoint),
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewExporterWithAgentShouldFailIfEndpointInvalid(t *testing.T) {
|
|
|
|
//empty
|
|
|
|
_, err := NewExporter(
|
|
|
|
WithAgentEndpoint(""),
|
|
|
|
)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
//invalid endpoint addr
|
|
|
|
_, err = NewExporter(
|
|
|
|
WithAgentEndpoint("http://localhost"),
|
|
|
|
)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2019-09-09 23:59:39 +02:00
|
|
|
|
|
|
|
func Test_spanDataToThrift(t *testing.T) {
|
|
|
|
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-09 23:59:39 +02:00
|
|
|
|
2019-10-23 08:01:33 +02:00
|
|
|
linkTraceID, _ := core.TraceIDFromHex("0102030405060709090a0b0c0d0e0f11")
|
2019-10-28 19:05:06 +02:00
|
|
|
linkSpanID, _ := core.SpanIDFromHex("0102030405060709")
|
2019-10-07 18:40:36 +02:00
|
|
|
|
2019-10-31 19:52:46 +02:00
|
|
|
messageEventValue := "event-test"
|
2019-09-09 23:59:39 +02:00
|
|
|
keyValue := "value"
|
|
|
|
statusCodeValue := int64(2)
|
|
|
|
doubleValue := float64(123.456)
|
|
|
|
boolTrue := true
|
|
|
|
statusMessage := "Unknown"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-10-08 20:56:58 +02:00
|
|
|
data *export.SpanData
|
2019-09-09 23:59:39 +02:00
|
|
|
want *gen.Span
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no parent",
|
2019-10-08 20:56:58 +02:00
|
|
|
data: &export.SpanData{
|
2019-09-09 23:59:39 +02:00
|
|
|
SpanContext: core.SpanContext{
|
|
|
|
TraceID: traceID,
|
|
|
|
SpanID: spanID,
|
|
|
|
},
|
|
|
|
Name: "/foo",
|
|
|
|
StartTime: now,
|
|
|
|
EndTime: now,
|
2019-10-07 18:40:36 +02:00
|
|
|
Links: []apitrace.Link{
|
2019-10-16 19:24:38 +02:00
|
|
|
{
|
2019-10-07 18:40:36 +02:00
|
|
|
SpanContext: core.SpanContext{
|
|
|
|
TraceID: linkTraceID,
|
|
|
|
SpanID: linkSpanID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-09-25 22:22:33 +02:00
|
|
|
Attributes: []core.KeyValue{
|
2019-10-17 18:59:30 +02:00
|
|
|
key.String("key", keyValue),
|
|
|
|
key.Float64("double", doubleValue),
|
|
|
|
// Jaeger doesn't handle Uint tags, this should be ignored.
|
|
|
|
key.Uint64("ignored", 123),
|
2019-09-09 23:59:39 +02:00
|
|
|
},
|
2019-10-31 19:52:46 +02:00
|
|
|
MessageEvents: []export.Event{
|
|
|
|
{Message: messageEventValue, Attributes: []core.KeyValue{key.String("k1", keyValue)}, Time: now},
|
|
|
|
},
|
2019-09-09 23:59:39 +02:00
|
|
|
Status: codes.Unknown,
|
|
|
|
},
|
|
|
|
want: &gen.Span{
|
|
|
|
TraceIdLow: 651345242494996240,
|
|
|
|
TraceIdHigh: 72623859790382856,
|
|
|
|
SpanId: 72623859790382856,
|
|
|
|
OperationName: "/foo",
|
|
|
|
StartTime: now.UnixNano() / 1000,
|
|
|
|
Duration: 0,
|
|
|
|
Tags: []*gen.Tag{
|
|
|
|
{Key: "double", VType: gen.TagType_DOUBLE, VDouble: &doubleValue},
|
|
|
|
{Key: "key", VType: gen.TagType_STRING, VStr: &keyValue},
|
|
|
|
{Key: "error", VType: gen.TagType_BOOL, VBool: &boolTrue},
|
|
|
|
{Key: "status.code", VType: gen.TagType_LONG, VLong: &statusCodeValue},
|
|
|
|
{Key: "status.message", VType: gen.TagType_STRING, VStr: &statusMessage},
|
|
|
|
},
|
2019-10-07 18:40:36 +02:00
|
|
|
References: []*gen.SpanRef{
|
2019-10-16 19:24:38 +02:00
|
|
|
{
|
2019-10-07 18:40:36 +02:00
|
|
|
RefType: gen.SpanRefType_CHILD_OF,
|
2019-10-23 08:01:33 +02:00
|
|
|
TraceIdHigh: int64(binary.BigEndian.Uint64(linkTraceID[0:8])),
|
|
|
|
TraceIdLow: int64(binary.BigEndian.Uint64(linkTraceID[8:16])),
|
2019-10-28 19:05:06 +02:00
|
|
|
SpanId: int64(binary.BigEndian.Uint64(linkSpanID[:])),
|
2019-10-07 18:40:36 +02:00
|
|
|
},
|
|
|
|
},
|
2019-10-31 19:52:46 +02:00
|
|
|
Logs: []*gen.Log{
|
|
|
|
{
|
|
|
|
Timestamp: now.UnixNano() / 1000,
|
|
|
|
Fields: []*gen.Tag{
|
|
|
|
{
|
|
|
|
Key: "k1",
|
|
|
|
VStr: &keyValue,
|
|
|
|
VType: gen.TagType_STRING,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "message",
|
|
|
|
VStr: &messageEventValue,
|
|
|
|
VType: gen.TagType_STRING,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-09-09 23:59:39 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := spanDataToThrift(tt.data)
|
|
|
|
sort.Slice(got.Tags, func(i, j int) bool {
|
|
|
|
return got.Tags[i].Key < got.Tags[j].Key
|
|
|
|
})
|
|
|
|
sort.Slice(tt.want.Tags, func(i, j int) bool {
|
|
|
|
return tt.want.Tags[i].Key < tt.want.Tags[j].Key
|
|
|
|
})
|
|
|
|
if diff := cmp.Diff(got, tt.want); diff != "" {
|
|
|
|
t.Errorf("Diff%v", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|