-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert liberty dispatcher instrumentation to instrumenter api (#4256)
* Convert liberty dispatcher instrumentation to instrumenter api * nullable * Add comment Co-authored-by: Trask Stalnaker <[email protected]>
- Loading branch information
Showing
10 changed files
with
274 additions
and
167 deletions.
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
32 changes: 0 additions & 32 deletions
32
.../opentelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyConnectionWrapper.java
This file was deleted.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
...avaagent/instrumentation/liberty/dispatcher/LibertyDispatcherHttpAttributesExtractor.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,101 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.liberty.dispatcher; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class LibertyDispatcherHttpAttributesExtractor | ||
extends HttpServerAttributesExtractor<LibertyRequest, LibertyResponse> { | ||
private static final Logger logger = | ||
LoggerFactory.getLogger(LibertyDispatcherHttpAttributesExtractor.class); | ||
|
||
@Override | ||
protected @Nullable String method(LibertyRequest libertyRequest) { | ||
return libertyRequest.getMethod(); | ||
} | ||
|
||
@Override | ||
protected @Nullable String userAgent(LibertyRequest libertyRequest) { | ||
return libertyRequest.getHeaderValue("User-Agent"); | ||
} | ||
|
||
@Override | ||
protected @Nullable Long requestContentLength( | ||
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected @Nullable Long requestContentLengthUncompressed( | ||
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected @Nullable String flavor( | ||
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) { | ||
String flavor = libertyRequest.getProtocol(); | ||
if (flavor != null) { | ||
// remove HTTP/ prefix to comply with semantic conventions | ||
if (flavor.startsWith("HTTP/")) { | ||
flavor = flavor.substring("HTTP/".length()); | ||
} | ||
} | ||
return flavor; | ||
} | ||
|
||
@Override | ||
protected @Nullable Integer statusCode( | ||
LibertyRequest libertyRequest, LibertyResponse libertyResponse) { | ||
return libertyResponse.getStatus(); | ||
} | ||
|
||
@Override | ||
protected @Nullable Long responseContentLength( | ||
LibertyRequest libertyRequest, LibertyResponse libertyResponse) { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected @Nullable Long responseContentLengthUncompressed( | ||
LibertyRequest libertyRequest, LibertyResponse libertyResponse) { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected @Nullable String target(LibertyRequest libertyRequest) { | ||
String requestUri = libertyRequest.getRequestUri(); | ||
String queryString = libertyRequest.getQueryString(); | ||
if (queryString != null && !queryString.isEmpty()) { | ||
return requestUri + "?" + queryString; | ||
} | ||
return requestUri; | ||
} | ||
|
||
@Override | ||
protected String host(LibertyRequest libertyRequest) { | ||
return libertyRequest.getServerName() + ":" + libertyRequest.getServerPort(); | ||
} | ||
|
||
@Override | ||
protected @Nullable String scheme(LibertyRequest libertyRequest) { | ||
return libertyRequest.getScheme(); | ||
} | ||
|
||
@Override | ||
protected @Nullable String route(LibertyRequest libertyRequest) { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected @Nullable String serverName( | ||
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) { | ||
return null; | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...javaagent/instrumentation/liberty/dispatcher/LibertyDispatcherNetAttributesExtractor.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,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.liberty.dispatcher; | ||
|
||
import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor; | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
public class LibertyDispatcherNetAttributesExtractor | ||
extends NetAttributesExtractor<LibertyRequest, LibertyResponse> { | ||
|
||
@Override | ||
public String transport(LibertyRequest libertyRequest) { | ||
return SemanticAttributes.NetTransportValues.IP_TCP; | ||
} | ||
|
||
@Override | ||
public @Nullable String peerName( | ||
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) { | ||
// condition limits calling peerName to onStart because in onEnd it may throw a NPE | ||
if (!libertyRequest.isCompleted()) { | ||
return libertyRequest.peerName(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public @Nullable Integer peerPort( | ||
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) { | ||
// condition limits calling getServerPort to onStart because in onEnd it may throw a NPE | ||
if (!libertyRequest.isCompleted()) { | ||
return libertyRequest.getServerPort(); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public @Nullable String peerIp( | ||
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) { | ||
// condition limits calling peerIp to onStart because in onEnd it may throw a NPE | ||
if (!libertyRequest.isCompleted()) { | ||
return libertyRequest.peerIp(); | ||
} | ||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...elemetry/javaagent/instrumentation/liberty/dispatcher/LibertyDispatcherRequestGetter.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,23 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.liberty.dispatcher; | ||
|
||
import io.opentelemetry.context.propagation.TextMapGetter; | ||
|
||
public class LibertyDispatcherRequestGetter implements TextMapGetter<LibertyRequest> { | ||
|
||
public static final LibertyDispatcherRequestGetter GETTER = new LibertyDispatcherRequestGetter(); | ||
|
||
@Override | ||
public Iterable<String> keys(LibertyRequest carrier) { | ||
return carrier.getAllHeaderNames(); | ||
} | ||
|
||
@Override | ||
public String get(LibertyRequest carrier, String key) { | ||
return carrier.getHeaderValue(key); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...entelemetry/javaagent/instrumentation/liberty/dispatcher/LibertyDispatcherSingletons.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,55 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.liberty.dispatcher; | ||
|
||
import static io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming.Source.CONTAINER; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerMetrics; | ||
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanStatusExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming; | ||
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor; | ||
|
||
public final class LibertyDispatcherSingletons { | ||
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.liberty-dispatcher"; | ||
|
||
private static final Instrumenter<LibertyRequest, LibertyResponse> INSTRUMENTER; | ||
|
||
static { | ||
HttpServerAttributesExtractor<LibertyRequest, LibertyResponse> httpAttributesExtractor = | ||
new LibertyDispatcherHttpAttributesExtractor(); | ||
SpanNameExtractor<LibertyRequest> spanNameExtractor = | ||
HttpSpanNameExtractor.create(httpAttributesExtractor); | ||
SpanStatusExtractor<LibertyRequest, LibertyResponse> spanStatusExtractor = | ||
HttpSpanStatusExtractor.create(httpAttributesExtractor); | ||
NetAttributesExtractor<LibertyRequest, LibertyResponse> netAttributesExtractor = | ||
new LibertyDispatcherNetAttributesExtractor(); | ||
|
||
INSTRUMENTER = | ||
Instrumenter.<LibertyRequest, LibertyResponse>newBuilder( | ||
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor) | ||
.setSpanStatusExtractor(spanStatusExtractor) | ||
.addAttributesExtractor(httpAttributesExtractor) | ||
.addAttributesExtractor(netAttributesExtractor) | ||
.addAttributesExtractor(PeerServiceAttributesExtractor.create(netAttributesExtractor)) | ||
.addContextCustomizer( | ||
(context, request, attributes) -> ServerSpanNaming.init(context, CONTAINER)) | ||
.addRequestMetrics(HttpServerMetrics.get()) | ||
.newServerInstrumenter(LibertyDispatcherRequestGetter.GETTER); | ||
} | ||
|
||
public static Instrumenter<LibertyRequest, LibertyResponse> instrumenter() { | ||
return INSTRUMENTER; | ||
} | ||
|
||
private LibertyDispatcherSingletons() {} | ||
} |
Oops, something went wrong.