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

Http spans #466

Closed
wants to merge 5 commits into from
Closed
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 @@ -25,6 +25,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Deprecated
public abstract class HttpServerDecorator<REQUEST, CONNECTION, RESPONSE> extends ServerDecorator {
public static final String SPAN_ATTRIBUTE = "io.opentelemetry.auto.span";
public static final String RESPONSE_ATTRIBUTE = "io.opentelemetry.auto.response";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package io.opentelemetry.auto.typed.server.http;

import io.opentelemetry.auto.config.Config;
import io.opentelemetry.auto.instrumentation.api.MoreTags;
import io.opentelemetry.auto.typed.base.DelegatingSpan;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.SpanId;
import io.opentelemetry.trace.Status;
import io.opentelemetry.trace.TraceId;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.attributes.SemanticAttributes;
import java.io.PrintWriter;
import java.io.StringWriter;

public class HttpServerSpan extends DelegatingSpan {

private HttpServerSpan(Span delegate) {
super(delegate);
}

public TraceId getTraceId() {
return delegate.getContext().getTraceId();
}

public SpanId getSpanId() {
return delegate.getContext().getSpanId();
}

//TODO review network attributes
public HttpServerSpan setPeerIp(String netPeerIp) {
SemanticAttributes.NET_PEER_IP.set(delegate, netPeerIp);
return this;
}

public void setPeerPort(Integer port) {
//TODO check this null handling and write documentation
// Negative or Zero ports might represent an unset/null value for an int type. Skip setting.
if (port != null && port > 0) {
SemanticAttributes.NET_PEER_PORT.set(delegate, port);
}
}

public void setMethod(String method) {
SemanticAttributes.HTTP_METHOD.set(delegate, method);
}

//TODO Specification does not recommend using this attribute
//See https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/http.md#http-server-semantic-conventions
Comment on lines +48 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

public void setUrl(String url) {
SemanticAttributes.HTTP_URL.set(delegate, url);
}
//See TODO above
// public void setUrl(String scheme, String host, String target){
// SemanticAttributes.HTTP_SCHEME.set(delegate, scheme);
// SemanticAttributes.HTTP_HOST.set(delegate, host);
// SemanticAttributes.HTTP_TARGET.set(delegate, target);
// }
//public void setUrl(String scheme, String serverName, Integer port, String target){
// SemanticAttributes.HTTP_SCHEME.set(delegate, scheme);
// SemanticAttributes.HTTP_SERVER_NAME.set(delegate, serverName);
// SemanticAttributes.NET_HOST_PORT.set(delegate, port);
// SemanticAttributes.HTTP_TARGET.set(delegate, target);
//}

public void setTarget(String target) {
SemanticAttributes.HTTP_TARGET.set(delegate, target);
}

public void setHost(String host) {
SemanticAttributes.HTTP_HOST.set(delegate, host);
}

public void setScheme(String scheme) {
SemanticAttributes.HTTP_SCHEME.set(delegate, scheme);
}

public void setStatus(Status status) {
delegate.setStatus(status);
}

public void setStatusCode(int code) {
SemanticAttributes.HTTP_STATUS_CODE.set(delegate, code);
//TODO think about it and at least document
if (Config.get().getHttpServerErrorStatuses().contains(code)) {
setStatus(Status.UNKNOWN);
}
}

public void setStatusText(String statusLine) {
SemanticAttributes.HTTP_STATUS_TEXT.set(delegate, statusLine);
}

public void setFlavor(String flavor) {
SemanticAttributes.HTTP_FLAVOR.set(delegate, flavor);
}

public void setUserAgent(String userAgent) {
SemanticAttributes.HTTP_USER_AGENT.set(delegate, userAgent);
}

public void setServerName(String serverName) {
SemanticAttributes.HTTP_SERVER_NAME.set(delegate, serverName);
}

public void setRoute(String route) {
SemanticAttributes.HTTP_ROUTE.set(delegate, route);
}

public void setClientIp(String clientIp) {
SemanticAttributes.HTTP_CLIENT_IP.set(delegate, clientIp);
}

public void setError(final Throwable throwable) {
delegate.setAttribute(MoreTags.ERROR_MSG, throwable.getMessage());
delegate.setAttribute(MoreTags.ERROR_TYPE, throwable.getClass().getName());

final StringWriter errorString = new StringWriter();
throwable.printStackTrace(new PrintWriter(errorString));
delegate.setAttribute(MoreTags.ERROR_STACK, errorString.toString());
}

public static HttpServerSpan create(Tracer tracer, String spanName, SpanContext parent) {
return new HttpServerSpan(tracer
.spanBuilder(spanName)
.setSpanKind(Kind.SERVER)
.setParent(parent)
.startSpan());
}

public void end(int responseStatus, Throwable throwable) {
setStatusCode(responseStatus);
//TODO status_message
setError(throwable);
delegate.end();
}

public void end(int responseStatus) {
setStatusCode(responseStatus);
delegate.end();
}

//TODO Only use if response status is uknown
public void end() {
delegate.end();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.opentelemetry.auto.typed.server.http;

import io.opentelemetry.context.Scope;

public class HttpServerSpanWithScope {
private final HttpServerSpan span;
private final Scope scope;

public HttpServerSpanWithScope(final HttpServerSpan span, final Scope scope) {
this.span = span;
this.scope = scope;
}

public HttpServerSpan getSpan() {
return span;
}

public void closeScope() {
scope.close();
}
}
Loading