prometheus
135 строк · 3.8 Кб
1// Copyright 2021 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package tracing
15
16import (
17"testing"
18
19"github.com/go-kit/log"
20config_util "github.com/prometheus/common/config"
21"github.com/stretchr/testify/require"
22"go.opentelemetry.io/otel"
23"go.opentelemetry.io/otel/trace/noop"
24
25"github.com/prometheus/prometheus/config"
26)
27
28func TestInstallingNewTracerProvider(t *testing.T) {
29tpBefore := otel.GetTracerProvider()
30
31m := NewManager(log.NewNopLogger())
32cfg := config.Config{
33TracingConfig: config.TracingConfig{
34Endpoint: "localhost:1234",
35ClientType: config.TracingClientGRPC,
36},
37}
38
39require.NoError(t, m.ApplyConfig(&cfg))
40require.NotEqual(t, tpBefore, otel.GetTracerProvider())
41}
42
43func TestReinstallingTracerProvider(t *testing.T) {
44m := NewManager(log.NewNopLogger())
45cfg := config.Config{
46TracingConfig: config.TracingConfig{
47Endpoint: "localhost:1234",
48ClientType: config.TracingClientGRPC,
49Headers: map[string]string{"foo": "bar"},
50},
51}
52
53require.NoError(t, m.ApplyConfig(&cfg))
54tpFirstConfig := otel.GetTracerProvider()
55
56// Trying to apply the same config should not reinstall provider.
57require.NoError(t, m.ApplyConfig(&cfg))
58require.Equal(t, tpFirstConfig, otel.GetTracerProvider())
59
60cfg2 := config.Config{
61TracingConfig: config.TracingConfig{
62Endpoint: "localhost:1234",
63ClientType: config.TracingClientHTTP,
64Headers: map[string]string{"bar": "foo"},
65},
66}
67
68require.NoError(t, m.ApplyConfig(&cfg2))
69require.NotEqual(t, tpFirstConfig, otel.GetTracerProvider())
70tpSecondConfig := otel.GetTracerProvider()
71
72// Setting previously unset option should reinstall provider.
73cfg2.TracingConfig.Compression = "gzip"
74require.NoError(t, m.ApplyConfig(&cfg2))
75require.NotEqual(t, tpSecondConfig, otel.GetTracerProvider())
76}
77
78func TestReinstallingTracerProviderWithTLS(t *testing.T) {
79m := NewManager(log.NewNopLogger())
80cfg := config.Config{
81TracingConfig: config.TracingConfig{
82Endpoint: "localhost:1234",
83ClientType: config.TracingClientGRPC,
84TLSConfig: config_util.TLSConfig{
85CAFile: "testdata/ca.cer",
86},
87},
88}
89
90require.NoError(t, m.ApplyConfig(&cfg))
91tpFirstConfig := otel.GetTracerProvider()
92
93// Trying to apply the same config with TLS should reinstall provider.
94require.NoError(t, m.ApplyConfig(&cfg))
95require.NotEqual(t, tpFirstConfig, otel.GetTracerProvider())
96}
97
98func TestUninstallingTracerProvider(t *testing.T) {
99m := NewManager(log.NewNopLogger())
100cfg := config.Config{
101TracingConfig: config.TracingConfig{
102Endpoint: "localhost:1234",
103ClientType: config.TracingClientGRPC,
104},
105}
106
107require.NoError(t, m.ApplyConfig(&cfg))
108require.NotEqual(t, noop.NewTracerProvider(), otel.GetTracerProvider())
109
110// Uninstall by passing empty config.
111cfg2 := config.Config{
112TracingConfig: config.TracingConfig{},
113}
114
115require.NoError(t, m.ApplyConfig(&cfg2))
116// Make sure we get a no-op tracer provider after uninstallation.
117require.Equal(t, noop.NewTracerProvider(), otel.GetTracerProvider())
118}
119
120func TestTracerProviderShutdown(t *testing.T) {
121m := NewManager(log.NewNopLogger())
122cfg := config.Config{
123TracingConfig: config.TracingConfig{
124Endpoint: "localhost:1234",
125ClientType: config.TracingClientGRPC,
126},
127}
128
129require.NoError(t, m.ApplyConfig(&cfg))
130m.Stop()
131
132// Check if we closed the done channel.
133_, ok := <-m.done
134require.False(t, ok)
135}
136