|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright The OpenTelemetry Authors | 
|  | 3 | + * SPDX-License-Identifier: Apache-2.0 | 
|  | 4 | + */ | 
|  | 5 | + | 
|  | 6 | +package io.opentelemetry.instrumentation.apachehttpclient.v5_2; | 
|  | 7 | + | 
|  | 8 | +import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter; | 
|  | 9 | +import java.util.ArrayList; | 
|  | 10 | +import java.util.Collections; | 
|  | 11 | +import java.util.List; | 
|  | 12 | +import javax.annotation.Nullable; | 
|  | 13 | +import org.apache.hc.core5.http.Header; | 
|  | 14 | +import org.apache.hc.core5.http.HttpResponse; | 
|  | 15 | +import org.apache.hc.core5.http.MessageHeaders; | 
|  | 16 | +import org.apache.hc.core5.http.ProtocolVersion; | 
|  | 17 | + | 
|  | 18 | +enum ApacheHttpClientHttpAttributesGetter | 
|  | 19 | +    implements HttpClientAttributesGetter<ApacheHttpClientRequest, HttpResponse> { | 
|  | 20 | +  INSTANCE; | 
|  | 21 | + | 
|  | 22 | +  @Override | 
|  | 23 | +  public String getHttpRequestMethod(ApacheHttpClientRequest request) { | 
|  | 24 | +    return request.getMethod(); | 
|  | 25 | +  } | 
|  | 26 | + | 
|  | 27 | +  @Override | 
|  | 28 | +  @Nullable | 
|  | 29 | +  public String getUrlFull(ApacheHttpClientRequest request) { | 
|  | 30 | +    return request.getUrl(); | 
|  | 31 | +  } | 
|  | 32 | + | 
|  | 33 | +  @Override | 
|  | 34 | +  public List<String> getHttpRequestHeader(ApacheHttpClientRequest request, String name) { | 
|  | 35 | +    return getHeader(request, name); | 
|  | 36 | +  } | 
|  | 37 | + | 
|  | 38 | +  @Override | 
|  | 39 | +  public Integer getHttpResponseStatusCode( | 
|  | 40 | +      ApacheHttpClientRequest request, HttpResponse response, @Nullable Throwable error) { | 
|  | 41 | +    return response.getCode(); | 
|  | 42 | +  } | 
|  | 43 | + | 
|  | 44 | +  @Override | 
|  | 45 | +  public List<String> getHttpResponseHeader( | 
|  | 46 | +      ApacheHttpClientRequest request, HttpResponse response, String name) { | 
|  | 47 | +    return getHeader(response, name); | 
|  | 48 | +  } | 
|  | 49 | + | 
|  | 50 | +  private static List<String> getHeader(MessageHeaders messageHeaders, String name) { | 
|  | 51 | +    return headersToList(messageHeaders.getHeaders(name)); | 
|  | 52 | +  } | 
|  | 53 | + | 
|  | 54 | +  private static List<String> getHeader(ApacheHttpClientRequest messageHeaders, String name) { | 
|  | 55 | +    return headersToList(messageHeaders.getDelegate().getHeaders(name)); | 
|  | 56 | +  } | 
|  | 57 | + | 
|  | 58 | +  // minimize memory overhead by not using streams | 
|  | 59 | +  private static List<String> headersToList(Header[] headers) { | 
|  | 60 | +    if (headers.length == 0) { | 
|  | 61 | +      return Collections.emptyList(); | 
|  | 62 | +    } | 
|  | 63 | +    List<String> headersList = new ArrayList<>(headers.length); | 
|  | 64 | +    for (Header header : headers) { | 
|  | 65 | +      headersList.add(header.getValue()); | 
|  | 66 | +    } | 
|  | 67 | +    return headersList; | 
|  | 68 | +  } | 
|  | 69 | + | 
|  | 70 | +  @Nullable | 
|  | 71 | +  @Override | 
|  | 72 | +  public String getNetworkProtocolName( | 
|  | 73 | +      ApacheHttpClientRequest request, @Nullable HttpResponse response) { | 
|  | 74 | +    ProtocolVersion protocolVersion = getVersion(request, response); | 
|  | 75 | +    if (protocolVersion == null) { | 
|  | 76 | +      return null; | 
|  | 77 | +    } | 
|  | 78 | +    return protocolVersion.getProtocol(); | 
|  | 79 | +  } | 
|  | 80 | + | 
|  | 81 | +  @Nullable | 
|  | 82 | +  @Override | 
|  | 83 | +  public String getNetworkProtocolVersion( | 
|  | 84 | +      ApacheHttpClientRequest request, @Nullable HttpResponse response) { | 
|  | 85 | +    ProtocolVersion protocolVersion = getVersion(request, response); | 
|  | 86 | +    if (protocolVersion == null) { | 
|  | 87 | +      return null; | 
|  | 88 | +    } | 
|  | 89 | +    if (protocolVersion.getMinor() == 0) { | 
|  | 90 | +      return Integer.toString(protocolVersion.getMajor()); | 
|  | 91 | +    } | 
|  | 92 | +    return protocolVersion.getMajor() + "." + protocolVersion.getMinor(); | 
|  | 93 | +  } | 
|  | 94 | + | 
|  | 95 | +  @Override | 
|  | 96 | +  @Nullable | 
|  | 97 | +  public String getServerAddress(ApacheHttpClientRequest request) { | 
|  | 98 | +    return request.getDelegate().getAuthority().getHostName(); | 
|  | 99 | +  } | 
|  | 100 | + | 
|  | 101 | +  @Override | 
|  | 102 | +  public Integer getServerPort(ApacheHttpClientRequest request) { | 
|  | 103 | +    return request.getDelegate().getAuthority().getPort(); | 
|  | 104 | +  } | 
|  | 105 | + | 
|  | 106 | +  private static ProtocolVersion getVersion( | 
|  | 107 | +      ApacheHttpClientRequest request, @Nullable HttpResponse response) { | 
|  | 108 | +    ProtocolVersion protocolVersion = request.getDelegate().getVersion(); | 
|  | 109 | +    if (protocolVersion == null && response != null) { | 
|  | 110 | +      protocolVersion = response.getVersion(); | 
|  | 111 | +    } | 
|  | 112 | +    return protocolVersion; | 
|  | 113 | +  } | 
|  | 114 | +} | 
0 commit comments