+ * On the client-side, a factory is created for each call, and the factory creates a stream tracer for each attempt.
+ *
+ * Note: This class uses experimental grpc-java-API features.
+ */
+public final class MetricsClientStreamTracers {
+
+ private MetricsClientStreamTracers() {}
+
+ private static final class ClientTracer extends ClientStreamTracer {
+ private final CallAttemptsTracerFactory attemptsState;
+ private final StreamInfo info;
+ private final String fullMethodName;
+
+ ClientTracer(CallAttemptsTracerFactory attemptsState, StreamInfo info, String fullMethodName) {
+ this.attemptsState = attemptsState;
+ this.info = info;
+ this.fullMethodName = fullMethodName;
+ }
+
+ }
+
+ static final class CallAttemptsTracerFactory extends ClientStreamTracer.Factory {
+ private final String fullMethodName;
+ private final MetricsMeters metricsMeters;
+ private boolean attemptRecorded;
+
+ CallAttemptsTracerFactory(String fullMethodName, MetricsMeters metricsMeters) {
+ this.fullMethodName = checkNotNull(fullMethodName, "fullMethodName");
+ this.metricsMeters = checkNotNull(metricsMeters, "metricsMeters");
+
+ // Record here in case newClientStreamTracer() would never be called.
+ this.metricsMeters.getAttemptCounter()
+ .withTags(Tags.of("grpc.method", fullMethodName))
+ .increment();
+ this.attemptRecorded = true;
+ }
+
+ @Override
+ public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata metadata) {
+ if (!this.attemptRecorded) {
+ this.metricsMeters.getAttemptCounter()
+ .withTags((Tags.of("grpc.method", fullMethodName)))
+ .increment();
+ } else {
+ this.attemptRecorded = false;
+ }
+ return new ClientTracer(this, info, fullMethodName);
+ }
+
+ }
+}
diff --git a/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/metrics/MetricsMeters.java b/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/metrics/MetricsMeters.java
new file mode 100644
index 000000000..728a7ccad
--- /dev/null
+++ b/grpc-client-spring-boot-autoconfigure/src/main/java/net/devh/boot/grpc/client/metrics/MetricsMeters.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2016-2023 The gRPC-Spring Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.devh.boot.grpc.client.metrics;
+
+import io.micrometer.core.instrument.Counter;
+import io.micrometer.core.instrument.Meter.MeterProvider;
+
+/*
+ * Collection of metrics meters.
+ */
+public class MetricsMeters {
+
+ private MeterProvider