1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package client
import (
"context"
"net/http"
"testing"
"github.com/go-openapi/runtime"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.opentelemetry.io/otel/trace"
)
func Test_OpenTelemetryRuntime_submit(t *testing.T) {
t.Parallel()
exporter := tracetest.NewInMemoryExporter()
tp := tracesdk.NewTracerProvider(
tracesdk.WithSampler(tracesdk.AlwaysSample()),
tracesdk.WithSyncer(exporter),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("go-runtime")
ctx, span := tracer.Start(context.Background(), "op")
defer span.End()
assertOpenTelemetrySubmit(t, testOperation(ctx), exporter, 1)
}
func Test_OpenTelemetryRuntime_submit_nilAuthInfo(t *testing.T) {
t.Parallel()
exporter := tracetest.NewInMemoryExporter()
tp := tracesdk.NewTracerProvider(
tracesdk.WithSampler(tracesdk.AlwaysSample()),
tracesdk.WithSyncer(exporter),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("go-runtime")
ctx, span := tracer.Start(context.Background(), "op")
defer span.End()
operation := testOperation(ctx)
operation.AuthInfo = nil
assertOpenTelemetrySubmit(t, operation, exporter, 1)
}
func Test_OpenTelemetryRuntime_submit_nilContext(t *testing.T) {
exporter := tracetest.NewInMemoryExporter()
tp := tracesdk.NewTracerProvider(
tracesdk.WithSampler(tracesdk.AlwaysSample()),
tracesdk.WithSyncer(exporter),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("go-runtime")
ctx, span := tracer.Start(context.Background(), "op")
defer span.End()
operation := testOperation(ctx)
operation.Context = nil
assertOpenTelemetrySubmit(t, operation, exporter, 0) // just don't panic
}
func Test_injectOpenTelemetrySpanContext(t *testing.T) {
t.Parallel()
exporter := tracetest.NewInMemoryExporter()
tp := tracesdk.NewTracerProvider(
tracesdk.WithSampler(tracesdk.AlwaysSample()),
tracesdk.WithSyncer(exporter),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer("go-runtime")
ctx, span := tracer.Start(context.Background(), "op")
defer span.End()
operation := testOperation(ctx)
header := map[string][]string{}
tr := newOpenTelemetryTransport(&mockRuntime{runtime.TestClientRequest{Headers: header}}, "", nil)
tr.config.Propagator = propagation.TraceContext{}
_, err := tr.Submit(operation)
require.NoError(t, err)
assert.Len(t, header, 1)
}
func assertOpenTelemetrySubmit(t *testing.T, operation *runtime.ClientOperation, exporter *tracetest.InMemoryExporter, expectedSpanCount int) {
header := map[string][]string{}
tr := newOpenTelemetryTransport(&mockRuntime{runtime.TestClientRequest{Headers: header}}, "remote_host", nil)
_, err := tr.Submit(operation)
require.NoError(t, err)
spans := exporter.GetSpans()
assert.Len(t, spans, expectedSpanCount)
if expectedSpanCount != 1 {
return
}
span := spans[0]
assert.Equal(t, "getCluster", span.Name)
assert.Equal(t, "go-openapi", span.InstrumentationLibrary.Name)
assert.Equal(t, codes.Error, span.Status.Code)
assert.Equal(t, []attribute.KeyValue{
attribute.String("net.peer.name", "remote_host"),
attribute.String("http.route", "/kubernetes-clusters/{cluster_id}"),
attribute.String("http.method", http.MethodGet),
attribute.String("span.kind", trace.SpanKindClient.String()),
attribute.String("http.scheme", schemeHTTPS),
// NOTE: this becomes http.response.status_code with semconv v1.21
attribute.Int("http.status_code", 490),
}, span.Attributes)
}
|