-
Notifications
You must be signed in to change notification settings - Fork 839
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
Prototype to show how Typed Spans can be implemented #964
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# OpenTelemetry Contrib Typed Spans | ||
====================================================== | ||
|
||
* Java 7 compatible. | ||
|
||
For usage see the following [examples](src/test/java/io/opentelemetry/sdk/contrib/typedspan/TypedSpanTest.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
plugins { | ||
id "java" | ||
|
||
id "ru.vyarus.animalsniffer" | ||
} | ||
|
||
description = 'OpenTelemetry SDK Typed Span' | ||
ext.moduleName = 'io.opentelemetry.sdk.contrib.trace.types.span' | ||
|
||
dependencies { | ||
implementation project(':opentelemetry-api'), | ||
project(':opentelemetry-sdk'), | ||
project(':opentelemetry-exporters-inmemory'), | ||
libraries.guava | ||
|
||
signature "org.codehaus.mojo.signature:java17:1.0@signature" | ||
} |
15 changes: 15 additions & 0 deletions
15
...trib/typed_span/src/main/java/io/opentelemetry/sdk/contrib/typedspan/BaseSpanWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.opentelemetry.sdk.contrib.typedspan; | ||
|
||
import io.opentelemetry.trace.Span; | ||
|
||
public abstract class BaseSpanWrapper { | ||
Span span; | ||
|
||
public BaseSpanWrapper(Span span) { | ||
this.span = span; | ||
} | ||
|
||
public Span getRawSpan() { | ||
return span; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ped_span/src/main/java/io/opentelemetry/sdk/contrib/typedspan/BaseSpanWrapperBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.opentelemetry.sdk.contrib.typedspan; | ||
|
||
import io.opentelemetry.trace.Span; | ||
|
||
public abstract class BaseSpanWrapperBuilder<B extends BaseSpanWrapper> { | ||
Span.Builder builder; | ||
|
||
public BaseSpanWrapperBuilder(Span.Builder builder) { | ||
this.builder = builder; | ||
} | ||
|
||
public Span.Builder getRawSpanBuilder() { | ||
return builder; | ||
} | ||
|
||
public abstract B startSpan(); | ||
} |
31 changes: 31 additions & 0 deletions
31
...span/src/main/java/io/opentelemetry/sdk/contrib/typedspan/http/HttpClientSpanWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.opentelemetry.sdk.contrib.typedspan.http; | ||
|
||
import static io.opentelemetry.sdk.contrib.typedspan.http.HttpSpanWrapperBuilder.STATUS_CODE_KEY; | ||
import static io.opentelemetry.sdk.contrib.typedspan.http.HttpSpanWrapperBuilder.STATUS_TEXT_KEY; | ||
|
||
import io.opentelemetry.trace.Span; | ||
import io.opentelemetry.trace.Status; | ||
|
||
public class HttpClientSpanWrapper extends HttpSpanWrapper { | ||
public HttpClientSpanWrapper(Span span) { | ||
super(span); | ||
} | ||
|
||
public HttpClientSpanWrapper setStatusCode(HttpStatusCode status) { | ||
this.getRawSpan().setAttribute(STATUS_CODE_KEY, status.getCode()); | ||
thisthat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.getRawSpan().setAttribute(STATUS_TEXT_KEY, status.getMsg()); | ||
thisthat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this; | ||
} | ||
|
||
public HttpClientSpanWrapper setStatusCode(int code) { | ||
HttpStatusCode status = new HttpStatusCode(code); | ||
this.getRawSpan().setAttribute(STATUS_CODE_KEY, status.getCode()); | ||
this.getRawSpan().setAttribute(STATUS_TEXT_KEY, status.getMsg()); | ||
return this; | ||
thisthat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void end(Status status) { | ||
this.getRawSpan().setStatus(status); | ||
this.getRawSpan().end(); | ||
thisthat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...c/main/java/io/opentelemetry/sdk/contrib/typedspan/http/HttpClientSpanWrapperBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package io.opentelemetry.sdk.contrib.typedspan.http; | ||
|
||
import io.opentelemetry.trace.Span; | ||
import io.opentelemetry.trace.Tracer; | ||
import java.net.HttpURLConnection; | ||
import java.net.URLConnection; | ||
|
||
public class HttpClientSpanWrapperBuilder extends HttpSpanWrapperBuilder { | ||
|
||
private HttpClientSpanWrapperBuilder( | ||
Tracer tracer, Method method, String url, String host, String scheme) { | ||
this(tracer, method, url, host, scheme, HttpClientSpanWrapperBuilder.extractSpanName(url)); | ||
} | ||
|
||
private HttpClientSpanWrapperBuilder( | ||
Tracer tracer, Method method, String url, String host, String scheme, String spanName) { | ||
super(tracer, spanName, method); | ||
setrequiredFields(url, host, scheme); | ||
} | ||
|
||
private final void setrequiredFields(String url, String host, String scheme) { | ||
this.getRawSpanBuilder().setSpanKind(Span.Kind.CLIENT); | ||
this.setUrl(url); | ||
this.setHost(host); | ||
this.setScheme(scheme); | ||
} | ||
|
||
public static HttpClientSpanWrapperBuilder create(Tracer tracer, URLConnection connection) { | ||
String url = connection.getURL().toString(); | ||
String host = connection.getHeaderField("Host"); | ||
String schema = connection.getURL().getProtocol(); | ||
return new HttpClientSpanWrapperBuilder( | ||
tracer, connection.getDoOutput() ? Method.GET : Method.POST, url, host, schema); | ||
} | ||
|
||
public static HttpClientSpanWrapperBuilder create(Tracer tracer, HttpURLConnection connection) { | ||
String url = connection.getURL().toString(); | ||
String host = connection.getHeaderField("Host"); | ||
String schema = connection.getURL().getProtocol(); | ||
Method method = Method.getMethod(connection.getRequestMethod()); | ||
return new HttpClientSpanWrapperBuilder(tracer, method, url, host, schema); | ||
} | ||
|
||
@Override | ||
public HttpClientSpanWrapper startSpan() { | ||
return new HttpClientSpanWrapper(this.getRawSpanBuilder().startSpan()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...typed_span/src/main/java/io/opentelemetry/sdk/contrib/typedspan/http/HttpSpanWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.opentelemetry.sdk.contrib.typedspan.http; | ||
|
||
import io.opentelemetry.sdk.contrib.typedspan.BaseSpanWrapper; | ||
import io.opentelemetry.trace.Span; | ||
|
||
class HttpSpanWrapper extends BaseSpanWrapper { | ||
|
||
HttpSpanWrapper(Span span) { | ||
super(span); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...pan/src/main/java/io/opentelemetry/sdk/contrib/typedspan/http/HttpSpanWrapperBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package io.opentelemetry.sdk.contrib.typedspan.http; | ||
|
||
import io.opentelemetry.sdk.contrib.typedspan.BaseSpanWrapper; | ||
import io.opentelemetry.sdk.contrib.typedspan.BaseSpanWrapperBuilder; | ||
import io.opentelemetry.trace.Tracer; | ||
|
||
public abstract class HttpSpanWrapperBuilder extends BaseSpanWrapperBuilder<BaseSpanWrapper> { | ||
|
||
static final String UNKNOWN_URL = "<unknown>"; | ||
|
||
/** | ||
* Semantic Attributes | ||
* https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-http.md | ||
*/ | ||
public static final String METHOD_KEY = "http.method"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should later be switched to use #758. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a prototype to showcase how the API should look like. It is not meant to be merged |
||
|
||
public static final String URL_KEY = "http.url"; | ||
public static final String TARGET_KEY = "http.target"; | ||
public static final String HOST_KEY = "http.host"; | ||
public static final String SCHEME_KEY = "http.scheme"; | ||
public static final String STATUS_CODE_KEY = "http.status_code"; | ||
public static final String STATUS_TEXT_KEY = "http.status_text"; | ||
public static final String FLAVOR_KEY = "http.flavor"; | ||
public static final String USER_AGENT_KEY = "http.user_agent"; | ||
|
||
public enum Method { | ||
thisthat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GET, | ||
HEAD, | ||
POST, | ||
PUT, | ||
DELETE, | ||
CONNECT, | ||
OPTIONS, | ||
PATCH; | ||
|
||
public static Method getMethod(String method) { | ||
switch (method.toUpperCase()) { | ||
case "GET": | ||
return GET; | ||
case "HEAD": | ||
return HEAD; | ||
case "POST": | ||
return POST; | ||
case "PUT": | ||
return PUT; | ||
case "DELETE": | ||
return DELETE; | ||
case "CONNECT": | ||
return CONNECT; | ||
case "OPTIONS": | ||
return OPTIONS; | ||
default: | ||
return PATCH; | ||
} | ||
} | ||
} | ||
|
||
public enum Flavor { | ||
HTTP_1, | ||
HTTP_1_1, | ||
HTTP_2, | ||
SPDY, | ||
QUIC | ||
} | ||
|
||
public HttpSpanWrapperBuilder(Tracer tracer, String spanName, Method method) { | ||
super(tracer.spanBuilder(spanName)); | ||
setRequiredFields(method); | ||
} | ||
|
||
private final void setRequiredFields(Method method) { | ||
this.getRawSpanBuilder().setAttribute(METHOD_KEY, method.toString()); | ||
} | ||
|
||
public HttpSpanWrapperBuilder setUrl(String url) { | ||
this.getRawSpanBuilder().setAttribute(URL_KEY, url); | ||
return this; | ||
} | ||
|
||
public HttpSpanWrapperBuilder setHost(String host) { | ||
this.getRawSpanBuilder().setAttribute(HOST_KEY, host); | ||
return this; | ||
} | ||
|
||
public HttpSpanWrapperBuilder setScheme(String schema) { | ||
this.getRawSpanBuilder().setAttribute(SCHEME_KEY, schema); | ||
return this; | ||
} | ||
|
||
public HttpSpanWrapperBuilder setTarget(String target) { | ||
this.getRawSpanBuilder().setAttribute(TARGET_KEY, target); | ||
return this; | ||
} | ||
|
||
public HttpSpanWrapperBuilder setFlavor(Flavor flavor) { | ||
this.getRawSpanBuilder().setAttribute(FLAVOR_KEY, flavor.toString()); | ||
return this; | ||
} | ||
|
||
public HttpSpanWrapperBuilder setUserAgent(String userAgent) { | ||
this.getRawSpanBuilder().setAttribute(USER_AGENT_KEY, userAgent); | ||
return this; | ||
} | ||
|
||
protected static String extractSpanName(String url) { | ||
if (url == null) { | ||
return HttpSpanWrapperBuilder.UNKNOWN_URL; | ||
} | ||
return url.split("\\?", 2)[0]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't feel like this is pulling much weight, and maybe just adding extra mental overhead. Do you imagine more things going in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might insert some hook mechanism for the auto-instrumentation people.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After seeing open-telemetry/opentelemetry-java-instrumentation#195, I think what the auto-instrumentation SIG calls typed spans is actually unrelated to this PR, so I wonder if we will ever need any hooks.