Skip to content

Commit

Permalink
fix liberty extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Oct 5, 2021
1 parent de116a1 commit d1a76e8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package com.ibm.ws.http.dispatcher.internal.channel;

import com.ibm.wsspi.http.HttpResponse;

// https://github.com/OpenLiberty/open-liberty/blob/master/dev/com.ibm.ws.transport.http/src/com/ibm/ws/http/dispatcher/internal/channel/HttpDispatcherLink.java
public class HttpDispatcherLink {

Expand All @@ -27,4 +29,8 @@ public String getRequestedHost() {
public int getRequestedPort() {
throw new UnsupportedOperationException();
}

public HttpResponse getResponse() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package com.ibm.wsspi.http;

import java.util.List;

// https://github.com/OpenLiberty/open-liberty/blob/integration/dev/com.ibm.ws.transport.http/src/com/ibm/wsspi/http/HttpResponse.java
public interface HttpResponse {

List<String> getHeaders(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface HttpRequestMessage {

HeaderField getHeader(String paramString);

List<HeaderField> getHeaders(String name);

String getScheme();

String getRequestURI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
package io.opentelemetry.javaagent.instrumentation.liberty.dispatcher;

import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.config.HttpHeadersConfig;
import java.util.List;
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);

public LibertyDispatcherHttpAttributesExtractor() {
super(HttpHeadersConfig.capturedServerHeaders());
}

@Override
protected @Nullable String method(LibertyRequest libertyRequest) {
Expand All @@ -25,6 +27,11 @@ public class LibertyDispatcherHttpAttributesExtractor
return libertyRequest.getHeaderValue("User-Agent");
}

@Override
protected List<String> requestHeader(LibertyRequest libertyRequest, String name) {
return libertyRequest.getHeaderValues(name);
}

@Override
protected @Nullable Long requestContentLength(
LibertyRequest libertyRequest, @Nullable LibertyResponse libertyResponse) {
Expand Down Expand Up @@ -67,6 +74,12 @@ public class LibertyDispatcherHttpAttributesExtractor
return null;
}

@Override
protected List<String> responseHeader(
LibertyRequest libertyRequest, LibertyResponse libertyResponse, String name) {
return libertyResponse.getHeaderValues(name);
}

@Override
protected @Nullable String target(LibertyRequest libertyRequest) {
String requestUri = libertyRequest.getRequestUri();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static void onEnter(

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.This HttpDispatcherLink httpDispatcherLink,
@Advice.Thrown Throwable throwable,
@Advice.Argument(value = 0) StatusCodes statusCode,
@Advice.Argument(value = 2) Exception failure,
Expand All @@ -79,7 +80,7 @@ public static void stopSpan(
}
scope.close();

LibertyResponse response = new LibertyResponse(statusCode);
LibertyResponse response = new LibertyResponse(httpDispatcherLink, statusCode);
request.setCompleted();

Throwable t = failure != null ? failure : throwable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink;
import com.ibm.wsspi.genericbnf.HeaderField;
import com.ibm.wsspi.http.channel.HttpRequestMessage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LibertyRequest {
Expand Down Expand Up @@ -54,6 +56,19 @@ public String getHeaderValue(String name) {
return hf != null ? hf.asString() : null;
}

public List<String> getHeaderValues(String name) {
List<HeaderField> headers = httpRequestMessage.getHeaders(name);
if (headers.isEmpty()) {
return Collections.emptyList();
}
List<String> stringHeaders = new ArrayList<>(headers.size());
int i = 0;
for (HeaderField header : headers) {
stringHeaders.set(i++, header.asString());
}
return stringHeaders;
}

public int peerPort() {
return httpDispatcherLink.getRemotePort();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@

package io.opentelemetry.javaagent.instrumentation.liberty.dispatcher;

import com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink;
import com.ibm.wsspi.http.HttpResponse;
import com.ibm.wsspi.http.channel.values.StatusCodes;
import java.util.Collections;
import java.util.List;

public class LibertyResponse {
private final HttpDispatcherLink httpDispatcherLink;
private final StatusCodes code;

public LibertyResponse(StatusCodes code) {
public LibertyResponse(HttpDispatcherLink httpDispatcherLink, StatusCodes code) {
this.httpDispatcherLink = httpDispatcherLink;
this.code = code;
}

public int getStatus() {
return code.getIntCode();
}

public List<String> getHeaderValues(String name) {
HttpResponse response = httpDispatcherLink.getResponse();
// response is set to null on destroy(), so it shouldn't really ever be null in the middle of
// request processing, but just to be safe let's check it
return response == null ? Collections.emptyList() : response.getHeaders(name);
}
}

0 comments on commit d1a76e8

Please sign in to comment.