mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-12 10:04:29 +02:00
942713a02d
This PR removes the non-compliant ChildOf and FollowsFrom interfaces and the Relation type, which were inherited from OpenTracing via the initial prototype. Instead allow adding a span context to the go context as a remote span context and use a simple algorithm for figuring out an actual parent of the new span, which was proposed for the OpenTelemetry specification. Also add a way to ignore current span and remote span context in go context, so we can force the tracer to create a new root span - a span with a new trace ID. That required some moderate changes in the opentracing bridge - first reference with ChildOfRef reference type becomes a local parent, the rest become links. This also fixes links handling in the meantime. The downside of the approach proposed here is that we can only set the remote parent when creating a span through the opentracing API. Co-authored-by: Joshua MacDonald <jmacd@users.noreply.github.com>
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
// 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 trace_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
apitrace "go.opentelemetry.io/otel/api/trace"
|
|
export "go.opentelemetry.io/otel/sdk/export/trace"
|
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
|
)
|
|
|
|
type testExporter struct {
|
|
spans []*export.SpanData
|
|
}
|
|
|
|
func (t *testExporter) ExportSpan(ctx context.Context, s *export.SpanData) {
|
|
t.spans = append(t.spans, s)
|
|
}
|
|
|
|
var _ export.SpanSyncer = (*testExporter)(nil)
|
|
|
|
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) {
|
|
tp := basicProvider(t)
|
|
te := testExporter{}
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&te)
|
|
if ssp == nil {
|
|
t.Errorf("Error creating new instance of SimpleSpanProcessor with nil Exporter\n")
|
|
}
|
|
|
|
tp.RegisterSpanProcessor(ssp)
|
|
tr := tp.Tracer("SimpleSpanProcessor")
|
|
tid, _ := core.TraceIDFromHex("01020304050607080102040810203040")
|
|
sid, _ := core.SpanIDFromHex("0102040810203040")
|
|
sc := core.SpanContext{
|
|
TraceID: tid,
|
|
SpanID: sid,
|
|
TraceFlags: 0x1,
|
|
}
|
|
ctx := apitrace.ContextWithRemoteSpanContext(context.Background(), sc)
|
|
_, span := tr.Start(ctx, "OnEnd")
|
|
span.End()
|
|
|
|
wantTraceID := tid
|
|
gotTraceID := te.spans[0].SpanContext.TraceID
|
|
if wantTraceID != gotTraceID {
|
|
t.Errorf("SimplerSpanProcessor OnEnd() check: got %+v, want %+v\n", gotTraceID, wantTraceID)
|
|
}
|
|
}
|
|
|
|
func TestSimpleSpanProcessorShutdown(t *testing.T) {
|
|
ssp := sdktrace.NewSimpleSpanProcessor(&testExporter{})
|
|
if ssp == nil {
|
|
t.Errorf("Error creating new instance of SimpleSpanProcessor\n")
|
|
}
|
|
|
|
ssp.Shutdown()
|
|
}
|