|
| 1 | +// Copyright 2019, OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package trace_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + apitrace "go.opentelemetry.io/api/trace" |
| 22 | + sdktrace "go.opentelemetry.io/sdk/trace" |
| 23 | +) |
| 24 | + |
| 25 | +type testSpanProcesor struct { |
| 26 | + spansStarted []*sdktrace.SpanData |
| 27 | + spansEnded []*sdktrace.SpanData |
| 28 | + shutdownCount int |
| 29 | +} |
| 30 | + |
| 31 | +func init() { |
| 32 | + sdktrace.Register() |
| 33 | + sdktrace.ApplyConfig(sdktrace.Config{DefaultSampler: sdktrace.AlwaysSample()}) |
| 34 | +} |
| 35 | + |
| 36 | +func (t *testSpanProcesor) OnStart(s *sdktrace.SpanData) { |
| 37 | + t.spansStarted = append(t.spansStarted, s) |
| 38 | +} |
| 39 | + |
| 40 | +func (t *testSpanProcesor) OnEnd(s *sdktrace.SpanData) { |
| 41 | + t.spansEnded = append(t.spansEnded, s) |
| 42 | +} |
| 43 | + |
| 44 | +func (t *testSpanProcesor) Shutdown() { |
| 45 | + t.shutdownCount++ |
| 46 | +} |
| 47 | + |
| 48 | +func TestRegisterSpanProcessort(t *testing.T) { |
| 49 | + name := "Register span processor before span starts" |
| 50 | + sp := NewTestSpanProcessor() |
| 51 | + sdktrace.RegisterSpanProcessor(sp) |
| 52 | + defer sdktrace.UnregisterSpanProcessor(sp) |
| 53 | + _, span := apitrace.GlobalTracer().Start(context.Background(), "OnStart") |
| 54 | + span.Finish() |
| 55 | + wantCount := 1 |
| 56 | + gotCount := len(sp.spansStarted) |
| 57 | + if gotCount != wantCount { |
| 58 | + t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount) |
| 59 | + } |
| 60 | + gotCount = len(sp.spansEnded) |
| 61 | + if gotCount != wantCount { |
| 62 | + t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestUnregisterSpanProcessor(t *testing.T) { |
| 67 | + name := "Start span after unregistering span processor" |
| 68 | + sp := NewTestSpanProcessor() |
| 69 | + sdktrace.RegisterSpanProcessor(sp) |
| 70 | + _, span := apitrace.GlobalTracer().Start(context.Background(), "OnStart") |
| 71 | + span.Finish() |
| 72 | + sdktrace.UnregisterSpanProcessor(sp) |
| 73 | + |
| 74 | + // start another span after unregistering span processor. |
| 75 | + _, span = apitrace.GlobalTracer().Start(context.Background(), "Start span after unregister") |
| 76 | + span.Finish() |
| 77 | + |
| 78 | + wantCount := 1 |
| 79 | + gotCount := len(sp.spansStarted) |
| 80 | + if gotCount != wantCount { |
| 81 | + t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount) |
| 82 | + } |
| 83 | + |
| 84 | + gotCount = len(sp.spansEnded) |
| 85 | + if gotCount != wantCount { |
| 86 | + t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestUnregisterSpanProcessorWhileSpanIsActive(t *testing.T) { |
| 91 | + name := "Unregister span processor while span is active" |
| 92 | + sp := NewTestSpanProcessor() |
| 93 | + sdktrace.RegisterSpanProcessor(sp) |
| 94 | + _, span := apitrace.GlobalTracer().Start(context.Background(), "OnStart") |
| 95 | + sdktrace.UnregisterSpanProcessor(sp) |
| 96 | + |
| 97 | + span.Finish() |
| 98 | + |
| 99 | + wantCount := 1 |
| 100 | + gotCount := len(sp.spansStarted) |
| 101 | + if gotCount != wantCount { |
| 102 | + t.Errorf("%s: started count: got %d, want %d\n", name, gotCount, wantCount) |
| 103 | + } |
| 104 | + |
| 105 | + wantCount = 0 |
| 106 | + gotCount = len(sp.spansEnded) |
| 107 | + if gotCount != wantCount { |
| 108 | + t.Errorf("%s: ended count: got %d, want %d\n", name, gotCount, wantCount) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// TODO(rghetia): Add Shutdown test when it is implemented. |
| 113 | +func TestShutdown(t *testing.T) { |
| 114 | +} |
| 115 | + |
| 116 | +func NewTestSpanProcessor() *testSpanProcesor { |
| 117 | + return &testSpanProcesor{} |
| 118 | +} |
0 commit comments