Skip to content

Commit

Permalink
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
Browse files Browse the repository at this point in the history
…nstrumentation into lettuce-6
  • Loading branch information
Anuraag Agrawal committed Mar 18, 2021
2 parents 962556a + 6ea1e8d commit 51fc7bc
Show file tree
Hide file tree
Showing 20 changed files with 902 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,36 @@ private Span internalStartSpan(
if (startTimeNanos > 0) {
spanBuilder.setStartTimestamp(startTimeNanos, TimeUnit.NANOSECONDS);
}
onRequest(spanBuilder, request);
Span span = spanBuilder.startSpan();
onRequest(span, request);
return span;
}

protected void onRequest(SpanBuilder spanBuilder, REQUEST request) {
onRequest(spanBuilder::setAttribute, request);
}

/**
* This method should only be used when the request is not yet available when {@link #startSpan}
* is called. Otherwise {@link #onRequest(SpanBuilder, Object)} should be used.
*/
protected void onRequest(Span span, REQUEST request) {
assert span != null;
onRequest(span::setAttribute, request);
}

private void onRequest(AttributeSetter setter, REQUEST request) {
assert setter != null;
if (request != null) {
span.setAttribute(SemanticAttributes.NET_TRANSPORT, "IP.TCP");
span.setAttribute(SemanticAttributes.HTTP_METHOD, method(request));
span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, requestHeader(request, USER_AGENT));
setter.setAttribute(SemanticAttributes.NET_TRANSPORT, "IP.TCP");
setter.setAttribute(SemanticAttributes.HTTP_METHOD, method(request));
setter.setAttribute(SemanticAttributes.HTTP_USER_AGENT, requestHeader(request, USER_AGENT));

setFlavor(span, request);
setUrl(span, request);
setFlavor(setter, request);
setUrl(setter, request);
}
}

