Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Span Attributes which may be missing from OTEL-provided Span #372

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ public boolean evaluateRequestHeaders(Span span, Map<String, String> headers) {
return shouldBlock;
}

@Override
public boolean evaluateRequestHeaders(
Span span, Map<String, String> headers, Map<String, String> possiblyMissingSpanAttrs) {
boolean shouldBlock = false;
for (Filter filter : filters) {
try {
if (filter.evaluateRequestHeaders(span, headers, possiblyMissingSpanAttrs)) {
shouldBlock = true;
}
} catch (Throwable t) {
logger.warn(
"Throwable thrown while evaluating Request headers for filter {}",
filter.getClass().getName(),
t);
}
}
return shouldBlock;
}

@Override
public boolean evaluateRequestBody(Span span, String body, Map<String, String> headers) {
boolean shouldBlock = false;
Expand All @@ -68,4 +87,26 @@ public boolean evaluateRequestBody(Span span, String body, Map<String, String> h
}
return shouldBlock;
}

@Override
public boolean evaluateRequestBody(
Span span,
String body,
Map<String, String> headers,
Map<String, String> possiblyMissingAttrs) {
boolean shouldBlock = false;
for (Filter filter : filters) {
try {
if (filter.evaluateRequestBody(span, body, headers, possiblyMissingAttrs)) {
shouldBlock = true;
}
} catch (Throwable t) {
logger.warn(
"Throwable thrown while evaluating Request body for filter {}",
filter.getClass().getName(),
t);
}
}
return shouldBlock;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public interface Filter {
*/
boolean evaluateRequestHeaders(Span span, Map<String, String> headers);

/**
* Evaluate the execution (includes extra attributes which might be missing from the span).
*
* @param span
* @param headers
* @param possiblyMissingSpanAttrs
* @return
*/
default boolean evaluateRequestHeaders(
Span span, Map<String, String> headers, Map<String, String> possiblyMissingSpanAttrs) {
return evaluateRequestHeaders(span, headers);
}

/**
* Evaluate the execution.
*
Expand All @@ -43,4 +56,12 @@ public interface Filter {
* @return filter result
*/
boolean evaluateRequestBody(Span span, String body, Map<String, String> headers);

default boolean evaluateRequestBody(
Span span,
String body,
Map<String, String> headers,
Map<String, String> possiblyMissingSpanAttrs) {
return evaluateRequestBody(span, body, headers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ dependencies {
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:${versions["opentelemetry_semconv"]}")
library("org.apache.httpcomponents:httpasyncclient:4.1")
testImplementation(testFixtures(project(":testing-common")))
implementation(project(":instrumentation:utils"))
}

1 change: 1 addition & 0 deletions instrumentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies{
implementation(project(":instrumentation:undertow:undertow-1.4"))
implementation(project(":instrumentation:undertow:undertow-servlet-1.4"))
implementation(project(":instrumentation:vertx:vertx-web-3.0"))
implementation(project(":instrumentation:utils"))
implementation(project(":otel-extensions"))
}

Expand Down
1 change: 1 addition & 0 deletions instrumentation/netty/netty-4.0/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ dependencies {

testImplementation(testFixtures(project(":testing-common")))
testImplementation("org.asynchttpclient:async-http-client:2.0.9")
implementation(project(":instrumentation:utils"))
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_0.AttributeKeys;
import io.opentelemetry.javaagent.instrumentation.hypertrace.utils.SpanUtils;
import java.util.Map;
import org.hypertrace.agent.filter.FilterRegistry;

Expand All @@ -50,7 +51,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
Attribute<Map<String, String>> headersAttr = channel.attr(AttributeKeys.REQUEST_HEADERS);
Map<String, String> headers = headersAttr.getAndRemove();
if (headers != null && FilterRegistry.getFilter().evaluateRequestHeaders(span, headers)) {
Map<String, String> possiblyMissingAttrs =
SpanUtils.getPossiblyMissingSpanAttributesFromURI(((HttpRequest) msg).getUri());
if (headers != null
&& FilterRegistry.getFilter()
.evaluateRequestHeaders(span, headers, possiblyMissingAttrs)) {
forbidden(ctx, (HttpRequest) msg);
return;
}
Expand Down
1 change: 1 addition & 0 deletions instrumentation/netty/netty-4.1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ dependencies {
testImplementation(testFixtures(project(":testing-common")))
testImplementation("io.netty:netty-handler:4.1.0.Final")
testImplementation("org.asynchttpclient:async-http-client:2.1.0")
implementation(project(":instrumentation:utils"))
}

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.javaagent.instrumentation.hypertrace.netty.v4_1.AttributeKeys;
import io.opentelemetry.javaagent.instrumentation.hypertrace.utils.SpanUtils;
import java.util.Map;
import org.hypertrace.agent.filter.FilterRegistry;

Expand All @@ -50,7 +51,12 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
Attribute<Map<String, String>> headersAttr = channel.attr(AttributeKeys.REQUEST_HEADERS);
Map<String, String> headers = headersAttr.getAndRemove();
if (headers != null && FilterRegistry.getFilter().evaluateRequestHeaders(span, headers)) {
Map<String, String> possiblyMissingAttrs =
SpanUtils.getPossiblyMissingSpanAttributesFromURI(((HttpRequest) msg).uri());

if (headers != null
&& FilterRegistry.getFilter()
.evaluateRequestHeaders(span, headers, possiblyMissingAttrs)) {
forbidden(ctx, (HttpRequest) msg);
return;
}
Expand Down
1 change: 1 addition & 0 deletions instrumentation/servlet/servlet-3.0/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ dependencies {
testImplementation("org.eclipse.jetty:jetty-server:8.1.22.v20160922")
testImplementation("org.eclipse.jetty:jetty-servlet:8.1.22.v20160922")
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-semconv:${versions["opentelemetry_semconv"]}")
implementation(project(":instrumentation:utils"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public static boolean start(
headers.put(attributeKey.getKey(), headerValue);
}

if (FilterRegistry.getFilter().evaluateRequestHeaders(currentSpan, headers)) {
Map<String, String> possiblyMissingAttrs =
Utils.getPossiblyMissingSpanAttributes(httpRequest);

if (FilterRegistry.getFilter()
.evaluateRequestHeaders(currentSpan, headers, possiblyMissingAttrs)) {
httpResponse.setStatus(403);
// skip execution of the user code
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.instrumentation.api.field.VirtualField;
import io.opentelemetry.javaagent.instrumentation.hypertrace.utils.SpanUtils;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -121,4 +122,14 @@ public static void resetRequestBodyBuffers(
}
}
}

/**
* Creates a Map of those attributes that may be missing from the Span.
*
* @param request
* @return
*/
public static Map<String, String> getPossiblyMissingSpanAttributes(HttpServletRequest request) {
return SpanUtils.getPossiblyMissingSpanAttributes(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ public static ByteBufferSpanPair createRequestByteBufferSpanPair(
if (contentLength < 0) {
contentLength = ContentLengthUtils.DEFAULT;
}
Map<String, String> possiblyMissingAttrs =
io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v3_0.nowrapping.Utils
.getPossiblyMissingSpanAttributes(httpServletRequest);

return new ByteBufferSpanPair(
span,
BoundedBuffersFactory.createStream(contentLength, charset),
filter::evaluateRequestBody,
headers);
headers,
possiblyMissingAttrs);
}

public static CharBufferSpanPair createRequestCharBufferSpanPair(
Expand All @@ -56,11 +61,15 @@ public static CharBufferSpanPair createRequestCharBufferSpanPair(
if (contentLength < 0) {
contentLength = ContentLengthUtils.DEFAULT;
}
Map<String, String> possiblyMissingAttrs =
io.opentelemetry.javaagent.instrumentation.hypertrace.servlet.v3_0.nowrapping.Utils
.getPossiblyMissingSpanAttributes(httpServletRequest);
return new CharBufferSpanPair(
span,
BoundedBuffersFactory.createWriter(contentLength),
filter::evaluateRequestBody,
headers);
headers,
possiblyMissingAttrs);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import javax.servlet.ServletInputStream;
import org.ServletStreamContextAccess;
import org.TestServletInputStream;
import org.hypertrace.agent.core.TriFunction;
import org.hypertrace.agent.core.QuadFunction;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedBuffersFactory;
import org.hypertrace.agent.core.instrumentation.buffer.BoundedByteArrayOutputStream;
import org.hypertrace.agent.core.instrumentation.buffer.ByteBufferSpanPair;
Expand All @@ -48,7 +48,8 @@ public void read() throws IOException {
BoundedByteArrayOutputStream buffer =
BoundedBuffersFactory.createStream(StandardCharsets.UTF_8);
ByteBufferSpanPair bufferSpanPair =
new ByteBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new ByteBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());
ServletStreamContextAccess.addToInputStreamContext(servletInputStream, bufferSpanPair);

while (servletInputStream.read() != -1) {}
Expand All @@ -66,7 +67,8 @@ public void read_callDepth_is_cleared() throws IOException {
BoundedByteArrayOutputStream buffer =
BoundedBuffersFactory.createStream(StandardCharsets.UTF_8);
ByteBufferSpanPair bufferSpanPair =
new ByteBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new ByteBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());
ServletStreamContextAccess.addToInputStreamContext(servletInputStream, bufferSpanPair);

while (servletInputStream.read() != -1) {}
Expand All @@ -84,13 +86,14 @@ public void read_call_depth_read_calls_read() throws IOException {
BoundedByteArrayOutputStream buffer =
BoundedBuffersFactory.createStream(StandardCharsets.UTF_8);
ByteBufferSpanPair bufferSpanPair =
new ByteBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new ByteBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());
ServletStreamContextAccess.addToInputStreamContext(servletInputStream, bufferSpanPair);

servletInputStream.read(new byte[BODY.length()]);
Assertions.assertEquals(BODY.substring(2), buffer.toStringWithSuppliedCharset());
}

private static final TriFunction<Span, String, Map<String, String>, Boolean> NOOP_FILTER =
(span, body, headers) -> false;
private static final QuadFunction<Span, String, Map<String, String>, Map<String, String>, Boolean>
NOOP_FILTER = (span, body, headers, missingAttrs) -> false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;
import org.BufferedReaderPrintWriterContextAccess;
import org.TestBufferedReader;
import org.hypertrace.agent.core.TriFunction;
import org.hypertrace.agent.core.QuadFunction;
import org.hypertrace.agent.core.instrumentation.buffer.*;
import org.hypertrace.agent.testing.AbstractInstrumenterTest;
import org.junit.jupiter.api.Assertions;
Expand All @@ -34,8 +34,8 @@ public class BufferedReaderInstrumentationTest extends AbstractInstrumenterTest

private static final String TEST_SPAN_NAME = "foo";
private static final String BODY = "boobar";
private static final TriFunction<Span, String, Map<String, String>, Boolean> NOOP_FILTER =
(span, s, stringStringMap) -> false;
private static final QuadFunction<Span, String, Map<String, String>, Map<String, String>, Boolean>
NOOP_FILTER = (span, s, stringStringMap, missingAttrMap) -> false;

@Test
public void read() throws IOException {
Expand All @@ -45,7 +45,8 @@ public void read() throws IOException {

BoundedCharArrayWriter buffer = BoundedBuffersFactory.createWriter();
CharBufferSpanPair bufferSpanPair =
new CharBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new CharBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());

BufferedReaderPrintWriterContextAccess.addToBufferedReaderContext(
bufferedReader, bufferSpanPair);
Expand All @@ -63,7 +64,8 @@ public void read_callDepth_isCleared() throws IOException {

BoundedCharArrayWriter buffer = BoundedBuffersFactory.createWriter();
CharBufferSpanPair bufferSpanPair =
new CharBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new CharBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());

BufferedReaderPrintWriterContextAccess.addToBufferedReaderContext(
bufferedReader, bufferSpanPair);
Expand All @@ -80,7 +82,8 @@ public void read_char_arr() throws IOException {

BoundedCharArrayWriter buffer = BoundedBuffersFactory.createWriter();
CharBufferSpanPair bufferSpanPair =
new CharBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new CharBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());

BufferedReaderPrintWriterContextAccess.addToBufferedReaderContext(
bufferedReader, bufferSpanPair);
Expand All @@ -98,7 +101,8 @@ public void read_callDepth_char_arr() throws IOException {

BoundedCharArrayWriter buffer = BoundedBuffersFactory.createWriter();
CharBufferSpanPair bufferSpanPair =
new CharBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new CharBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());

BufferedReaderPrintWriterContextAccess.addToBufferedReaderContext(
bufferedReader, bufferSpanPair);
Expand All @@ -115,7 +119,8 @@ public void read_char_arr_offset() throws IOException {

BoundedCharArrayWriter buffer = BoundedBuffersFactory.createWriter();
CharBufferSpanPair bufferSpanPair =
new CharBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new CharBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());

BufferedReaderPrintWriterContextAccess.addToBufferedReaderContext(
bufferedReader, bufferSpanPair);
Expand All @@ -134,7 +139,8 @@ public void readLine() throws IOException {

BoundedCharArrayWriter buffer = BoundedBuffersFactory.createWriter();
CharBufferSpanPair bufferSpanPair =
new CharBufferSpanPair(span, buffer, NOOP_FILTER, Collections.emptyMap());
new CharBufferSpanPair(
span, buffer, NOOP_FILTER, Collections.emptyMap(), Collections.emptyMap());

BufferedReaderPrintWriterContextAccess.addToBufferedReaderContext(
bufferedReader, bufferSpanPair);
Expand Down
13 changes: 13 additions & 0 deletions instrumentation/utils/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
`java-library`
id("net.bytebuddy.byte-buddy")
id("io.opentelemetry.instrumentation.auto-instrumentation")
muzzle
}

val versions: Map<String, String> by extra

dependencies{
implementation("javax.servlet:javax.servlet-api:3.1.0")
implementation("org.slf4j:slf4j-api:${versions["slf4j"]}")
}
Loading