forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXrayRulesSampler.java
More file actions
549 lines (501 loc) · 21.4 KB
/
XrayRulesSampler.java
File metadata and controls
549 lines (501 loc) · 21.4 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.awsxray;
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
import static io.opentelemetry.semconv.ServiceAttributes.SERVICE_NAME;
import static java.util.logging.Level.FINE;
import static java.util.stream.Collectors.toList;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.contrib.awsxray.GetSamplingTargetsResponse.SamplingTargetDocument;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Logger;
import javax.annotation.Nullable;
final class XrayRulesSampler implements Sampler {
private static final Logger logger = Logger.getLogger(XrayRulesSampler.class.getName());
public static final AttributeKey<String> AWS_XRAY_SAMPLING_RULE =
AttributeKey.stringKey("aws.xray.sampling_rule");
// Used for generating operation
private static final String UNKNOWN_OPERATION = "UnknownOperation";
private static final AttributeKey<String> URL_PATH = AttributeKey.stringKey("url.path");
private static final AttributeKey<String> HTTP_TARGET = AttributeKey.stringKey("http.target");
private static final AttributeKey<String> HTTP_REQUEST_METHOD =
AttributeKey.stringKey("http.request.method");
private static final AttributeKey<String> HTTP_METHOD = AttributeKey.stringKey("http.method");
private final String clientId;
private final Supplier<Resource> resource;
private final Clock clock;
private final Sampler fallbackSampler;
private final SamplingRuleApplier[] ruleAppliers;
private final Map<String, String> ruleToHashMap;
private final Map<String, String> hashToRuleMap;
private final boolean adaptiveSamplingRuleExists;
private final Cache<String, AwsXrayAdaptiveSamplingConfig.UsageType> traceUsageCache;
@Nullable private AwsXrayAdaptiveSamplingConfig adaptiveSamplingConfig;
@Nullable private RateLimiter anomalyCaptureRateLimiter;
XrayRulesSampler(
String clientId,
Supplier<Resource> resource,
Clock clock,
Sampler fallbackSampler,
List<GetSamplingRulesResponse.SamplingRule> rules,
@Nullable AwsXrayAdaptiveSamplingConfig adaptiveSamplingConfig) {
this(
clientId,
resource,
clock,
fallbackSampler,
rules.stream()
// Lower priority value takes precedence so normal ascending sort.
.sorted(Comparator.comparingInt(GetSamplingRulesResponse.SamplingRule::getPriority))
.map(
rule ->
new SamplingRuleApplier(
clientId, rule, resource.get().getAttribute(SERVICE_NAME), clock))
.toArray(SamplingRuleApplier[]::new),
createRuleHashMaps(rules),
rules.stream().anyMatch(r -> r.getSamplingRateBoost() != null),
adaptiveSamplingConfig,
Caffeine.newBuilder()
.maximumSize(100_000)
.ticker(clock::nanoTime)
.expireAfterWrite(Duration.ofMinutes(10))
.build());
}
private XrayRulesSampler(
String clientId,
Supplier<Resource> resource,
Clock clock,
Sampler fallbackSampler,
SamplingRuleApplier[] ruleAppliers,
Map<String, String> ruleToHashMap,
boolean adaptiveSamplingRuleExists,
@Nullable AwsXrayAdaptiveSamplingConfig adaptiveSamplingConfig,
Cache<String, AwsXrayAdaptiveSamplingConfig.UsageType> traceUsageCache) {
this.clientId = clientId;
this.resource = resource;
this.clock = clock;
this.fallbackSampler = fallbackSampler;
this.ruleAppliers = ruleAppliers;
this.ruleToHashMap = ruleToHashMap;
this.hashToRuleMap = new HashMap<>();
for (Map.Entry<String, String> entry : ruleToHashMap.entrySet()) {
this.hashToRuleMap.put(entry.getValue(), entry.getKey());
}
this.adaptiveSamplingRuleExists = adaptiveSamplingRuleExists;
this.adaptiveSamplingConfig = adaptiveSamplingConfig;
this.traceUsageCache = traceUsageCache;
// Initialize anomaly capture rate limiter
if (this.adaptiveSamplingConfig != null
&& this.adaptiveSamplingConfig.getAnomalyCaptureLimit() == null) {
this.anomalyCaptureRateLimiter = new RateLimiter(1, 1, clock);
} else if (adaptiveSamplingConfig != null
&& adaptiveSamplingConfig.getAnomalyCaptureLimit() != null) {
int anomalyTracesPerSecond =
adaptiveSamplingConfig.getAnomalyCaptureLimit().getAnomalyTracesPerSecond();
this.anomalyCaptureRateLimiter =
new RateLimiter(anomalyTracesPerSecond, anomalyTracesPerSecond, clock);
}
}
@Override
public SamplingResult shouldSample(
Context parentContext,
String traceId,
String name,
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks) {
SpanContext parentSpanContext = Span.fromContext(parentContext).getSpanContext();
String upstreamMatchedRule =
parentSpanContext
.getTraceState()
.get(AwsSamplingResult.AWS_XRAY_SAMPLING_RULE_TRACE_STATE_KEY);
if (upstreamMatchedRule == null) {
Baggage b = Baggage.fromContext(parentContext);
upstreamMatchedRule =
b != null
? b.getEntryValue(AwsSamplingResult.AWS_XRAY_SAMPLING_RULE_TRACE_STATE_KEY)
: null;
}
for (SamplingRuleApplier applier : ruleAppliers) {
if (applier.matches(attributes, resource.get())) {
SamplingResult result =
applier.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
// If the trace state has a sampling rule reference, propagate it
// Otherwise, encode and propagate the matched sampling rule using AwsSamplingResult
String ruleToPropagate;
if (upstreamMatchedRule != null) {
ruleToPropagate = hashToRuleMap.getOrDefault(upstreamMatchedRule, null);
} else if (parentSpanContext.isValid()) {
ruleToPropagate = null;
} else {
ruleToPropagate = applier.getRuleName();
}
String hashedRule = ruleToHashMap.getOrDefault(ruleToPropagate, upstreamMatchedRule);
return AwsSamplingResult.create(
result.getDecision(),
result.getAttributes().toBuilder()
.put(
AWS_XRAY_SAMPLING_RULE.getKey(),
ruleToPropagate != null ? ruleToPropagate : "UNKNOWN")
.build(),
hashedRule);
}
}
// In practice, X-Ray always returns a Default rule that matches all requests so it is a bug in
// our code or X-Ray to reach here, fallback just in case.
logger.log(
FINE,
"No sampling rule matched the request. "
+ "This is a bug in either the OpenTelemetry SDK or X-Ray.");
return fallbackSampler.shouldSample(
parentContext, traceId, name, spanKind, attributes, parentLinks);
}
@Override
public String getDescription() {
return "XrayRulesSampler{" + Arrays.toString(ruleAppliers) + "}";
}
void setAdaptiveSamplingConfig(AwsXrayAdaptiveSamplingConfig config) {
if (this.adaptiveSamplingConfig != null) {
throw new IllegalStateException("Programming bug - Adaptive sampling config is already set");
} else if (config != null && this.adaptiveSamplingConfig == null) {
this.adaptiveSamplingConfig = config;
// Initialize anomaly capture rate limiter if error capture limit is configured
if (config.getAnomalyCaptureLimit() != null) {
int anomalyTracesPerSecond = config.getAnomalyCaptureLimit().getAnomalyTracesPerSecond();
this.anomalyCaptureRateLimiter =
new RateLimiter(anomalyTracesPerSecond, anomalyTracesPerSecond, clock);
} else {
this.anomalyCaptureRateLimiter = new RateLimiter(1, 1, clock);
}
}
}
void adaptSampling(ReadableSpan span, SpanData spanData, Consumer<ReadableSpan> spanBatcher) {
if (!adaptiveSamplingRuleExists && this.adaptiveSamplingConfig == null) {
return;
}
AnomalyDetectionResult result = isAnomaly(span, spanData);
boolean shouldBoostSampling = result.shouldBoostSampling();
boolean shouldCaptureAnomalySpan = result.shouldCaptureAnomalySpan();
String traceId = spanData.getTraceId();
AwsXrayAdaptiveSamplingConfig.UsageType existingUsage = traceUsageCache.getIfPresent(traceId);
boolean isNewTrace = existingUsage == null;
// Anomaly Capture
boolean isSpanCaptured = false;
if (AwsXrayAdaptiveSamplingConfig.UsageType.isUsedForAnomalyTraceCapture(existingUsage)
|| (shouldCaptureAnomalySpan
&& !span.getSpanContext().isSampled()
&& anomalyCaptureRateLimiter != null
&& anomalyCaptureRateLimiter.trySpend(1))) {
spanBatcher.accept(span);
isSpanCaptured = true;
}
// Sampling Boost
boolean isCountedAsAnomalyForBoost = false;
if (shouldBoostSampling || isNewTrace) {
String traceStateValue =
span.getSpanContext()
.getTraceState()
.get(AwsSamplingResult.AWS_XRAY_SAMPLING_RULE_TRACE_STATE_KEY);
String upstreamRuleName =
traceStateValue != null
? hashToRuleMap.getOrDefault(traceStateValue, traceStateValue)
: traceStateValue;
SamplingRuleApplier ruleToReportTo = null;
SamplingRuleApplier matchedRule = null;
for (SamplingRuleApplier applier : ruleAppliers) {
// Rule propagated from when sampling decision was made, otherwise the matched rule
if (applier.getRuleName().equals(upstreamRuleName)) {
ruleToReportTo = applier;
break;
}
if (applier.matches(spanData.getAttributes(), resource.get())) {
matchedRule = applier;
}
}
if (ruleToReportTo == null) {
if (matchedRule == null) {
logger.log(
FINE,
"No sampling rule matched the request. This is a bug in either the OpenTelemetry SDK or X-Ray.");
} else if (!span.getParentSpanContext().isValid()) {
// Span is not from an upstream service, so we should boost the matched rule
ruleToReportTo = matchedRule;
}
}
if (shouldBoostSampling
&& ruleToReportTo != null
&& ruleToReportTo.hasBoost()
&& !AwsXrayAdaptiveSamplingConfig.UsageType.isUsedForBoost(existingUsage)) {
ruleToReportTo.countAnomalyTrace(span);
isCountedAsAnomalyForBoost = true;
}
if (isNewTrace && ruleToReportTo != null && ruleToReportTo.hasBoost()) {
ruleToReportTo.countTrace();
}
}
updateTraceUsageCache(traceId, isSpanCaptured, isCountedAsAnomalyForBoost);
}
List<SamplingRuleApplier.SamplingRuleStatisticsSnapshot> snapshot(Date now) {
return Arrays.stream(ruleAppliers)
.map(rule -> rule.snapshot(now))
.filter(Objects::nonNull)
.collect(toList());
}
long nextTargetFetchTimeNanos() {
return Arrays.stream(ruleAppliers)
.mapToLong(SamplingRuleApplier::getNextSnapshotTimeNanos)
.min()
// There is always at least one rule in practice so this should never be exercised.
.orElseGet(() -> clock.nanoTime() + AwsXrayRemoteSampler.DEFAULT_TARGET_INTERVAL_NANOS);
}
XrayRulesSampler withTargets(
Map<String, SamplingTargetDocument> ruleTargets,
Set<String> requestedTargetRuleNames,
Date now) {
long currentNanoTime = clock.nanoTime();
long defaultNextSnapshotTimeNanos =
currentNanoTime + AwsXrayRemoteSampler.DEFAULT_TARGET_INTERVAL_NANOS;
SamplingRuleApplier[] newAppliers =
Arrays.stream(ruleAppliers)
.map(
rule -> {
SamplingTargetDocument target = ruleTargets.get(rule.getRuleName());
if (target != null) {
return rule.withTarget(target, now, currentNanoTime);
}
if (requestedTargetRuleNames.contains(rule.getRuleName())) {
// In practice X-Ray should return a target for any rule we requested but
// do a defensive check here in case. If we requested a target but got nothing
// back assume the default interval.
return rule.withNextSnapshotTimeNanos(defaultNextSnapshotTimeNanos);
}
// Target not requested, will be updated in a future target fetch.
return rule;
})
.toArray(SamplingRuleApplier[]::new);
return new XrayRulesSampler(
clientId,
resource,
clock,
fallbackSampler,
newAppliers,
ruleToHashMap,
adaptiveSamplingRuleExists,
adaptiveSamplingConfig,
traceUsageCache);
}
private AnomalyDetectionResult isAnomaly(ReadableSpan span, SpanData spanData) {
boolean shouldBoostSampling = false;
boolean shouldCaptureAnomalySpan = false;
Long statusCode = spanData.getAttributes().get(HTTP_RESPONSE_STATUS_CODE);
List<AwsXrayAdaptiveSamplingConfig.AnomalyConditions> anomalyConditions =
adaptiveSamplingConfig != null ? adaptiveSamplingConfig.getAnomalyConditions() : null;
// Empty list -> no conditions will apply and we will not do anything
if (anomalyConditions != null) {
String operation = spanData.getAttributes().get(AwsAttributeKeys.AWS_LOCAL_OPERATION);
if (operation == null) {
operation = generateIngressOperation(spanData);
}
for (AwsXrayAdaptiveSamplingConfig.AnomalyConditions condition : anomalyConditions) {
// Skip condition if it would only re-apply action already being taken
if ((shouldBoostSampling
&& AwsXrayAdaptiveSamplingConfig.UsageType.SAMPLING_BOOST.equals(
condition.getUsage()))
|| (shouldCaptureAnomalySpan
&& AwsXrayAdaptiveSamplingConfig.UsageType.ANOMALY_TRACE_CAPTURE.equals(
condition.getUsage()))) {
continue;
}
// Check if the operation matches any in the list or if operations list is null (match all)
List<String> operations = condition.getOperations();
if (!(operations == null || operations.isEmpty() || operations.contains(operation))) {
continue;
}
// Check if any anomalyConditions detect an anomaly either through error code or latency
boolean isAnomaly = false;
String errorCodeRegex = condition.getErrorCodeRegex();
if (statusCode != null && errorCodeRegex != null) {
isAnomaly = statusCode.toString().matches(errorCodeRegex);
}
Long highLatencyMs = condition.getHighLatencyMs();
if (highLatencyMs != null) {
isAnomaly =
(errorCodeRegex == null || isAnomaly)
&& (span.getLatencyNanos() / 1_000_000.0) >= highLatencyMs;
}
if (isAnomaly) {
AwsXrayAdaptiveSamplingConfig.UsageType usage = condition.getUsage();
if (usage != null) {
switch (usage) {
case BOTH:
shouldBoostSampling = true;
shouldCaptureAnomalySpan = true;
break;
case SAMPLING_BOOST:
shouldBoostSampling = true;
break;
case ANOMALY_TRACE_CAPTURE:
shouldCaptureAnomalySpan = true;
break;
default: // do nothing
}
} else {
shouldBoostSampling = true;
shouldCaptureAnomalySpan = true;
}
}
if (shouldBoostSampling && shouldCaptureAnomalySpan) {
break;
}
}
} else if ((statusCode != null && statusCode > 499)
|| (statusCode == null
&& spanData.getStatus() != null
&& StatusCode.ERROR.equals(spanData.getStatus().getStatusCode()))) {
shouldBoostSampling = true;
shouldCaptureAnomalySpan = true;
}
return new AnomalyDetectionResult(shouldBoostSampling, shouldCaptureAnomalySpan);
}
static boolean isKeyPresent(SpanData span, AttributeKey<?> key) {
return span.getAttributes().get(key) != null;
}
private static String generateIngressOperation(SpanData span) {
String operation = UNKNOWN_OPERATION;
if (isKeyPresent(span, URL_PATH) || isKeyPresent(span, HTTP_TARGET)) {
String httpTarget =
isKeyPresent(span, URL_PATH)
? span.getAttributes().get(URL_PATH)
: span.getAttributes().get(HTTP_TARGET);
// get the first part from API path string as operation value
// the more levels/parts we get from API path the higher chance for getting high cardinality
// data
if (httpTarget != null) {
operation = extractApiPathValue(httpTarget);
if (isKeyPresent(span, HTTP_REQUEST_METHOD) || isKeyPresent(span, HTTP_METHOD)) {
String httpMethod =
isKeyPresent(span, HTTP_REQUEST_METHOD)
? span.getAttributes().get(HTTP_REQUEST_METHOD)
: span.getAttributes().get(HTTP_METHOD);
if (httpMethod != null) {
operation = httpMethod + " " + operation;
}
}
}
}
return operation;
}
private static String extractApiPathValue(String httpTarget) {
if (httpTarget == null || httpTarget.isEmpty()) {
return "/";
}
String[] paths = httpTarget.split("/");
if (paths.length > 1) {
return "/" + paths[1];
}
return "/";
}
private void updateTraceUsageCache(
String traceId, boolean isSpanCaptured, boolean isCountedAsAnomalyForBoost) {
AwsXrayAdaptiveSamplingConfig.UsageType existingUsage = traceUsageCache.getIfPresent(traceId);
// Any interaction with a cache entry will reset the expiration timer of that entry
if (isSpanCaptured && isCountedAsAnomalyForBoost) {
this.traceUsageCache.put(traceId, AwsXrayAdaptiveSamplingConfig.UsageType.BOTH);
} else if (isSpanCaptured) {
if (AwsXrayAdaptiveSamplingConfig.UsageType.isUsedForBoost(existingUsage)) {
this.traceUsageCache.put(traceId, AwsXrayAdaptiveSamplingConfig.UsageType.BOTH);
} else {
this.traceUsageCache.put(
traceId, AwsXrayAdaptiveSamplingConfig.UsageType.ANOMALY_TRACE_CAPTURE);
}
} else if (isCountedAsAnomalyForBoost) {
if (AwsXrayAdaptiveSamplingConfig.UsageType.isUsedForAnomalyTraceCapture(existingUsage)) {
this.traceUsageCache.put(traceId, AwsXrayAdaptiveSamplingConfig.UsageType.BOTH);
} else {
this.traceUsageCache.put(traceId, AwsXrayAdaptiveSamplingConfig.UsageType.SAMPLING_BOOST);
}
} else if (existingUsage != null) {
this.traceUsageCache.put(traceId, existingUsage);
} else {
this.traceUsageCache.put(traceId, AwsXrayAdaptiveSamplingConfig.UsageType.NEITHER);
}
}
private static Map<String, String> createRuleHashMaps(
List<GetSamplingRulesResponse.SamplingRule> rules) {
Map<String, String> ruleToHashMap = new HashMap<>();
for (GetSamplingRulesResponse.SamplingRule rule : rules) {
String ruleName = rule.getRuleName();
if (ruleName != null) {
ruleToHashMap.put(ruleName, hashRuleName(ruleName));
}
}
return ruleToHashMap;
}
static String hashRuleName(String ruleName) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(ruleName.getBytes(StandardCharsets.UTF_8));
StringBuilder hexString = new StringBuilder();
for (int i = 0; i < Math.min(hash.length, 8); i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
return ruleName;
}
}
// For testing
Cache<String, AwsXrayAdaptiveSamplingConfig.UsageType> getTraceUsageCache() {
traceUsageCache.cleanUp();
return traceUsageCache;
}
private static class AnomalyDetectionResult {
private final boolean shouldBoostSampling;
private final boolean shouldCaptureAnomalySpan;
AnomalyDetectionResult(boolean shouldBoostSampling, boolean shouldCaptureAnomalySpan) {
this.shouldBoostSampling = shouldBoostSampling;
this.shouldCaptureAnomalySpan = shouldCaptureAnomalySpan;
}
boolean shouldBoostSampling() {
return shouldBoostSampling;
}
boolean shouldCaptureAnomalySpan() {
return shouldCaptureAnomalySpan;
}
}
}