-
Notifications
You must be signed in to change notification settings - Fork 214
/
TraceUtils.java
179 lines (152 loc) · 7.19 KB
/
TraceUtils.java
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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.tracing;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.auth.AuthorizationContextType;
import org.eclipse.ditto.base.model.headers.DittoHeaderDefinition;
import org.eclipse.ditto.internal.utils.metrics.DittoMetrics;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.Tag;
import org.eclipse.ditto.internal.utils.metrics.instruments.tag.TagSet;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.PreparedTimer;
import org.eclipse.ditto.internal.utils.tracing.span.SpanTagKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.pekko.http.javadsl.model.HttpRequest;
/**
* Utility for tracing Http requests.
*/
@Immutable
public final class TraceUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(TraceUtils.class);
private static final String TRACING_FILTER_DELIMITER = "_";
private static final String SLASH = "/";
private static final Pattern DUPLICATE_SLASH_PATTERN = Pattern.compile("\\/+");
private static final String HTTP_ROUNDTRIP_METRIC_NAME = "roundtrip_http";
private static final String FILTER_AUTH_METRIC_NAME = "filter_auth";
private static final String LIVE_CHANNEL_NAME = "live";
private static final String TWIN_CHANNEL_NAME = "twin";
private static final Pattern MESSAGE_PATTERN = Pattern.compile("(.*/(inbox|outbox)/messages/.*)|(.*/inbox/claim)");
private TraceUtils() {
throw new AssertionError();
}
/**
* Prepares an {@link PreparedTimer} with default {@link #HTTP_ROUNDTRIP_METRIC_NAME} and tags.
*
* @param request The request to extract tags and request method.
* @return The prepared {@link PreparedTimer}
*/
public static PreparedTimer newHttpRoundTripTimer(final HttpRequest request) {
return newExpiringTimer(HTTP_ROUNDTRIP_METRIC_NAME)
.tags(getTraceInformationTags(request))
.tag(SpanTagKey.REQUEST_METHOD_NAME.getTagForValue(request.method().name()))
.tag(SpanTagKey.CHANNEL.getTagForValue(determineChannel(request)));
}
private static TagSet getTraceInformationTags(final HttpRequest httpRequest) {
final TagSet result;
@Nullable final var traceInformation = tryToDetermineTraceInformation(getRelativeUriPath(httpRequest));
if (null == traceInformation) {
result = TagSet.empty();
} else {
result = traceInformation.getTagSet();
}
return result;
}
@Nullable
private static TraceInformation tryToDetermineTraceInformation(final String requestPath) {
try {
return determineTraceInformation(requestPath);
} catch (final IllegalArgumentException e) {
LOGGER.warn("Failed to determine trace information for request path <{}>: {}", requestPath, e.getMessage());
return null;
}
}
private static TraceInformation determineTraceInformation(final String requestPath) {
final var traceInformationGenerator = TraceInformationGenerator.getInstance();
return traceInformationGenerator.apply(requestPath);
}
private static String getRelativeUriPath(final HttpRequest httpRequest) {
final var uri = httpRequest.getUri();
final var relativeUri = uri.toRelative();
return relativeUri.path();
}
/**
* Prepares an {@link PreparedTimer} with default {@link #FILTER_AUTH_METRIC_NAME} and tags.
*
* @param authorizationContextType the authorization context type (i.e. JWT, ...).
* @return the prepared {@link PreparedTimer}.
* @throws NullPointerException if {@code authorizationContextType} is {@code null}.
*/
public static PreparedTimer newAuthFilterTimer(final AuthorizationContextType authorizationContextType) {
return newAuthFilterTimer(authorizationContextType, TagSet.empty());
}
private static PreparedTimer newAuthFilterTimer(
final AuthorizationContextType authorizationContextType,
final TagSet requestTags
) {
return newExpiringTimer(FILTER_AUTH_METRIC_NAME)
.tags(requestTags)
.tag(SpanTagKey.AUTH_SUCCESS.getTagForValue(false))
.tag(SpanTagKey.AUTH_ERROR.getTagForValue(false))
.tag(getAuthTypeMetricsTag(authorizationContextType))
.onExpiration(expiredTimer ->
expiredTimer
.tag(SpanTagKey.AUTH_SUCCESS.getTagForValue(false))
.tag(SpanTagKey.AUTH_ERROR.getTagForValue(true)));
}
private static PreparedTimer newExpiringTimer(final String tracingFilter) {
return DittoMetrics.timer(metricizeTraceUri(tracingFilter));
}
private static Tag getAuthTypeMetricsTag(final AuthorizationContextType authorizationContextType) {
return Tag.of(SpanTagKey.KEY_PREFIX + "auth.type", authorizationContextType.toString());
}
private static String determineChannel(final HttpRequest request) {
// determine channel based on header or query parameter
final boolean liveHeaderPresent = request.getHeader(DittoHeaderDefinition.CHANNEL.getKey())
.filter(channelHeader -> LIVE_CHANNEL_NAME.equals(channelHeader.value()))
.isPresent();
final boolean liveQueryPresent = request.getUri().query().get(DittoHeaderDefinition.CHANNEL.getKey())
.filter(LIVE_CHANNEL_NAME::equals)
.isPresent();
// messages are always live commands
final String normalizePath = normalizePath(request.getUri().path());
final boolean messageRequest = MESSAGE_PATTERN.matcher(normalizePath).matches();
return liveHeaderPresent || liveQueryPresent || messageRequest ? LIVE_CHANNEL_NAME : TWIN_CHANNEL_NAME;
}
/**
* Replaces all characters that are invalid for metrics (at least for Prometheus metrics).
*/
public static String metricizeTraceUri(final String traceUri) {
return traceUri.replaceAll("[./:-]", TRACING_FILTER_DELIMITER);
}
/**
* Normalizes the path and removes duplicate slashes.
*/
public static String normalizePath(final String path) {
if (path.isEmpty()) {
return SLASH;
}
// remove duplicate slashes
String normalized = DUPLICATE_SLASH_PATTERN.matcher(path).replaceAll(SLASH);
// strip trailing slash if necessary
if (normalized.length() > 1 && normalized.endsWith(SLASH)) {
normalized = normalized.substring(0, normalized.length() - 1);
}
// add leading slash if necessary
if (!normalized.startsWith(SLASH)) {
normalized = SLASH + normalized;
}
return normalized;
}
}