private void setFlavor(Span span, REQUEST request) {
private void setFlavor(AttributeSetter setter, REQUEST request) {
String flavor = flavor(request);
if (flavor == null) {
return;
Expand All @@ -163,15 +175,15 @@ private void setFlavor(Span span, REQUEST request) {
flavor = flavor.substring(httpProtocolPrefix.length());
}

span.setAttribute(SemanticAttributes.HTTP_FLAVOR, flavor);
setter.setAttribute(SemanticAttributes.HTTP_FLAVOR, flavor);
}

private void setUrl(Span span, REQUEST request) {
private void setUrl(AttributeSetter setter, REQUEST request) {
try {
URI url = url(request);
if (url != null) {
netPeerAttributes.setNetPeer(span, url.getHost(), null, url.getPort());
span.setAttribute(SemanticAttributes.HTTP_URL, url.toString());
netPeerAttributes.setNetPeer(setter, url.getHost(), null, url.getPort());
setter.setAttribute(SemanticAttributes.HTTP_URL, url.toString());
}
} catch (Exception e) {
log.debug("Error tagging url", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,17 @@ public Context startSpan(
// whether to call end() or not on the Span in the returned Context

Context parentContext = extract(request, getGetter());
SpanBuilder builder = spanBuilder(parentContext, spanName, SERVER);
SpanBuilder spanBuilder = spanBuilder(parentContext, spanName, SERVER);

if (startTimestamp >= 0) {
builder.setStartTimestamp(startTimestamp, TimeUnit.NANOSECONDS);
spanBuilder.setStartTimestamp(startTimestamp, TimeUnit.NANOSECONDS);
}

Span span = builder.startSpan();
onConnection(span, connection);
onRequest(span, request);
onConnectionAndRequest(span, connection, request);
onConnection(spanBuilder, connection);
onRequest(spanBuilder, request);
onConnectionAndRequest(spanBuilder, connection, request);

Context context = withServerSpan(parentContext, span);
Context context = withServerSpan(parentContext, spanBuilder.startSpan());
context = customizeContext(context, request);
attachServerContext(context, storage);

Expand Down Expand Up @@ -113,6 +112,7 @@ public void end(Context context, RESPONSE response, long timestamp) {
* Convenience method. Delegates to {@link #endExceptionally(Context, Throwable, Object)}, passing
* {@code response} value of {@code null}.
*/
@Override
public void endExceptionally(Context context, Throwable throwable) {
endExceptionally(context, throwable, null);
}
Expand Down Expand Up @@ -153,21 +153,22 @@ public Span getServerSpan(STORAGE storage) {
@Nullable
public abstract Context getServerContext(STORAGE storage);

protected void onConnection(Span span, CONNECTION connection) {
protected void onConnection(SpanBuilder spanBuilder, CONNECTION connection) {
// TODO: use NetPeerAttributes here
span.setAttribute(SemanticAttributes.NET_PEER_IP, peerHostIP(connection));
spanBuilder.setAttribute(SemanticAttributes.NET_PEER_IP, peerHostIP(connection));
Integer port = peerPort(connection);
// Negative or Zero ports might represent an unset/null value for an int type. Skip setting.
if (port != null && port > 0) {
span.setAttribute(SemanticAttributes.NET_PEER_PORT, (long) port);
spanBuilder.setAttribute(SemanticAttributes.NET_PEER_PORT, (long) port);
}
}

protected void onRequest(Span span, REQUEST request) {
span.setAttribute(SemanticAttributes.HTTP_METHOD, method(request));
span.setAttribute(SemanticAttributes.HTTP_USER_AGENT, requestHeader(request, USER_AGENT));
protected void onRequest(SpanBuilder spanBuilder, REQUEST request) {
spanBuilder.setAttribute(SemanticAttributes.HTTP_METHOD, method(request));
spanBuilder.setAttribute(
SemanticAttributes.HTTP_USER_AGENT, requestHeader(request, USER_AGENT));

setUrl(span, request);
setUrl(spanBuilder, request);

// TODO set resource name from URL.
}
Expand All @@ -183,20 +184,21 @@ protected void onRequest(Span span, REQUEST request) {
which is the recommended value for http.target attribute. Therefore we cannot use any of the
recommended combinations of attributes and are forced to use http.url.
*/
private void setUrl(Span span, REQUEST request) {
span.setAttribute(SemanticAttributes.HTTP_URL, url(request));
private void setUrl(SpanBuilder spanBuilder, REQUEST request) {
spanBuilder.setAttribute(SemanticAttributes.HTTP_URL, url(request));
}

protected void onConnectionAndRequest(Span span, CONNECTION connection, REQUEST request) {
protected void onConnectionAndRequest(
SpanBuilder spanBuilder, CONNECTION connection, REQUEST request) {
String flavor = flavor(connection, request);
if (flavor != null) {
// remove HTTP/ prefix to comply with semantic conventions
if (flavor.startsWith("HTTP/")) {
flavor = flavor.substring("HTTP/".length());
}
span.setAttribute(SemanticAttributes.HTTP_FLAVOR, flavor);
spanBuilder.setAttribute(SemanticAttributes.HTTP_FLAVOR, flavor);
}
span.setAttribute(SemanticAttributes.HTTP_CLIENT_IP, clientIP(connection, request));
spanBuilder.setAttribute(SemanticAttributes.HTTP_CLIENT_IP, clientIP(connection, request));
}

private String clientIP(CONNECTION connection, REQUEST request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.instrumentation.api.tracer

import io.opentelemetry.api.trace.Span

import io.opentelemetry.context.propagation.TextMapSetter
import io.opentelemetry.instrumentation.api.tracer.net.NetPeerAttributes
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
Expand Down Expand Up @@ -132,23 +132,6 @@ class HttpClientTracerTest extends BaseTracerTest {
null | null
}

def "test assert null span"() {
setup:
def tracer = newTracer()

when:
tracer.onRequest((Span) null, null)

then:
thrown(AssertionError)

when:
tracer.onResponse((Span) null, null)

then:
thrown(AssertionError)
}

@Override
def newTracer() {
def netPeerAttributes = new NetPeerAttributes([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.instrumentation.servlet;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.instrumentation.api.servlet.AppServerBridge;
Expand All @@ -27,7 +28,14 @@ public abstract class ServletHttpServerTracer<RESPONSE>
private static final Logger log = LoggerFactory.getLogger(ServletHttpServerTracer.class);

public Context startSpan(HttpServletRequest request, String spanName) {
return startSpan(request, request, request, spanName);
Context context = startSpan(request, request, request, spanName);

SpanContext spanContext = Span.fromContext(context).getSpanContext();
// we do this e.g. so that servlet containers can use these values in their access logs
request.setAttribute("traceId", spanContext.getTraceId());
request.setAttribute("spanId", spanContext.getSpanId());

return context;
}

@Override
Expand Down Expand Up @@ -108,15 +116,6 @@ protected String method(HttpServletRequest request) {
return request.getMethod();
}

@Override
public void onRequest(Span span, HttpServletRequest request) {
// we do this e.g. so that servlet containers can use these values in their access logs
request.setAttribute("traceId", span.getSpanContext().getTraceId());
request.setAttribute("spanId", span.getSpanContext().getSpanId());

super.onRequest(span, request);
}

@Override
protected TextMapGetter<HttpServletRequest> getGetter() {
return HttpServletRequestGetter.GETTER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ dependencies {
testImplementation group: 'org.spockframework', name: 'spock-spring', version: "$versions.spock"
testImplementation 'javax.xml.bind:jaxb-api:2.3.1'
testImplementation group: 'org.elasticmq', name: 'elasticmq-rest-sqs_2.12', version: '1.0.0'
/**
* Temporarily using emq instead of localstack till the latter supports AWS trace propagation
*
* testImplementation deps.testcontainers
* testImplementation "org.testcontainers:localstack:${versions.testcontainers}
**/

testImplementation deps.testcontainers
testImplementation "org.testcontainers:localstack:${versions.testcontainers}"

latestDepTestLibrary group: 'org.apache.camel', name: 'camel-core', version: '2.+'
latestDepTestLibrary group: 'org.apache.camel', name: 'camel-spring-boot-starter', version: '2.+'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ final class CamelPropagationUtil {

private CamelPropagationUtil() {}

private static boolean isAwsPropagated(Endpoint endpoint) {
return endpoint.getClass().getName().endsWith("SqsEndpoint");
}

static Context extractParent(final Map<String, Object> exchangeHeaders, Endpoint endpoint) {
return (isAwsPropagated(endpoint)
? extractAwsPropagationParent(exchangeHeaders)
: extractHttpPropagationParent(exchangeHeaders));
}

private static boolean isAwsPropagated(Endpoint endpoint) {
return endpoint.getClass().getName().endsWith("SqsEndpoint");
}

private static Context extractAwsPropagationParent(final Map<String, Object> exchangeHeaders) {
return AwsXrayPropagator.getInstance()
.extract(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected String getDestination(Exchange exchange, Endpoint endpoint) {
@Override
public SpanKind getInitiatorSpanKind() {
switch (component) {
case "aws-sns":
case "aws-sqs":
return SpanKind.INTERNAL;
default:
Expand All @@ -105,6 +106,7 @@ public SpanKind getInitiatorSpanKind() {
@Override
public SpanKind getReceiverSpanKind() {
switch (component) {
case "aws-sns":
case "aws-sqs":
return SpanKind.INTERNAL;
default:
Expand Down
Loading

0 comments on commit 51fc7bc

Please sign in to comment.