io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter Maven / Gradle / Ivy
Show all versions of opentelemetry-exporter-otlp Show documentation
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.otlp.metrics;
import io.opentelemetry.exporter.internal.grpc.GrpcExporter;
import io.opentelemetry.exporter.internal.grpc.GrpcExporterBuilder;
import io.opentelemetry.exporter.internal.marshal.Marshaler;
import io.opentelemetry.exporter.internal.otlp.metrics.LowAllocationMetricsRequestMarshaler;
import io.opentelemetry.exporter.internal.otlp.metrics.MetricsRequestMarshaler;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.export.MemoryMode;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Deque;
import java.util.StringJoiner;
import javax.annotation.concurrent.ThreadSafe;
/**
* Exports metrics using OTLP via gRPC, using OpenTelemetry's protobuf model.
*
* @since 1.14.0
*/
@ThreadSafe
public final class OtlpGrpcMetricExporter implements MetricExporter {
private final Deque marshalerPool = new ArrayDeque<>();
private final GrpcExporterBuilder builder;
private final GrpcExporter delegate;
private final AggregationTemporalitySelector aggregationTemporalitySelector;
private final DefaultAggregationSelector defaultAggregationSelector;
private final MemoryMode memoryMode;
/**
* Returns a new {@link OtlpGrpcMetricExporter} using the default values.
*
* To load configuration values from environment variables and system properties, use opentelemetry-sdk-extension-autoconfigure.
*
* @return a new {@link OtlpGrpcMetricExporter} instance.
*/
public static OtlpGrpcMetricExporter getDefault() {
return builder().build();
}
/**
* Returns a new builder instance for this exporter.
*
* @return a new builder instance for this exporter.
*/
public static OtlpGrpcMetricExporterBuilder builder() {
return new OtlpGrpcMetricExporterBuilder();
}
OtlpGrpcMetricExporter(
GrpcExporterBuilder builder,
GrpcExporter delegate,
AggregationTemporalitySelector aggregationTemporalitySelector,
DefaultAggregationSelector defaultAggregationSelector,
MemoryMode memoryMode) {
this.builder = builder;
this.delegate = delegate;
this.aggregationTemporalitySelector = aggregationTemporalitySelector;
this.defaultAggregationSelector = defaultAggregationSelector;
this.memoryMode = memoryMode;
}
/**
* Returns a builder with configuration values equal to those for this exporter.
*
* IMPORTANT: Be sure to {@link #shutdown()} this instance if it will no longer be used.
*
* @since 1.29.0
*/
public OtlpGrpcMetricExporterBuilder toBuilder() {
return new OtlpGrpcMetricExporterBuilder(builder.copy(), memoryMode);
}
@Override
public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) {
return aggregationTemporalitySelector.getAggregationTemporality(instrumentType);
}
@Override
public Aggregation getDefaultAggregation(InstrumentType instrumentType) {
return defaultAggregationSelector.getDefaultAggregation(instrumentType);
}
@Override
public MemoryMode getMemoryMode() {
return memoryMode;
}
/**
* Submits all the given metrics in a single batch to the OpenTelemetry collector.
*
* @param metrics the list of Metrics to be exported.
* @return the result of the operation
*/
@Override
public CompletableResultCode export(Collection metrics) {
if (memoryMode == MemoryMode.REUSABLE_DATA) {
LowAllocationMetricsRequestMarshaler marshaler = marshalerPool.poll();
if (marshaler == null) {
marshaler = new LowAllocationMetricsRequestMarshaler();
}
LowAllocationMetricsRequestMarshaler exportMarshaler = marshaler;
exportMarshaler.initialize(metrics);
return delegate
.export(exportMarshaler, metrics.size())
.whenComplete(
() -> {
exportMarshaler.reset();
marshalerPool.add(exportMarshaler);
});
}
// MemoryMode == MemoryMode.IMMUTABLE_DATA
MetricsRequestMarshaler request = MetricsRequestMarshaler.create(metrics);
return delegate.export(request, metrics.size());
}
/**
* The OTLP exporter does not batch metrics, so this method will immediately return with success.
*
* @return always Success
*/
@Override
public CompletableResultCode flush() {
return CompletableResultCode.ofSuccess();
}
/**
* Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately
* cancelled. The channel is forcefully closed after a timeout.
*/
@Override
public CompletableResultCode shutdown() {
return delegate.shutdown();
}
@Override
public String toString() {
StringJoiner joiner = new StringJoiner(", ", "OtlpGrpcMetricExporter{", "}");
joiner.add(builder.toString(false));
joiner.add(
"aggregationTemporalitySelector="
+ AggregationTemporalitySelector.asString(aggregationTemporalitySelector));
joiner.add(
"defaultAggregationSelector="
+ DefaultAggregationSelector.asString(defaultAggregationSelector));
joiner.add("memoryMode=" + memoryMode);
return joiner.toString();
}
}