forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleBasedRoutingSamplerComponentProviderTest.java
More file actions
223 lines (209 loc) · 8.84 KB
/
RuleBasedRoutingSamplerComponentProviderTest.java
File metadata and controls
223 lines (209 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package internal;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.config.DeclarativeConfigException;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSampler;
import io.opentelemetry.contrib.sampler.internal.RuleBasedRoutingSamplerComponentProvider;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.extension.incubator.fileconfig.DeclarativeConfiguration;
import io.opentelemetry.sdk.trace.IdGenerator;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
class RuleBasedRoutingSamplerComponentProviderTest {
private static final RuleBasedRoutingSamplerComponentProvider PROVIDER =
new RuleBasedRoutingSamplerComponentProvider();
@Test
void endToEnd() {
String yaml =
"file_format: 0.3\n"
+ "tracer_provider:\n"
+ " sampler:\n"
+ " parent_based:\n"
+ " root:\n"
+ " rule_based_routing:\n"
+ " fallback_sampler:\n"
+ " always_on:\n"
+ " span_kind: SERVER\n"
+ " rules:\n"
+ " - attribute: url.path\n"
+ " pattern: /actuator.*\n"
+ " action: DROP\n";
OpenTelemetrySdk openTelemetrySdk =
DeclarativeConfiguration.parseAndCreate(
new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8)));
Sampler sampler = openTelemetrySdk.getSdkTracerProvider().getSampler();
assertThat(sampler.toString())
.isEqualTo(
"ParentBased{"
+ "root:RuleBasedRoutingSampler{"
+ "rules=["
+ "SamplingRule{attributeKey=url.path, delegate=AlwaysOffSampler, pattern=/actuator.*}"
+ "], "
+ "kind=SERVER, "
+ "fallback=AlwaysOnSampler"
+ "},"
+ "remoteParentSampled:AlwaysOnSampler,"
+ "remoteParentNotSampled:AlwaysOffSampler,"
+ "localParentSampled:AlwaysOnSampler,"
+ "localParentNotSampled:AlwaysOffSampler"
+ "}");
// SERVER span to /actuator.* path should be dropped
assertThat(
sampler.shouldSample(
Context.root(),
IdGenerator.random().generateTraceId(),
"GET /actuator/health",
SpanKind.SERVER,
Attributes.builder().put("url.path", "/actuator/health").build(),
Collections.emptyList()))
.isEqualTo(SamplingResult.drop());
// SERVER span to other path should be recorded and sampled
assertThat(
sampler.shouldSample(
Context.root(),
IdGenerator.random().generateTraceId(),
"GET /actuator/health",
SpanKind.SERVER,
Attributes.builder().put("url.path", "/v1/users").build(),
Collections.emptyList()))
.isEqualTo(SamplingResult.recordAndSample());
}
@ParameterizedTest
@MethodSource("createValidArgs")
void create_Valid(String yaml, RuleBasedRoutingSampler expectedSampler) {
DeclarativeConfigProperties configProperties =
DeclarativeConfiguration.toConfigProperties(
new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8)));
Sampler sampler = PROVIDER.create(configProperties);
assertThat(sampler.toString()).isEqualTo(expectedSampler.toString());
}
static Stream<Arguments> createValidArgs() {
return Stream.of(
Arguments.of(
"fallback_sampler:\n"
+ " always_on:\n"
+ "span_kind: SERVER\n"
+ "rules:\n"
+ " - attribute: url.path\n"
+ " pattern: path\n"
+ " action: DROP\n",
RuleBasedRoutingSampler.builder(SpanKind.SERVER, Sampler.alwaysOn())
.drop(AttributeKey.stringKey("url.path"), "path")
.build()),
Arguments.of(
"fallback_sampler:\n"
+ " always_off:\n"
+ "span_kind: SERVER\n"
+ "rules:\n"
+ " - attribute: url.path\n"
+ " pattern: path\n"
+ " action: RECORD_AND_SAMPLE\n",
RuleBasedRoutingSampler.builder(SpanKind.SERVER, Sampler.alwaysOff())
.recordAndSample(AttributeKey.stringKey("url.path"), "path")
.build()),
Arguments.of(
"fallback_sampler:\n"
+ " always_off:\n"
+ "span_kind: CLIENT\n"
+ "rules:\n"
+ " - attribute: http.request.method\n"
+ " pattern: GET\n"
+ " action: DROP\n"
+ " - attribute: url.path\n"
+ " pattern: /foo/bar\n"
+ " action: DROP\n",
RuleBasedRoutingSampler.builder(SpanKind.CLIENT, Sampler.alwaysOff())
.drop(AttributeKey.stringKey("http.request.method"), "GET")
.drop(AttributeKey.stringKey("url.path"), "/foo/bar")
.build()));
}
@ParameterizedTest
@MethodSource("createInvalidArgs")
void create_Invalid(String yaml, String expectedErrorMessage) {
DeclarativeConfigProperties configProperties =
DeclarativeConfiguration.toConfigProperties(
new ByteArrayInputStream(yaml.getBytes(StandardCharsets.UTF_8)));
assertThatThrownBy(() -> PROVIDER.create(configProperties))
.isInstanceOf(DeclarativeConfigException.class)
.hasMessage(expectedErrorMessage);
}
static Stream<Arguments> createInvalidArgs() {
return Stream.of(
Arguments.of(
"span_kind: SERVER\n"
+ "rules:\n"
+ " - attribute: url.path\n"
+ " pattern: path\n",
"rule_based_routing sampler .fallback is required but is null"),
Arguments.of(
"fallback_sampler:\n"
+ " foo:\n"
+ "span_kind: foo\n"
+ "rules:\n"
+ " - attribute: url.path\n"
+ " pattern: path\n",
"rule_Based_routing sampler failed to create .fallback sampler"),
Arguments.of(
"fallback_sampler:\n"
+ " always_on:\n"
+ "span_kind: foo\n"
+ "rules:\n"
+ " - attribute: url.path\n"
+ " pattern: path\n",
"rule_based_routing sampler .span_kind is invalid: foo"),
Arguments.of(
"fallback_sampler:\n" + " always_on:\n" + "span_kind: SERVER\n",
"rule_based_routing sampler .rules is required"),
Arguments.of(
"fallback_sampler:\n" + " always_on:\n" + "span_kind: SERVER\n" + "rules: []\n",
"rule_based_routing sampler .rules is required"),
Arguments.of(
"fallback_sampler:\n"
+ " always_on:\n"
+ "span_kind: SERVER\n"
+ "rules:\n"
+ " - attribute: url.path\n",
"rule_based_routing sampler .rules[].pattern is required"),
Arguments.of(
"fallback_sampler:\n"
+ " always_on:\n"
+ "span_kind: SERVER\n"
+ "rules:\n"
+ " - pattern: path\n",
"rule_based_routing sampler .rules[].attribute is required"),
Arguments.of(
"fallback_sampler:\n"
+ " always_on:\n"
+ "span_kind: SERVER\n"
+ "rules:\n"
+ " - attribute: url.path\n"
+ " pattern: path\n",
"rule_based_routing sampler .rules[].action is required"),
Arguments.of(
"fallback_sampler:\n"
+ " always_on:\n"
+ "span_kind: SERVER\n"
+ "rules:\n"
+ " - attribute: url.path\n"
+ " pattern: path\n"
+ " action: foo\n",
"rule_based_routing sampler .rules[].action is must be RECORD_AND_SAMPLE or DROP"));
}
}