1// Code created by gotmpl. DO NOT MODIFY.
2// source: internal/shared/otlp/otlptrace/otlptracetest/otlptest.go.tmpl
3
4// Copyright The OpenTelemetry Authors
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18package otlptracetest
19
20import (
21 "context"
22 "testing"
23 "time"
24
25 "go.opentelemetry.io/otel/attribute"
26 "go.opentelemetry.io/otel/exporters/otlp/otlptrace"
27 "go.opentelemetry.io/otel/sdk/resource"
28 sdktrace "go.opentelemetry.io/otel/sdk/trace"
29 commonpb "go.opentelemetry.io/proto/otlp/common/v1"
30)
31
32// RunEndToEndTest can be used by otlptrace.Client tests to validate
33// themselves.
34func RunEndToEndTest(ctx context.Context, t *testing.T, exp *otlptrace.Exporter, tracesCollector TracesCollector) {
35 pOpts := []sdktrace.TracerProviderOption{
36 sdktrace.WithSampler(sdktrace.AlwaysSample()),
37 sdktrace.WithBatcher(
38 exp,
39 // add following two options to ensure flush
40 sdktrace.WithBatchTimeout(5*time.Second),
41 sdktrace.WithMaxExportBatchSize(10),
42 ),
43 }
44 tp1 := sdktrace.NewTracerProvider(append(pOpts,
45 sdktrace.WithResource(resource.NewSchemaless(
46 attribute.String("rk1", "rv11)"),
47 attribute.Int64("rk2", 5),
48 )))...)
49
50 tp2 := sdktrace.NewTracerProvider(append(pOpts,
51 sdktrace.WithResource(resource.NewSchemaless(
52 attribute.String("rk1", "rv12)"),
53 attribute.Float64("rk3", 6.5),
54 )))...)
55
56 tr1 := tp1.Tracer("test-tracer1")
57 tr2 := tp2.Tracer("test-tracer2")
58 // Now create few spans
59 m := 4
60 for i := 0; i < m; i++ {
61 _, span := tr1.Start(ctx, "AlwaysSample")
62 span.SetAttributes(attribute.Int64("i", int64(i)))
63 span.End()
64
65 _, span = tr2.Start(ctx, "AlwaysSample")
66 span.SetAttributes(attribute.Int64("i", int64(i)))
67 span.End()
68 }
69
70 func() {
71 ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
72 defer cancel()
73 if err := tp1.Shutdown(ctx); err != nil {
74 t.Fatalf("failed to shut down a tracer provider 1: %v", err)
75 }
76 if err := tp2.Shutdown(ctx); err != nil {
77 t.Fatalf("failed to shut down a tracer provider 2: %v", err)
78 }
79 }()
80
81 // Wait >2 cycles.
82 <-time.After(40 * time.Millisecond)
83
84 // Now shutdown the exporter
85 ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
86 defer cancel()
87 if err := exp.Shutdown(ctx); err != nil {
88 t.Fatalf("failed to stop the exporter: %v", err)
89 }
90
91 // Shutdown the collector too so that we can begin
92 // verification checks of expected data back.
93 if err := tracesCollector.Stop(); err != nil {
94 t.Fatalf("failed to stop the mock collector: %v", err)
95 }
96
97 // Now verify that we only got two resources
98 rss := tracesCollector.GetResourceSpans()
99 if got, want := len(rss), 2; got != want {
100 t.Fatalf("resource span count: got %d, want %d\n", got, want)
101 }
102
103 // Now verify spans and attributes for each resource span.
104 for _, rs := range rss {
105 if len(rs.ScopeSpans) == 0 {
106 t.Fatalf("zero ScopeSpans")
107 }
108 if got, want := len(rs.ScopeSpans[0].Spans), m; got != want {
109 t.Fatalf("span counts: got %d, want %d", got, want)
110 }
111 attrMap := map[int64]bool{}
112 for _, s := range rs.ScopeSpans[0].Spans {
113 if gotName, want := s.Name, "AlwaysSample"; gotName != want {
114 t.Fatalf("span name: got %s, want %s", gotName, want)
115 }
116 attrMap[s.Attributes[0].Value.Value.(*commonpb.AnyValue_IntValue).IntValue] = true
117 }
118 if got, want := len(attrMap), m; got != want {
119 t.Fatalf("span attribute unique values: got %d want %d", got, want)
120 }
121 for i := 0; i < m; i++ {
122 _, ok := attrMap[int64(i)]
123 if !ok {
124 t.Fatalf("span with attribute %d missing", i)
125 }
126 }
127 }
128}
View as plain text