2020-03-24 07:41:10 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-09-16 22:58:15 +02:00
|
|
|
//
|
|
|
|
// 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 trace_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2020-10-09 04:58:56 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
2019-11-01 20:40:29 +02:00
|
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
2019-09-16 22:58:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type testExporter struct {
|
2019-10-08 20:56:58 +02:00
|
|
|
spans []*export.SpanData
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
func (t *testExporter) ExportSpans(ctx context.Context, spans []*export.SpanData) error {
|
|
|
|
t.spans = append(t.spans, spans...)
|
|
|
|
return nil
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 19:19:03 +02:00
|
|
|
func (t *testExporter) Shutdown(context.Context) error { return nil }
|
|
|
|
|
|
|
|
var _ export.SpanExporter = (*testExporter)(nil)
|
2019-09-16 22:58:15 +02:00
|
|
|
|
|
|
|
func TestNewSimpleSpanProcessor(t *testing.T) {
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{})
|
|
|
|
if ssp == nil {
|
|
|
|
t.Errorf("Error creating new instance of SimpleSpanProcessor\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewSimpleSpanProcessorWithNilExporter(t *testing.T) {
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(nil)
|
|
|
|
if ssp == nil {
|
|
|
|
t.Errorf("Error creating new instance of SimpleSpanProcessor with nil Exporter\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimpleSpanProcessorOnEnd(t *testing.T) {
|
2020-09-24 00:16:13 +02:00
|
|
|
tp := basicTracerProvider(t)
|
2019-09-16 22:58:15 +02:00
|
|
|
te := testExporter{}
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&te)
|
|
|
|
if ssp == nil {
|
|
|
|
t.Errorf("Error creating new instance of SimpleSpanProcessor with nil Exporter\n")
|
|
|
|
}
|
2019-10-23 08:01:33 +02:00
|
|
|
|
2019-10-22 22:19:11 +02:00
|
|
|
tp.RegisterSpanProcessor(ssp)
|
2019-11-25 19:46:07 +02:00
|
|
|
tr := tp.Tracer("SimpleSpanProcessor")
|
2020-10-09 04:58:56 +02:00
|
|
|
tid, _ := otel.TraceIDFromHex("01020304050607080102040810203040")
|
|
|
|
sid, _ := otel.SpanIDFromHex("0102040810203040")
|
|
|
|
sc := otel.SpanContext{
|
2019-09-25 23:37:36 +02:00
|
|
|
TraceID: tid,
|
|
|
|
SpanID: sid,
|
|
|
|
TraceFlags: 0x1,
|
2019-09-16 22:58:15 +02:00
|
|
|
}
|
2020-10-09 04:58:56 +02:00
|
|
|
ctx := otel.ContextWithRemoteSpanContext(context.Background(), sc)
|
2020-02-04 18:55:03 +02:00
|
|
|
_, span := tr.Start(ctx, "OnEnd")
|
2019-09-27 19:48:10 +02:00
|
|
|
span.End()
|
2019-09-16 22:58:15 +02:00
|
|
|
|
|
|
|
wantTraceID := tid
|
|
|
|
gotTraceID := te.spans[0].SpanContext.TraceID
|
|
|
|
if wantTraceID != gotTraceID {
|
|
|
|
t.Errorf("SimplerSpanProcessor OnEnd() check: got %+v, want %+v\n", gotTraceID, wantTraceID)
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 19:34:19 +02:00
|
|
|
|
|
|
|
func TestSimpleSpanProcessorShutdown(t *testing.T) {
|
|
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{})
|
|
|
|
if ssp == nil {
|
|
|
|
t.Errorf("Error creating new instance of SimpleSpanProcessor\n")
|
2020-10-27 04:06:55 +02:00
|
|
|
return
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|
|
|
|
|
2020-10-27 04:06:55 +02:00
|
|
|
err := ssp.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error shutting the SimpleSpanProcessor down\n")
|
|
|
|
}
|
2019-10-08 19:34:19 +02:00
|
|
|
}
|