Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system/env vars and properties configuration for Metric classes #1168

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@
import io.grpc.ManagedChannel;
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
import io.opentelemetry.proto.collector.metrics.v1.MetricsServiceGrpc;
import io.opentelemetry.sdk.common.export.ConfigBuilder;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.concurrent.ThreadSafe;

/** Exports spans using OTLP via gRPC, using OpenTelemetry's protobuf model. */
/** Exports metrics using OTLP via gRPC, using OpenTelemetry's protobuf model. */
@ThreadSafe
public final class OtlpGrpcMetricExporter implements MetricExporter {
private static final Logger logger = Logger.getLogger(OtlpGrpcMetricExporter.class.getName());
Expand All @@ -37,11 +40,11 @@ public final class OtlpGrpcMetricExporter implements MetricExporter {
private final long deadlineMs;

/**
* Creates a new Jaeger gRPC Metric Reporter with the given name, using the given channel.
* Creates a new OTLP gRPC Metric Reporter with the given name, using the given channel.
*
* @param channel the channel to use when communicating with the Jaeger Collector.
* @param deadlineMs max waiting time for the collector to process each span batch. When set to 0
* or to a negative value, the exporter will wait indefinitely.
* @param channel the channel to use when communicating with the OpenTelemetry Collector.
* @param deadlineMs max waiting time for the collector to process each metric batch. When set to
* 0 or to a negative value, the exporter will wait indefinitely.
*/
private OtlpGrpcMetricExporter(ManagedChannel channel, long deadlineMs) {
this.managedChannel = channel;
Expand All @@ -50,7 +53,7 @@ private OtlpGrpcMetricExporter(ManagedChannel channel, long deadlineMs) {
}

/**
* Submits all the given spans in a single batch to the Jaeger collector.
* 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
Expand Down Expand Up @@ -96,6 +99,18 @@ public static Builder newBuilder() {
return new Builder();
}

/**
* Returns a new {@link OtlpGrpcMetricExporter} reading the configuration values from the
* environment and from system properties. System properties override values defined in the
* environment. If a configuration value is missing, it uses the default value.
*
* @return a new {@link OtlpGrpcMetricExporter} instance.
* @since 0.5.0
*/
public static OtlpGrpcMetricExporter getDefault() {
return newBuilder().readEnvironment().readSystemProperties().build();
}

/**
* Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately
* cancelled. The channel is forcefully closed after a timeout.
Expand All @@ -110,7 +125,9 @@ public void shutdown() {
}

/** Builder utility for this exporter. */
public static class Builder {
public static class Builder extends ConfigBuilder<Builder> {
private static final String KEY_METRIC_TIMEOUT = "otel.otlp.metric.timeout";

private ManagedChannel channel;
private long deadlineMs = 1_000; // 1 second

Expand All @@ -126,7 +143,7 @@ public Builder setChannel(ManagedChannel channel) {
}

/**
* Sets the max waiting time for the collector to process each span batch. Optional.
* Sets the max waiting time for the collector to process each metric batch. Optional.
*
* @param deadlineMs the max waiting time
* @return this builder's instance
Expand All @@ -146,5 +163,77 @@ public OtlpGrpcMetricExporter build() {
}

private Builder() {}

/**
* Sets the configuration values from the given configuration map for only the available keys.
* This method looks for the following keys:
*
* <ul>
* <li>{@code otel.otlp.metric.timeout}: to set the max waiting time for the collector to
* process each metric batch.
* </ul>
*
* @param configMap {@link Map} holding the configuration values.
* @return this.
*/
@Override
protected Builder fromConfigMap(
Map<String, String> configMap, NamingConvention namingConvention) {
configMap = namingConvention.normalize(configMap);
Long value = getLongProperty(KEY_METRIC_TIMEOUT, configMap);
if (value != null) {
this.setDeadlineMs(value);
}
return this;
}

/**
* Sets the configuration values from the given properties object for only the available keys.
* This method looks for the following keys:
*
* <ul>
* <li>{@code otel.otlp.metric.timeout}: to set the max waiting time for the collector to
* process each metric batch.
* </ul>
*
* @param properties {@link Properties} holding the configuration values.
* @return this.
*/
@Override
public Builder readProperties(Properties properties) {
jkwatson marked this conversation as resolved.
Show resolved Hide resolved
return super.readProperties(properties);
}

/**
* Sets the configuration values from environment variables for only the available keys. This
* method looks for the following keys:
*
* <ul>
* <li>{@code OTEL_OTLP_METRIC_TIMEOUT}: to set the max waiting time for the collector to
* process each metric batch.
* </ul>
*
* @return this.
*/
@Override
public Builder readEnvironment() {
return super.readEnvironment();
}

/**
* Sets the configuration values from system properties for only the available keys. This method
* looks for the following keys:
*
* <ul>
* <li>{@code otel.otlp.metric.timeout}: to set the max waiting time for the collector to
* process each metric batch.
* </ul>
*
* @return this.
*/
@Override
public Builder readSystemProperties() {
return super.readSystemProperties();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import io.grpc.ManagedChannel;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.proto.collector.trace.v1.TraceServiceGrpc;
import io.opentelemetry.sdk.common.export.ConfigBuilder;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.util.Collection;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -37,9 +40,9 @@ public final class OtlpGrpcSpanExporter implements SpanExporter {
private final long deadlineMs;

/**
* Creates a new Jaeger gRPC Span Reporter with the given name, using the given channel.
* Creates a new OTLP gRPC Span Reporter with the given name, using the given channel.
*
* @param channel the channel to use when communicating with the Jaeger Collector.
* @param channel the channel to use when communicating with the OpenTelemetry Collector.
* @param deadlineMs max waiting time for the collector to process each span batch. When set to 0
* or to a negative value, the exporter will wait indefinitely.
*/
Expand All @@ -50,7 +53,7 @@ private OtlpGrpcSpanExporter(ManagedChannel channel, long deadlineMs) {
}

/**
* Submits all the given spans in a single batch to the Jaeger collector.
* Submits all the given spans in a single batch to the OpenTelemetry collector.
*
* @param spans the list of sampled Spans to be exported.
* @return the result of the operation
Expand Down Expand Up @@ -96,6 +99,18 @@ public static Builder newBuilder() {
return new Builder();
}

/**
* Returns a new {@link OtlpGrpcSpanExporter} reading the configuration values from the
* environment and from system properties. System properties override values defined in the
* environment. If a configuration value is missing, it uses the default value.
*
* @return a new {@link OtlpGrpcSpanExporter} instance.
* @since 0.5.0
*/
public static OtlpGrpcSpanExporter getDefault() {
return newBuilder().readEnvironment().readSystemProperties().build();
}

/**
* Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately
* cancelled. The channel is forcefully closed after a timeout.
Expand All @@ -110,7 +125,8 @@ public void shutdown() {
}

/** Builder utility for this exporter. */
public static class Builder {
public static class Builder extends ConfigBuilder<Builder> {
private static final String KEY_SPAN_TIMEOUT = "otel.otlp.span.timeout";
private ManagedChannel channel;
private long deadlineMs = 1_000; // 1 second

Expand Down Expand Up @@ -146,5 +162,77 @@ public OtlpGrpcSpanExporter build() {
}

private Builder() {}

/**
* Sets the configuration values from the given configuration map for only the available keys.
* This method looks for the following keys:
*
* <ul>
* <li>{@code otel.otlp.span.timeout}: to set the max waiting time for the collector to
* process each span batch.
* </ul>
*
* @param configMap {@link Map} holding the configuration values.
* @return this.
*/
@Override
protected Builder fromConfigMap(
Map<String, String> configMap, NamingConvention namingConvention) {
configMap = namingConvention.normalize(configMap);
Long value = getLongProperty(KEY_SPAN_TIMEOUT, configMap);
if (value != null) {
this.setDeadlineMs(value);
}
return this;
}

/**
* Sets the configuration values from the given properties object for only the available keys.
* This method looks for the following keys:
*
* <ul>
* <li>{@code otel.otlp.span.timeout}: to set the max waiting time for the collector to
* process each span batch.
* </ul>
*
* @param properties {@link Properties} holding the configuration values.
* @return this.
*/
@Override
public Builder readProperties(Properties properties) {
return super.readProperties(properties);
}

/**
* Sets the configuration values from environment variables for only the available keys. This
* method looks for the following keys:
*
* <ul>
* <li>{@code OTEL_OTLP_SPAN_TIMEOUT}: to set the max waiting time for the collector to
* process each span batch.
* </ul>
*
* @return this.
*/
@Override
public Builder readEnvironment() {
return super.readEnvironment();
}

/**
* Sets the configuration values from system properties for only the available keys. This method
* looks for the following keys:
*
* <ul>
* <li>{@code otel.otlp.span.timeout}: to set the max waiting time for the collector to
* process each span batch.
* </ul>
*
* @return this.
*/
@Override
public Builder readSystemProperties() {
return super.readSystemProperties();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020, OpenTelemetry 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.
*/

/**
* OpenTelemetry exporter which sends span and metric data to OpenTelemetry collector via gRPC.
*
* <h2>Contents</h2>
*
* <ul>
* <li>{@link io.opentelemetry.exporters.otlp.CommonAdapter}
* <li>{@link io.opentelemetry.exporters.otlp.MetricAdapter}
* <li>{@link io.opentelemetry.exporters.otlp.OtlpGrpcMetricExporter}
* <li>{@link io.opentelemetry.exporters.otlp.OtlpGrpcSpanExporter}
* <li>{@link io.opentelemetry.exporters.otlp.ResourceAdapter}
* <li>{@link io.opentelemetry.exporters.otlp.SpanAdapter}
* </ul>
*
* <p>Values for {@link io.opentelemetry.exporters.otlp.OtlpGrpcMetricExporter} and {@link
* io.opentelemetry.exporters.otlp.OtlpGrpcSpanExporter} can be read from system properties,
* environment variables, or {@link java.util.Properties} objects.
*
* <h2>{@link io.opentelemetry.exporters.otlp.OtlpGrpcMetricExporter}</h2>
*
* <p>For System Properties and {@link java.util.Properties} objects, {@link
* io.opentelemetry.exporters.otlp.OtlpGrpcMetricExporter} will look for the following names:
*
* <ul>
* <li>{@code otel.otlp.metric.timeout}: to set the max waiting time for the collector to process
* each metric batch.
* </ul>
*
* <p>For Environment Variable, {@link io.opentelemetry.exporters.otlp.OtlpGrpcMetricExporter} will
* look for the following names:
*
* <ul>
* <li>{@code OTEL_OTLP_METRIC_TIMEOUT}: to set the max waiting time for the collector to process
* each metric batch.
* </ul>
*
* <h2>{@link io.opentelemetry.exporters.otlp.OtlpGrpcSpanExporter}</h2>
*
* <p>For System Properties and {@link java.util.Properties} objects, {@link
* io.opentelemetry.exporters.otlp.OtlpGrpcSpanExporter} will look for the following names:
*
* <ul>
* <li>{@code otel.otlp.span.timeout}: to set the max waiting time for the collector to process
* each span batch.
* </ul>
*
* <p>For Environment Variable, {@link io.opentelemetry.exporters.otlp.OtlpGrpcSpanExporter} will
* look for the following names:
*
* <ul>
* <li>{@code OTEL_OTLP_SPAN_TIMEOUT}: to set the max waiting time for the collector to process
* each span batch.
* </ul>
*/
package io.opentelemetry.exporters.otlp;
Loading