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

Prototype to show how Typed Spans can be implemented #964

Closed
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
6 changes: 6 additions & 0 deletions sdk_contrib/typed_span/README.md
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)
17 changes: 17 additions & 0 deletions sdk_contrib/typed_span/build.gradle
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"
}
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;
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member

@Oberon00 Oberon00 Mar 13, 2020

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.


public BaseSpanWrapper(Span span) {
this.span = span;
}

public Span getRawSpan() {
return span;
}
}
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();
}
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
}
}
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());
}
}
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);
}
}
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";
Copy link
Member

Choose a reason for hiding this comment

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

This should later be switched to use #758.

Copy link
Member Author

Choose a reason for hiding this comment

The 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];
}
}
Loading