Skip to content

Commit

Permalink
Implement ExtendedTextMapGetter in http server instrumentations (#13053)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Jan 20, 2025
1 parent 69bcebd commit b00858f
Show file tree
Hide file tree
Showing 24 changed files with 242 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.api.internal;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import javax.annotation.Nullable;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class EnumerationUtil {

public static <E> Iterator<E> asIterator(@Nullable Enumeration<E> enumeration) {
if (enumeration == null) {
return Collections.emptyIterator();
}
return new Iterator<E>() {
@Override
public boolean hasNext() {
return enumeration.hasMoreElements();
}

@Override
public E next() {
return enumeration.nextElement();
}
};
}

private EnumerationUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

import akka.http.javadsl.model.HttpHeader;
import akka.http.scaladsl.model.HttpRequest;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

enum AkkaHttpServerHeaders implements TextMapGetter<HttpRequest> {
enum AkkaHttpServerHeaders implements ExtendedTextMapGetter<HttpRequest> {
INSTANCE;

@Override
Expand All @@ -27,4 +31,16 @@ public String get(HttpRequest carrier, String key) {
Optional<HttpHeader> header = carrier.getHeader(key);
return header.map(HttpHeader::value).orElse(null);
}

@Override
public Iterator<String> getAll(HttpRequest carrier, String key) {
String headerName = key.toLowerCase(Locale.ROOT);
List<String> result = new ArrayList<>();
for (HttpHeader header : carrier.getHeaders()) {
if (header.is(headerName)) {
result.add(header.value());
}
}
return result.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

import com.linecorp.armeria.server.ServiceRequestContext;
import io.netty.util.AsciiString;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.Collections;
import java.util.Iterator;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

enum RequestContextGetter implements TextMapGetter<ServiceRequestContext> {
enum RequestContextGetter implements ExtendedTextMapGetter<ServiceRequestContext> {
INSTANCE;

@Override
Expand All @@ -33,4 +34,12 @@ public String get(@Nullable ServiceRequestContext carrier, String key) {
}
return carrier.request().headers().get(key);
}

@Override
public Iterator<String> getAll(@Nullable ServiceRequestContext carrier, String key) {
if (carrier == null) {
return Collections.emptyIterator();
}
return carrier.request().headers().valueIterator(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

package io.opentelemetry.javaagent.instrumentation.grizzly;

import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.Iterator;
import org.glassfish.grizzly.http.HttpRequestPacket;

enum HttpRequestHeadersGetter implements TextMapGetter<HttpRequestPacket> {
enum HttpRequestHeadersGetter implements ExtendedTextMapGetter<HttpRequestPacket> {
INSTANCE;

@Override
Expand All @@ -20,4 +21,9 @@ public Iterable<String> keys(HttpRequestPacket request) {
public String get(HttpRequestPacket request, String key) {
return request.getHeader(key);
}

@Override
public Iterator<String> getAll(HttpRequestPacket request, String key) {
return request.getHeaders().values(key).iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

package io.opentelemetry.javaagent.instrumentation.jetty.v12_0;

import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import io.opentelemetry.instrumentation.api.internal.EnumerationUtil;
import java.util.Iterator;
import org.eclipse.jetty.server.Request;

enum Jetty12TextMapGetter implements TextMapGetter<Request> {
enum Jetty12TextMapGetter implements ExtendedTextMapGetter<Request> {
INSTANCE;

@Override
Expand All @@ -20,4 +22,9 @@ public Iterable<String> keys(Request carrier) {
public String get(Request carrier, String key) {
return carrier.getHeaders().get(key);
}

@Override
public Iterator<String> getAll(Request carrier, String key) {
return EnumerationUtil.asIterator(carrier.getHeaders().getValues(key));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
package io.opentelemetry.instrumentation.ktor.v1_0

import io.ktor.request.*
import io.opentelemetry.context.propagation.TextMapGetter
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter
import java.util.Collections

internal object ApplicationRequestGetter : TextMapGetter<ApplicationRequest> {
internal object ApplicationRequestGetter : ExtendedTextMapGetter<ApplicationRequest> {
override fun keys(carrier: ApplicationRequest): Iterable<String> = carrier.headers.names()

override fun get(carrier: ApplicationRequest?, name: String): String? = carrier?.headers?.get(name)

override fun getAll(carrier: ApplicationRequest?, name: String): Iterator<String> = carrier?.headers?.getAll(name)?.iterator() ?: Collections.emptyIterator()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
package io.opentelemetry.instrumentation.ktor.v2_0.common

import io.ktor.server.request.*
import io.opentelemetry.context.propagation.TextMapGetter
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter
import java.util.Collections

internal object ApplicationRequestGetter : TextMapGetter<ApplicationRequest> {
internal object ApplicationRequestGetter : ExtendedTextMapGetter<ApplicationRequest> {
override fun keys(carrier: ApplicationRequest): Iterable<String> = carrier.headers.names()

override fun get(carrier: ApplicationRequest?, name: String): String? = carrier?.headers?.get(name)

override fun getAll(carrier: ApplicationRequest?, name: String): Iterator<String> = carrier?.headers?.getAll(name)?.iterator() ?: Collections.emptyIterator()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

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

import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.Iterator;

enum LibertyDispatcherRequestGetter implements TextMapGetter<LibertyRequest> {
enum LibertyDispatcherRequestGetter implements ExtendedTextMapGetter<LibertyRequest> {
INSTANCE;

@Override
Expand All @@ -19,4 +20,9 @@ public Iterable<String> keys(LibertyRequest carrier) {
public String get(LibertyRequest carrier, String key) {
return carrier.getHeaderValue(key);
}

@Override
public Iterator<String> getAll(LibertyRequest carrier, String key) {
return carrier.getHeaderValues(key).iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

package io.opentelemetry.javaagent.instrumentation.netty.v3_8.server;

import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import io.opentelemetry.javaagent.instrumentation.netty.v3_8.HttpRequestAndChannel;
import java.util.Iterator;
import javax.annotation.Nullable;

enum NettyHeadersGetter implements TextMapGetter<HttpRequestAndChannel> {
enum NettyHeadersGetter implements ExtendedTextMapGetter<HttpRequestAndChannel> {
INSTANCE;

@Override
Expand All @@ -22,4 +23,9 @@ public Iterable<String> keys(HttpRequestAndChannel requestAndChannel) {
public String get(@Nullable HttpRequestAndChannel requestAndChannel, String s) {
return requestAndChannel.request().headers().get(s);
}

@Override
public Iterator<String> getAll(@Nullable HttpRequestAndChannel carrier, String key) {
return carrier.request().headers().getAll(key).iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

package io.opentelemetry.instrumentation.netty.v4.common.internal.server;

import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import io.opentelemetry.instrumentation.netty.v4.common.HttpRequestAndChannel;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public enum HttpRequestHeadersGetter implements TextMapGetter<HttpRequestAndChannel> {
public enum HttpRequestHeadersGetter implements ExtendedTextMapGetter<HttpRequestAndChannel> {
INSTANCE;

@Override
Expand All @@ -26,4 +29,10 @@ public Iterable<String> keys(HttpRequestAndChannel carrier) {
public String get(@Nullable HttpRequestAndChannel carrier, String key) {
return carrier.request().headers().get(key);
}

@Override
public Iterator<String> getAll(@Nullable HttpRequestAndChannel carrier, String key) {
List<String> list = carrier.request().headers().getAll(key);
return list != null ? list.iterator() : Collections.emptyIterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server;

import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.pekko.http.javadsl.model.HttpHeader;
import org.apache.pekko.http.scaladsl.model.HttpRequest;

enum PekkoHttpServerHeaders implements TextMapGetter<HttpRequest> {
enum PekkoHttpServerHeaders implements ExtendedTextMapGetter<HttpRequest> {
INSTANCE;

@Override
Expand All @@ -27,4 +31,16 @@ public String get(HttpRequest carrier, String key) {
Optional<HttpHeader> header = carrier.getHeader(key);
return header.map(HttpHeader::value).orElse(null);
}

@Override
public Iterator<String> getAll(HttpRequest carrier, String key) {
String headerName = key.toLowerCase(Locale.ROOT);
List<String> result = new ArrayList<>();
for (HttpHeader header : carrier.getHeaders()) {
if (header.is(headerName)) {
result.add(header.value());
}
}
return result.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

package io.opentelemetry.instrumentation.ratpack.v1_7.internal;

import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.Collections;
import java.util.Iterator;
import javax.annotation.Nullable;
import ratpack.http.Request;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
enum RatpackGetter implements TextMapGetter<Request> {
enum RatpackGetter implements ExtendedTextMapGetter<Request> {
INSTANCE;

@Override
Expand All @@ -29,4 +31,12 @@ public String get(@Nullable Request request, String key) {
}
return request.getHeaders().get(key);
}

@Override
public Iterator<String> getAll(@Nullable Request request, String key) {
if (request == null) {
return Collections.emptyIterator();
}
return request.getHeaders().getAll(key).iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

package io.opentelemetry.instrumentation.restlet.v1_1.internal;

import io.opentelemetry.context.propagation.TextMapGetter;
import java.util.Locale;
import io.opentelemetry.context.propagation.internal.ExtendedTextMapGetter;
import java.util.Iterator;
import java.util.stream.Collectors;
import org.restlet.data.Form;
import org.restlet.data.Message;
import org.restlet.data.Parameter;
import org.restlet.data.Request;

enum RestletHeadersGetter implements TextMapGetter<Request> {
enum RestletHeadersGetter implements ExtendedTextMapGetter<Request> {
INSTANCE;

@Override
Expand All @@ -21,14 +23,17 @@ public Iterable<String> keys(Request carrier) {

@Override
public String get(Request carrier, String key) {

Form headers = getHeaders(carrier);
return headers.getFirstValue(key, true);
}

String value = headers.getFirstValue(key);
if (value != null) {
return value;
}
return headers.getFirstValue(key.toLowerCase(Locale.ROOT));
@Override
public Iterator<String> getAll(Request carrier, String key) {
Form headers = getHeaders(carrier);
return headers.subList(key, true).stream()
.map(Parameter::getValue)
.collect(Collectors.toList())
.iterator();
}

static Form getHeaders(Message carrier) {
Expand Down
Loading

0 comments on commit b00858f

Please sign in to comment.