|
| 1 | +package io.opentelemetry.instrumentation.awssdk.v2_2.internal; |
| 2 | + |
| 3 | +import io.opentelemetry.api.metrics.Meter; |
| 4 | +import java.util.function.BiFunction; |
| 5 | +import software.amazon.awssdk.core.metrics.CoreMetric; |
| 6 | +import software.amazon.awssdk.http.HttpMetric; |
| 7 | + |
| 8 | +/** |
| 9 | + * Catalogue of AWS-SDK metric definitions that this instrumentation recognizes. |
| 10 | + * <p> |
| 11 | + * Each enum constant knows: (1) the SDK metric identifier, (2) the scope in the |
| 12 | + * request/attempt/http hierarchy, and (3) how to build the {@link MetricStrategy} |
| 13 | + * that records the metric. |
| 14 | + */ |
| 15 | +public enum MetricSpec { |
| 16 | + // per-request metrics |
| 17 | + API_CALL_DURATION( |
| 18 | + CoreMetric.API_CALL_DURATION.name(), |
| 19 | + Scope.REQUEST, |
| 20 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "api_call_duration", "The total time taken to finish a request (inclusive of all retries)") |
| 21 | + ), |
| 22 | + CREDENTIALS_FETCH_DURATION( |
| 23 | + CoreMetric.CREDENTIALS_FETCH_DURATION.name(), |
| 24 | + Scope.REQUEST, |
| 25 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "credentials_fetch_duration", "Time taken to fetch AWS signing credentials for the request") |
| 26 | + ), |
| 27 | + ENDPOINT_RESOLVE_DURATION( |
| 28 | + CoreMetric.ENDPOINT_RESOLVE_DURATION.name(), |
| 29 | + Scope.REQUEST, |
| 30 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "endpoint_resolve_duration", "Time it took to resolve the endpoint used for the API call") |
| 31 | + ), |
| 32 | + MARSHALLING_DURATION( |
| 33 | + CoreMetric.MARSHALLING_DURATION.name(), |
| 34 | + Scope.REQUEST, |
| 35 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "marshalling_duration", "Time it takes to marshall an SDK request to an HTTP request") |
| 36 | + ), |
| 37 | + TOKEN_FETCH_DURATION( |
| 38 | + CoreMetric.TOKEN_FETCH_DURATION.name(), |
| 39 | + Scope.REQUEST, |
| 40 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "token_fetch_duration", "Time taken to fetch token signing credentials for the request") |
| 41 | + ), |
| 42 | + |
| 43 | + // per-attempt metrics |
| 44 | + BACKOFF_DELAY_DURATION( |
| 45 | + CoreMetric.BACKOFF_DELAY_DURATION.name(), |
| 46 | + Scope.ATTEMPT, |
| 47 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "backoff_delay_duration", "Duration of time the SDK waited before this API call attempt") |
| 48 | + ), |
| 49 | + READ_THROUGHPUT( |
| 50 | + CoreMetric.READ_THROUGHPUT.name(), |
| 51 | + Scope.ATTEMPT, |
| 52 | + (meter, metricPrefix) -> new DoubleHistogramStrategy(meter, metricPrefix + "read_throughput", "Read throughput of the client in bytes/second") |
| 53 | + ), |
| 54 | + SERVICE_CALL_DURATION( |
| 55 | + CoreMetric.SERVICE_CALL_DURATION.name(), |
| 56 | + Scope.ATTEMPT, |
| 57 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "service_call_duration", "Time to connect, send the request and receive the HTTP status code and header") |
| 58 | + ), |
| 59 | + SIGNING_DURATION( |
| 60 | + CoreMetric.SIGNING_DURATION.name(), |
| 61 | + Scope.ATTEMPT, |
| 62 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "signing_duration", "Time it takes to sign the HTTP request") |
| 63 | + ), |
| 64 | + TIME_TO_FIRST_BYTE( |
| 65 | + CoreMetric.TIME_TO_FIRST_BYTE.name(), |
| 66 | + Scope.ATTEMPT, |
| 67 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "time_to_first_byte", "Elapsed time from sending the HTTP request to receiving the first byte of the headers") |
| 68 | + ), |
| 69 | + TIME_TO_LAST_BYTE( |
| 70 | + CoreMetric.TIME_TO_LAST_BYTE.name(), |
| 71 | + Scope.ATTEMPT, |
| 72 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "time_to_last_byte", "Elapsed time from sending the HTTP request to receiving the last byte of the response") |
| 73 | + ), |
| 74 | + UNMARSHALLING_DURATION( |
| 75 | + CoreMetric.UNMARSHALLING_DURATION.name(), |
| 76 | + Scope.ATTEMPT, |
| 77 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "unmarshalling_duration", "Time it takes to unmarshall an HTTP response to an SDK response") |
| 78 | + ), |
| 79 | + |
| 80 | + // HTTP metrics |
| 81 | + AVAILABLE_CONCURRENCY( |
| 82 | + HttpMetric.AVAILABLE_CONCURRENCY.name(), |
| 83 | + Scope.HTTP, |
| 84 | + (meter, metricPrefix) -> new LongHistogramStrategy(meter, metricPrefix + "available_concurrency", "Remaining concurrent requests that can be supported without a new connection") |
| 85 | + ), |
| 86 | + CONCURRENCY_ACQUIRE_DURATION( |
| 87 | + HttpMetric.CONCURRENCY_ACQUIRE_DURATION.name(), |
| 88 | + Scope.HTTP, |
| 89 | + (meter, metricPrefix) -> new DurationStrategy(meter, metricPrefix + "concurrency_acquire_duration", "Time taken to acquire a channel from the connection pool") |
| 90 | + ), |
| 91 | + LEASED_CONCURRENCY( |
| 92 | + HttpMetric.LEASED_CONCURRENCY.name(), |
| 93 | + Scope.HTTP, |
| 94 | + (meter, metricPrefix) -> new LongHistogramStrategy(meter, metricPrefix + "leased_concurrency", "Number of requests currently being executed by the HTTP client") |
| 95 | + ), |
| 96 | + MAX_CONCURRENCY( |
| 97 | + HttpMetric.MAX_CONCURRENCY.name(), |
| 98 | + Scope.HTTP, |
| 99 | + (meter, metricPrefix) -> new LongHistogramStrategy(meter, metricPrefix + "max_concurrency", "Maximum number of concurrent requests supported by the HTTP client") |
| 100 | + ), |
| 101 | + PENDING_CONCURRENCY_ACQUIRES( |
| 102 | + HttpMetric.PENDING_CONCURRENCY_ACQUIRES.name(), |
| 103 | + Scope.HTTP, |
| 104 | + (meter, metricPrefix) -> new LongHistogramStrategy(meter, metricPrefix + "pending_concurrency_acquires", "Number of requests waiting for a connection or stream to be available") |
| 105 | + ); |
| 106 | + |
| 107 | + private final String sdkMetricName; |
| 108 | + private final Scope scope; |
| 109 | + private final BiFunction<Meter, String, MetricStrategy> strategyFactory; |
| 110 | + |
| 111 | + MetricSpec(String sdkMetricName, Scope scope, BiFunction<Meter, String, MetricStrategy> strategyFactory) { |
| 112 | + this.sdkMetricName = sdkMetricName; |
| 113 | + this.scope = scope; |
| 114 | + this.strategyFactory = strategyFactory; |
| 115 | + } |
| 116 | + |
| 117 | + public String getSdkMetricName() { |
| 118 | + return sdkMetricName; |
| 119 | + } |
| 120 | + |
| 121 | + public Scope getScope() { |
| 122 | + return scope; |
| 123 | + } |
| 124 | + |
| 125 | + /** Create a {@link MetricStrategy} for this metric. */ |
| 126 | + public MetricStrategy create(Meter meter, String metricPrefix) { |
| 127 | + return strategyFactory.apply(meter, metricPrefix); |
| 128 | + } |
| 129 | + |
| 130 | + /** Denotes where in the AWS-SDK metric hierarchy the metric lives. */ |
| 131 | + public enum Scope { REQUEST, ATTEMPT, HTTP } |
| 132 | +} |
0 commit comments