|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.javahttpserver; |
| 7 | + |
| 8 | +import com.sun.net.httpserver.HttpExchange; |
| 9 | +import com.sun.net.httpserver.HttpsExchange; |
| 10 | +import io.opentelemetry.instrumentation.api.internal.HttpProtocolUtil; |
| 11 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesGetter; |
| 12 | +import java.net.InetSocketAddress; |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import javax.annotation.Nullable; |
| 16 | + |
| 17 | +enum JavaHttpServerAttributesGetter |
| 18 | + implements HttpServerAttributesGetter<HttpExchange, HttpExchange> { |
| 19 | + INSTANCE; |
| 20 | + |
| 21 | + @Override |
| 22 | + public String getHttpRequestMethod(HttpExchange exchange) { |
| 23 | + return exchange.getRequestMethod(); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public String getUrlScheme(HttpExchange exchange) { |
| 28 | + return exchange instanceof HttpsExchange ? "https" : "http"; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public String getUrlPath(HttpExchange exchange) { |
| 33 | + return exchange.getRequestURI().getPath(); |
| 34 | + } |
| 35 | + |
| 36 | + @Nullable |
| 37 | + @Override |
| 38 | + public String getUrlQuery(HttpExchange exchange) { |
| 39 | + return exchange.getRequestURI().getQuery(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public List<String> getHttpRequestHeader(HttpExchange exchange, String name) { |
| 44 | + return exchange.getRequestHeaders().getOrDefault(name, Collections.emptyList()); |
| 45 | + } |
| 46 | + |
| 47 | + @Nullable |
| 48 | + @Override |
| 49 | + public Integer getHttpResponseStatusCode( |
| 50 | + HttpExchange exchange, @Nullable HttpExchange res, @Nullable Throwable error) { |
| 51 | + int status = exchange.getResponseCode(); |
| 52 | + return status != -1 ? status : null; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public List<String> getHttpResponseHeader( |
| 57 | + HttpExchange exchange, @Nullable HttpExchange res, String name) { |
| 58 | + return exchange.getResponseHeaders().getOrDefault(name, Collections.emptyList()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getHttpRoute(HttpExchange exchange) { |
| 63 | + return exchange.getHttpContext().getPath(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String getNetworkProtocolName(HttpExchange exchange, @Nullable HttpExchange res) { |
| 68 | + return HttpProtocolUtil.getProtocol(exchange.getProtocol()); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String getNetworkProtocolVersion(HttpExchange exchange, @Nullable HttpExchange res) { |
| 73 | + return HttpProtocolUtil.getVersion(exchange.getProtocol()); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public InetSocketAddress getNetworkPeerInetSocketAddress( |
| 78 | + HttpExchange exchange, @Nullable HttpExchange res) { |
| 79 | + return exchange.getRemoteAddress(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public InetSocketAddress getNetworkLocalInetSocketAddress( |
| 84 | + HttpExchange exchange, @Nullable HttpExchange res) { |
| 85 | + return exchange.getLocalAddress(); |
| 86 | + } |
| 87 | +} |
0 commit comments