prometheus

Форк
0
/
tracing_test.go 
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

14
package tracing
15

16
import (
17
	"testing"
18

19
	"github.com/go-kit/log"
20
	config_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

28
func TestInstallingNewTracerProvider(t *testing.T) {
29
	tpBefore := otel.GetTracerProvider()
30

31
	m := NewManager(log.NewNopLogger())
32
	cfg := config.Config{
33
		TracingConfig: config.TracingConfig{
34
			Endpoint:   "localhost:1234",
35
			ClientType: config.TracingClientGRPC,
36
		},
37
	}
38

39
	require.NoError(t, m.ApplyConfig(&cfg))
40
	require.NotEqual(t, tpBefore, otel.GetTracerProvider())
41
}
42

43
func TestReinstallingTracerProvider(t *testing.T) {
44
	m := NewManager(log.NewNopLogger())
45
	cfg := config.Config{
46
		TracingConfig: config.TracingConfig{
47
			Endpoint:   "localhost:1234",
48
			ClientType: config.TracingClientGRPC,
49
			Headers:    map[string]string{"foo": "bar"},
50
		},
51
	}
52

53
	require.NoError(t, m.ApplyConfig(&cfg))
54
	tpFirstConfig := otel.GetTracerProvider()
55

56
	// Trying to apply the same config should not reinstall provider.
57
	require.NoError(t, m.ApplyConfig(&cfg))
58
	require.Equal(t, tpFirstConfig, otel.GetTracerProvider())
59

60
	cfg2 := config.Config{
61
		TracingConfig: config.TracingConfig{
62
			Endpoint:   "localhost:1234",
63
			ClientType: config.TracingClientHTTP,
64
			Headers:    map[string]string{"bar": "foo"},
65
		},
66
	}
67

68
	require.NoError(t, m.ApplyConfig(&cfg2))
69
	require.NotEqual(t, tpFirstConfig, otel.GetTracerProvider())
70
	tpSecondConfig := otel.GetTracerProvider()
71

72
	// Setting previously unset option should reinstall provider.
73
	cfg2.TracingConfig.Compression = "gzip"
74
	require.NoError(t, m.ApplyConfig(&cfg2))
75
	require.NotEqual(t, tpSecondConfig, otel.GetTracerProvider())
76
}
77

78
func TestReinstallingTracerProviderWithTLS(t *testing.T) {
79
	m := NewManager(log.NewNopLogger())
80
	cfg := config.Config{
81
		TracingConfig: config.TracingConfig{
82
			Endpoint:   "localhost:1234",
83
			ClientType: config.TracingClientGRPC,
84
			TLSConfig: config_util.TLSConfig{
85
				CAFile: "testdata/ca.cer",
86
			},
87
		},
88
	}
89

90
	require.NoError(t, m.ApplyConfig(&cfg))
91
	tpFirstConfig := otel.GetTracerProvider()
92

93
	// Trying to apply the same config with TLS should reinstall provider.
94
	require.NoError(t, m.ApplyConfig(&cfg))
95
	require.NotEqual(t, tpFirstConfig, otel.GetTracerProvider())
96
}
97

98
func TestUninstallingTracerProvider(t *testing.T) {
99
	m := NewManager(log.NewNopLogger())
100
	cfg := config.Config{
101
		TracingConfig: config.TracingConfig{
102
			Endpoint:   "localhost:1234",
103
			ClientType: config.TracingClientGRPC,
104
		},
105
	}
106

107
	require.NoError(t, m.ApplyConfig(&cfg))
108
	require.NotEqual(t, noop.NewTracerProvider(), otel.GetTracerProvider())
109

110
	// Uninstall by passing empty config.
111
	cfg2 := config.Config{
112
		TracingConfig: config.TracingConfig{},
113
	}
114

115
	require.NoError(t, m.ApplyConfig(&cfg2))
116
	// Make sure we get a no-op tracer provider after uninstallation.
117
	require.Equal(t, noop.NewTracerProvider(), otel.GetTracerProvider())
118
}
119

120
func TestTracerProviderShutdown(t *testing.T) {
121
	m := NewManager(log.NewNopLogger())
122
	cfg := config.Config{
123
		TracingConfig: config.TracingConfig{
124
			Endpoint:   "localhost:1234",
125
			ClientType: config.TracingClientGRPC,
126
		},
127
	}
128

129
	require.NoError(t, m.ApplyConfig(&cfg))
130
	m.Stop()
131

132
	// Check if we closed the done channel.
133
	_, ok := <-m.done
134
	require.False(t, ok)
135
}
136

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.