|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.google.api.gax.tracing; |
| 32 | + |
| 33 | +import com.google.api.core.BetaApi; |
| 34 | +import com.google.api.core.InternalApi; |
| 35 | +import com.google.api.gax.core.GaxProperties; |
| 36 | +import com.google.common.annotations.VisibleForTesting; |
| 37 | +import com.google.common.base.Preconditions; |
| 38 | +import io.opentelemetry.api.OpenTelemetry; |
| 39 | +import io.opentelemetry.api.common.Attributes; |
| 40 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 41 | +import io.opentelemetry.api.metrics.DoubleHistogram; |
| 42 | +import io.opentelemetry.api.metrics.LongCounter; |
| 43 | +import io.opentelemetry.api.metrics.Meter; |
| 44 | +import java.util.Map; |
| 45 | + |
| 46 | +/** |
| 47 | + * OpenTelemetry implementation of recording metrics. This implementation collections the |
| 48 | + * measurements related to the lifecyle of an RPC. |
| 49 | + * |
| 50 | + * <p>For the Otel implementation, an attempt is a single RPC invocation and an operation is the |
| 51 | + * collection of all the attempts made before a response is returned (either as a success or an |
| 52 | + * error). A single call (i.e. `EchoClient.echo()`) should have an operation_count of 1 and may have |
| 53 | + * an attempt_count of 1+ (depending on the retry configurations). |
| 54 | + */ |
| 55 | +@BetaApi |
| 56 | +@InternalApi |
| 57 | +public class OpenTelemetryMetricsRecorder implements MetricsRecorder { |
| 58 | + private final DoubleHistogram attemptLatencyRecorder; |
| 59 | + private final DoubleHistogram operationLatencyRecorder; |
| 60 | + private final LongCounter operationCountRecorder; |
| 61 | + private final LongCounter attemptCountRecorder; |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates the following instruments for the following metrics: |
| 65 | + * |
| 66 | + * <ul> |
| 67 | + * <li>Attempt Latency: Histogram |
| 68 | + * <li>Operation Latency: Histogram |
| 69 | + * <li>Attempt Count: Counter |
| 70 | + * <li>Operation Count: Counter |
| 71 | + * </ul> |
| 72 | + * |
| 73 | + * @param openTelemetry OpenTelemetry instance |
| 74 | + * @param serviceName Service Name |
| 75 | + */ |
| 76 | + public OpenTelemetryMetricsRecorder(OpenTelemetry openTelemetry, String serviceName) { |
| 77 | + Meter meter = |
| 78 | + openTelemetry |
| 79 | + .meterBuilder("gax-java") |
| 80 | + .setInstrumentationVersion(GaxProperties.getGaxVersion()) |
| 81 | + .build(); |
| 82 | + this.attemptLatencyRecorder = |
| 83 | + meter |
| 84 | + .histogramBuilder(serviceName + "/attempt_latency") |
| 85 | + .setDescription("Time an individual attempt took") |
| 86 | + .setUnit("ms") |
| 87 | + .build(); |
| 88 | + this.operationLatencyRecorder = |
| 89 | + meter |
| 90 | + .histogramBuilder(serviceName + "/operation_latency") |
| 91 | + .setDescription( |
| 92 | + "Total time until final operation success or failure, including retries and backoff.") |
| 93 | + .setUnit("ms") |
| 94 | + .build(); |
| 95 | + this.attemptCountRecorder = |
| 96 | + meter |
| 97 | + .counterBuilder(serviceName + "/attempt_count") |
| 98 | + .setDescription("Number of Attempts") |
| 99 | + .setUnit("1") |
| 100 | + .build(); |
| 101 | + this.operationCountRecorder = |
| 102 | + meter |
| 103 | + .counterBuilder(serviceName + "/operation_count") |
| 104 | + .setDescription("Number of Operations") |
| 105 | + .setUnit("1") |
| 106 | + .build(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Record the latency for an individual attempt. Data is stored in a Histogram. |
| 111 | + * |
| 112 | + * @param attemptLatency Attempt Latency in ms |
| 113 | + * @param attributes Map of the attributes to store |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public void recordAttemptLatency(double attemptLatency, Map<String, String> attributes) { |
| 117 | + attemptLatencyRecorder.record(attemptLatency, toOtelAttributes(attributes)); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Record an attempt made. The attempt count number is stored in a LongCounter. |
| 122 | + * |
| 123 | + * <p>The count should be set as 1 every time this is invoked (each retry attempt) |
| 124 | + * |
| 125 | + * @param count The number of attempts made |
| 126 | + * @param attributes Map of the attributes to store |
| 127 | + */ |
| 128 | + @Override |
| 129 | + public void recordAttemptCount(long count, Map<String, String> attributes) { |
| 130 | + attemptCountRecorder.add(count, toOtelAttributes(attributes)); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Record the latency for the entire operation. This is the latency for the entire RPC, including |
| 135 | + * all the retry attempts |
| 136 | + * |
| 137 | + * @param operationLatency Operation Latency in ms |
| 138 | + * @param attributes Map of the attributes to store |
| 139 | + */ |
| 140 | + @Override |
| 141 | + public void recordOperationLatency(double operationLatency, Map<String, String> attributes) { |
| 142 | + operationLatencyRecorder.record(operationLatency, toOtelAttributes(attributes)); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Record an operation made. The operation count number is stored in a LongCounter. |
| 147 | + * |
| 148 | + * <p>The operation count should always be 1 and this should be invoked once. |
| 149 | + * |
| 150 | + * @param count The number of operations made |
| 151 | + * @param attributes Map of the attributes to store |
| 152 | + */ |
| 153 | + @Override |
| 154 | + public void recordOperationCount(long count, Map<String, String> attributes) { |
| 155 | + operationCountRecorder.add(count, toOtelAttributes(attributes)); |
| 156 | + } |
| 157 | + |
| 158 | + @VisibleForTesting |
| 159 | + Attributes toOtelAttributes(Map<String, String> attributes) { |
| 160 | + Preconditions.checkNotNull(attributes, "Attributes map cannot be null"); |
| 161 | + AttributesBuilder attributesBuilder = Attributes.builder(); |
| 162 | + attributes.forEach(attributesBuilder::put); |
| 163 | + return attributesBuilder.build(); |
| 164 | + } |
| 165 | +} |
0 commit comments