1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-14 10:13:10 +02:00
opentelemetry-go/internal/global/trace_test.go

245 lines
7.0 KiB
Go
Raw Normal View History

// Copyright The 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 global_test
import (
"context"
Fix synchronization issues in global trace delegate implementation (#1686) * Fix synchronization issues in global trace delegate implementation Running tests with `-race` and before the fix ``` ➜ opentelemetry-go git:(fixconcurrency) go test -race go.opentelemetry.io/otel/internal/global ================== WARNING: DATA RACE Write at 0x00c00010ffc8 by goroutine 28: go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0xf4 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0x76 go.opentelemetry.io/otel/internal/global.(*tracerProvider).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:75 +0x16f go.opentelemetry.io/otel/internal/global.SetTracerProvider.func1() /Users/lazy/github/opentelemetry-go/internal/global/state.go:65 +0xd0 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.SetTracerProvider() /Users/lazy/github/opentelemetry-go/internal/global/state.go:57 +0x79 go.opentelemetry.io/otel.SetTracerProvider() /Users/lazy/github/opentelemetry-go/trace.go:43 +0x388 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:177 +0x373 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Previous read at 0x00c00010ffc8 by goroutine 29: go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:124 +0x64 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 ================== ================== WARNING: DATA RACE Write at 0x00c00037a000 by goroutine 29: sync/atomic.StoreInt32() /usr/local/opt/go/libexec/src/runtime/race_amd64.s:242 +0xb go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func2.1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:183 +0x6d go.opentelemetry.io/otel/internal/global_test.fnTracer.Start() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:81 +0x98 go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:125 +0xfd go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Previous write at 0x00c00037a000 by goroutine 28: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:176 +0x2c4 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 ================== --- FAIL: TestTracerDelegates_MultiGoRoutines (0.20s) testing.go:1093: race detected during execution of test FAIL FAIL go.opentelemetry.io/otel/internal/global 0.920s FAIL ``` Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Make tests not flaky Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2021-03-10 18:45:31 +02:00
"sync/atomic"
"testing"
Fix synchronization issues in global trace delegate implementation (#1686) * Fix synchronization issues in global trace delegate implementation Running tests with `-race` and before the fix ``` ➜ opentelemetry-go git:(fixconcurrency) go test -race go.opentelemetry.io/otel/internal/global ================== WARNING: DATA RACE Write at 0x00c00010ffc8 by goroutine 28: go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0xf4 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0x76 go.opentelemetry.io/otel/internal/global.(*tracerProvider).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:75 +0x16f go.opentelemetry.io/otel/internal/global.SetTracerProvider.func1() /Users/lazy/github/opentelemetry-go/internal/global/state.go:65 +0xd0 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.SetTracerProvider() /Users/lazy/github/opentelemetry-go/internal/global/state.go:57 +0x79 go.opentelemetry.io/otel.SetTracerProvider() /Users/lazy/github/opentelemetry-go/trace.go:43 +0x388 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:177 +0x373 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Previous read at 0x00c00010ffc8 by goroutine 29: go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:124 +0x64 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 ================== ================== WARNING: DATA RACE Write at 0x00c00037a000 by goroutine 29: sync/atomic.StoreInt32() /usr/local/opt/go/libexec/src/runtime/race_amd64.s:242 +0xb go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func2.1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:183 +0x6d go.opentelemetry.io/otel/internal/global_test.fnTracer.Start() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:81 +0x98 go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:125 +0xfd go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Previous write at 0x00c00037a000 by goroutine 28: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:176 +0x2c4 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 ================== --- FAIL: TestTracerDelegates_MultiGoRoutines (0.20s) testing.go:1093: race detected during execution of test FAIL FAIL go.opentelemetry.io/otel/internal/global 0.920s FAIL ``` Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Make tests not flaky Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2021-03-10 18:45:31 +02:00
"time"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/trace"
)
type fnTracerProvider struct {
tracer func(string, ...trace.TracerOption) trace.Tracer
}
func (fn fnTracerProvider) Tracer(instrumentationName string, opts ...trace.TracerOption) trace.Tracer {
return fn.tracer(instrumentationName, opts...)
}
type fnTracer struct {
start func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
}
func (fn fnTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return fn.start(ctx, spanName, opts...)
}
func TestTraceProviderDelegation(t *testing.T) {
global.ResetForTest()
// Map of tracers to expected span names.
expected := map[string][]string{
"pre": {"span2"},
"post": {"span3"},
"fromSpan": {"span4"},
}
ctx := context.Background()
gtp := otel.GetTracerProvider()
tracer1 := gtp.Tracer("pre")
// This is started before an SDK was registered and should be dropped.
_, span1 := tracer1.Start(ctx, "span1")
otel.SetTracerProvider(fnTracerProvider{
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
spans, ok := expected[name]
assert.Truef(t, ok, "invalid tracer: %s", name)
return fnTracer{
start: func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
if ok {
if len(spans) == 0 {
t.Errorf("unexpected span: %s", spanName)
} else {
var want string
want, spans = spans[0], spans[1:]
assert.Equal(t, want, spanName)
}
}
return trace.NewNoopTracerProvider().Tracer(name).Start(ctx, spanName)
},
}
},
})
// This span was started before initialization, it is expected to be dropped.
span1.End()
// The existing Tracer should have been configured to now use the configured SDK.
_, span2 := tracer1.Start(ctx, "span2")
span2.End()
// The global TracerProvider should now create Tracers that also use the newly configured SDK.
tracer2 := gtp.Tracer("post")
_, span3 := tracer2.Start(ctx, "span3")
span3.End()
// The noop-span should still provide access to a usable TracerProvider.
_, span4 := span1.TracerProvider().Tracer("fromSpan").Start(ctx, "span4")
span4.End()
Fix synchronization issues in global trace delegate implementation (#1686) * Fix synchronization issues in global trace delegate implementation Running tests with `-race` and before the fix ``` ➜ opentelemetry-go git:(fixconcurrency) go test -race go.opentelemetry.io/otel/internal/global ================== WARNING: DATA RACE Write at 0x00c00010ffc8 by goroutine 28: go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0xf4 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0x76 go.opentelemetry.io/otel/internal/global.(*tracerProvider).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:75 +0x16f go.opentelemetry.io/otel/internal/global.SetTracerProvider.func1() /Users/lazy/github/opentelemetry-go/internal/global/state.go:65 +0xd0 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.SetTracerProvider() /Users/lazy/github/opentelemetry-go/internal/global/state.go:57 +0x79 go.opentelemetry.io/otel.SetTracerProvider() /Users/lazy/github/opentelemetry-go/trace.go:43 +0x388 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:177 +0x373 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Previous read at 0x00c00010ffc8 by goroutine 29: go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:124 +0x64 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 ================== ================== WARNING: DATA RACE Write at 0x00c00037a000 by goroutine 29: sync/atomic.StoreInt32() /usr/local/opt/go/libexec/src/runtime/race_amd64.s:242 +0xb go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func2.1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:183 +0x6d go.opentelemetry.io/otel/internal/global_test.fnTracer.Start() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:81 +0x98 go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:125 +0xfd go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Previous write at 0x00c00037a000 by goroutine 28: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:176 +0x2c4 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 ================== --- FAIL: TestTracerDelegates_MultiGoRoutines (0.20s) testing.go:1093: race detected during execution of test FAIL FAIL go.opentelemetry.io/otel/internal/global 0.920s FAIL ``` Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Make tests not flaky Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2021-03-10 18:45:31 +02:00
}
func TestTraceProviderDelegates(t *testing.T) {
global.ResetForTest()
// Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider()
// Configure it with a spy.
called := false
otel.SetTracerProvider(fnTracerProvider{
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
called = true
assert.Equal(t, "abc", name)
return trace.NewNoopTracerProvider().Tracer("")
},
})
gtp.Tracer("abc", trace.WithInstrumentationVersion("xyz"))
assert.True(t, called, "expected configured TraceProvider to be called")
}
Fix synchronization issues in global trace delegate implementation (#1686) * Fix synchronization issues in global trace delegate implementation Running tests with `-race` and before the fix ``` ➜ opentelemetry-go git:(fixconcurrency) go test -race go.opentelemetry.io/otel/internal/global ================== WARNING: DATA RACE Write at 0x00c00010ffc8 by goroutine 28: go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0xf4 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0x76 go.opentelemetry.io/otel/internal/global.(*tracerProvider).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:75 +0x16f go.opentelemetry.io/otel/internal/global.SetTracerProvider.func1() /Users/lazy/github/opentelemetry-go/internal/global/state.go:65 +0xd0 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.SetTracerProvider() /Users/lazy/github/opentelemetry-go/internal/global/state.go:57 +0x79 go.opentelemetry.io/otel.SetTracerProvider() /Users/lazy/github/opentelemetry-go/trace.go:43 +0x388 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:177 +0x373 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Previous read at 0x00c00010ffc8 by goroutine 29: go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:124 +0x64 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 ================== ================== WARNING: DATA RACE Write at 0x00c00037a000 by goroutine 29: sync/atomic.StoreInt32() /usr/local/opt/go/libexec/src/runtime/race_amd64.s:242 +0xb go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func2.1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:183 +0x6d go.opentelemetry.io/otel/internal/global_test.fnTracer.Start() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:81 +0x98 go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:125 +0xfd go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Previous write at 0x00c00037a000 by goroutine 28: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:176 +0x2c4 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 ================== --- FAIL: TestTracerDelegates_MultiGoRoutines (0.20s) testing.go:1093: race detected during execution of test FAIL FAIL go.opentelemetry.io/otel/internal/global 0.920s FAIL ``` Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Make tests not flaky Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2021-03-10 18:45:31 +02:00
func TestTraceProviderDelegatesConcurrentSafe(t *testing.T) {
global.ResetForTest()
// Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider()
done := make(chan struct{})
quit := make(chan struct{})
go func() {
defer close(done)
for {
select {
case <-time.After(1 * time.Millisecond):
gtp.Tracer("abc", trace.WithInstrumentationVersion("xyz"))
case <-quit:
return
}
}
}()
// Wait for the goroutine to make some calls before installing the provider.
<-time.After(100 * time.Millisecond)
// Configure it with a spy.
called := int32(0)
otel.SetTracerProvider(fnTracerProvider{
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
newVal := atomic.AddInt32(&called, 1)
assert.Equal(t, "abc", name)
if newVal == 10 {
// Signal the goroutine to finish.
close(quit)
}
return trace.NewNoopTracerProvider().Tracer("")
},
})
// Wait for the go routine to finish
<-done
assert.LessOrEqual(t, int32(10), atomic.LoadInt32(&called), "expected configured TraceProvider to be called")
}
func TestTracerDelegatesConcurrentSafe(t *testing.T) {
global.ResetForTest()
// Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider()
tracer := gtp.Tracer("abc", trace.WithInstrumentationVersion("xyz"))
done := make(chan struct{})
quit := make(chan struct{})
go func() {
defer close(done)
for {
select {
case <-time.After(1 * time.Millisecond):
tracer.Start(context.Background(), "name")
case <-quit:
return
}
}
}()
// Wait for the goroutine to make some calls before installing the provider.
<-time.After(100 * time.Millisecond)
// Configure it with a spy.
called := int32(0)
otel.SetTracerProvider(fnTracerProvider{
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
assert.Equal(t, "abc", name)
return fnTracer{
start: func(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
Fix synchronization issues in global trace delegate implementation (#1686) * Fix synchronization issues in global trace delegate implementation Running tests with `-race` and before the fix ``` ➜ opentelemetry-go git:(fixconcurrency) go test -race go.opentelemetry.io/otel/internal/global ================== WARNING: DATA RACE Write at 0x00c00010ffc8 by goroutine 28: go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0xf4 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.(*tracer).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:118 +0x76 go.opentelemetry.io/otel/internal/global.(*tracerProvider).setDelegate() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:75 +0x16f go.opentelemetry.io/otel/internal/global.SetTracerProvider.func1() /Users/lazy/github/opentelemetry-go/internal/global/state.go:65 +0xd0 sync.(*Once).doSlow() /usr/local/opt/go/libexec/src/sync/once.go:68 +0x109 sync.(*Once).Do() /usr/local/opt/go/libexec/src/sync/once.go:59 +0x68 go.opentelemetry.io/otel/internal/global.SetTracerProvider() /Users/lazy/github/opentelemetry-go/internal/global/state.go:57 +0x79 go.opentelemetry.io/otel.SetTracerProvider() /Users/lazy/github/opentelemetry-go/trace.go:43 +0x388 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:177 +0x373 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Previous read at 0x00c00010ffc8 by goroutine 29: go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:124 +0x64 go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 ================== ================== WARNING: DATA RACE Write at 0x00c00037a000 by goroutine 29: sync/atomic.StoreInt32() /usr/local/opt/go/libexec/src/runtime/race_amd64.s:242 +0xb go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func2.1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:183 +0x6d go.opentelemetry.io/otel/internal/global_test.fnTracer.Start() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:81 +0x98 go.opentelemetry.io/otel/internal/global.(*tracer).Start() /Users/lazy/github/opentelemetry-go/internal/global/trace.go:125 +0xfd go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines.func1() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:165 +0xf1 Previous write at 0x00c00037a000 by goroutine 28: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:176 +0x2c4 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 29 (running) created at: go.opentelemetry.io/otel/internal/global_test.TestTracerDelegates_MultiGoRoutines() /Users/lazy/github/opentelemetry-go/internal/global/trace_test.go:160 +0x259 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 Goroutine 28 (running) created at: testing.(*T).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1239 +0x5d7 testing.runTests.func1() /usr/local/opt/go/libexec/src/testing/testing.go:1512 +0xa6 testing.tRunner() /usr/local/opt/go/libexec/src/testing/testing.go:1194 +0x202 testing.runTests() /usr/local/opt/go/libexec/src/testing/testing.go:1510 +0x612 testing.(*M).Run() /usr/local/opt/go/libexec/src/testing/testing.go:1418 +0x3b3 go.opentelemetry.io/otel/internal/global_test.TestMain() /Users/lazy/github/opentelemetry-go/internal/global/internal_test.go:39 +0x4dc main.main() _testmain.go:85 +0x271 ================== --- FAIL: TestTracerDelegates_MultiGoRoutines (0.20s) testing.go:1093: race detected during execution of test FAIL FAIL go.opentelemetry.io/otel/internal/global 0.920s FAIL ``` Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update internal/global/trace_test.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Make tests not flaky Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2021-03-10 18:45:31 +02:00
newVal := atomic.AddInt32(&called, 1)
assert.Equal(t, "name", spanName)
if newVal == 10 {
// Signal the goroutine to finish.
close(quit)
}
return trace.NewNoopTracerProvider().Tracer("").Start(ctx, spanName)
},
}
},
})
// Wait for the go routine to finish
<-done
assert.LessOrEqual(t, int32(10), atomic.LoadInt32(&called), "expected configured TraceProvider to be called")
}
func TestTraceProviderDelegatesSameInstance(t *testing.T) {
global.ResetForTest()
// Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider()
tracer := gtp.Tracer("abc", trace.WithInstrumentationVersion("xyz"))
assert.Same(t, tracer, gtp.Tracer("abc", trace.WithInstrumentationVersion("xyz")))
assert.Same(t, tracer, gtp.Tracer("abc", trace.WithInstrumentationVersion("xyz")))
otel.SetTracerProvider(fnTracerProvider{
tracer: func(name string, opts ...trace.TracerOption) trace.Tracer {
return trace.NewNoopTracerProvider().Tracer("")
},
})
assert.NotSame(t, tracer, gtp.Tracer("abc", trace.WithInstrumentationVersion("xyz")))
}
func TestSpanContextPropagatedWithNonRecordingSpan(t *testing.T) {
global.ResetForTest()
sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{0x01},
SpanID: [8]byte{0x01},
TraceFlags: trace.FlagsSampled,
Remote: true,
})
ctx := trace.ContextWithSpanContext(context.Background(), sc)
_, span := otel.Tracer("test").Start(ctx, "test")
assert.Equal(t, sc, span.SpanContext())
assert.False(t, span.IsRecording())
}