Skip to content

Commit 42f5f33

Browse files
authored
X-Ray Sampler: Match rule's HTTP path against http.url attribute if target not present. (#141)
1 parent ec06b65 commit 42f5f33

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/SamplingRuleApplier.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,15 @@ private SamplingRuleApplier(
147147
boolean matches(Attributes attributes, Resource resource) {
148148
int matchedAttributes = 0;
149149
String httpTarget = null;
150+
String httpUrl = null;
150151
String httpMethod = null;
151152
String host = null;
152153

153154
for (Map.Entry<AttributeKey<?>, Object> entry : attributes.asMap().entrySet()) {
154155
if (entry.getKey().equals(SemanticAttributes.HTTP_TARGET)) {
155156
httpTarget = (String) entry.getValue();
157+
} else if (entry.getKey().equals(SemanticAttributes.HTTP_URL)) {
158+
httpUrl = (String) entry.getValue();
156159
} else if (entry.getKey().equals(SemanticAttributes.HTTP_METHOD)) {
157160
httpMethod = (String) entry.getValue();
158161
} else if (entry.getKey().equals(SemanticAttributes.HTTP_HOST)) {
@@ -174,6 +177,22 @@ boolean matches(Attributes attributes, Resource resource) {
174177
return false;
175178
}
176179

180+
// URL Path may be in either http.target or http.url
181+
if (httpTarget == null && httpUrl != null) {
182+
int schemeEndIndex = httpUrl.indexOf("://");
183+
// Per spec, http.url is always populated with scheme://host/target. If scheme doesn't
184+
// match, assume it's bad instrumentation and ignore.
185+
if (schemeEndIndex > 0) {
186+
int pathIndex = httpUrl.indexOf('/', schemeEndIndex + "://".length());
187+
if (pathIndex < 0) {
188+
// No path, equivalent to root path.
189+
httpTarget = "/";
190+
} else {
191+
httpTarget = httpUrl.substring(pathIndex);
192+
}
193+
}
194+
}
195+
177196
return urlPathMatcher.matches(httpTarget)
178197
&& serviceNameMatcher.matches(resource.getAttribute(ResourceAttributes.SERVICE_NAME))
179198
&& httpMethodMatcher.matches(httpMethod)

aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/SamplingRuleApplierTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ void fixedRateAlwaysSample() {
104104
@Test
105105
void matches() {
106106
assertThat(applier.matches(attributes, resource)).isTrue();
107+
108+
// http.url works too
109+
assertThat(
110+
applier.matches(
111+
attributes.toBuilder()
112+
.remove(SemanticAttributes.HTTP_TARGET)
113+
.put(SemanticAttributes.HTTP_URL, "scheme://host:port/instrument-me")
114+
.build(),
115+
resource))
116+
.isTrue();
107117
}
108118

109119
@Test
@@ -143,6 +153,27 @@ void pathNotMatch() {
143153
.put(SemanticAttributes.HTTP_TARGET, "/instrument-you")
144154
.build();
145155
assertThat(applier.matches(attributes, resource)).isFalse();
156+
attributes =
157+
this.attributes.toBuilder()
158+
.remove(SemanticAttributes.HTTP_TARGET)
159+
.put(SemanticAttributes.HTTP_URL, "scheme://host:port/instrument-you")
160+
.build();
161+
assertThat(applier.matches(attributes, resource)).isFalse();
162+
attributes =
163+
this.attributes.toBuilder()
164+
.remove(SemanticAttributes.HTTP_TARGET)
165+
.put(SemanticAttributes.HTTP_URL, "scheme://host:port")
166+
.build();
167+
assertThat(applier.matches(attributes, resource)).isFalse();
168+
169+
// Correct path, but we ignore anyways since the URL is malformed per spec, scheme is always
170+
// present.
171+
attributes =
172+
this.attributes.toBuilder()
173+
.remove(SemanticAttributes.HTTP_TARGET)
174+
.put(SemanticAttributes.HTTP_URL, "host:port/instrument-me")
175+
.build();
176+
assertThat(applier.matches(attributes, resource)).isFalse();
146177
}
147178

148179
@Test

0 commit comments

Comments
 (0)