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 file configuration ComponentProvider support for exporters #6493

Merged
merged 3 commits into from
Aug 5, 2024
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 @@ -7,6 +7,7 @@

import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import io.opentelemetry.sdk.common.export.MemoryMode;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -54,5 +55,21 @@
memoryModeConsumer.accept(memoryMode);
}

/** Invoke the {@code memoryModeConsumer} with the configured {@link MemoryMode}. */
public static void configureExporterMemoryMode(
StructuredConfigProperties config, Consumer<MemoryMode> memoryModeConsumer) {
String memoryModeStr = config.getString("memory_mode");
if (memoryModeStr == null) {
return;
}
MemoryMode memoryMode;
try {
memoryMode = MemoryMode.valueOf(memoryModeStr.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
throw new ConfigurationException("Unrecognized memory_mode: " + memoryModeStr, e);
}
memoryModeConsumer.accept(memoryMode);
}

Check warning on line 72 in exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterBuilderUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/common/src/main/java/io/opentelemetry/exporter/internal/ExporterBuilderUtil.java#L67-L72

Added lines #L67 - L72 were not covered by tests

private ExporterBuilderUtil() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.logging.internal;

import io.opentelemetry.exporter.logging.SystemOutLogRecordExporter;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;

/**
* File configuration SPI implementation for {@link SystemOutLogRecordExporter}.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ConsoleLogRecordExporterComponentProvider
implements ComponentProvider<LogRecordExporter> {

@Override
public Class<LogRecordExporter> getType() {
return LogRecordExporter.class;
}

@Override
public String getName() {
return "console";
}

@Override
public LogRecordExporter create(StructuredConfigProperties config) {
return SystemOutLogRecordExporter.create();

Check warning on line 34 in exporters/logging/src/main/java/io/opentelemetry/exporter/logging/internal/ConsoleLogRecordExporterComponentProvider.java

View check run for this annotation

Codecov / codecov/patch

exporters/logging/src/main/java/io/opentelemetry/exporter/logging/internal/ConsoleLogRecordExporterComponentProvider.java#L34

Added line #L34 was not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.logging.internal;

import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import io.opentelemetry.sdk.metrics.export.MetricExporter;

/**
* File configuration SPI implementation for {@link LoggingMetricExporter}.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ConsoleMetricExporterComponentProvider
implements ComponentProvider<MetricExporter> {

@Override
public Class<MetricExporter> getType() {
return MetricExporter.class;
}

@Override
public String getName() {
return "console";
}

@Override
public MetricExporter create(StructuredConfigProperties config) {
return LoggingMetricExporter.create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.logging.internal;

import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.autoconfigure.spi.internal.ComponentProvider;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import io.opentelemetry.sdk.trace.export.SpanExporter;

/**
* File configuration SPI implementation for {@link LoggingSpanExporter}.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ConsoleSpanExporterComponentProvider implements ComponentProvider<SpanExporter> {

@Override
public Class<SpanExporter> getType() {
return SpanExporter.class;
}

@Override
public String getName() {
return "console";
}

@Override
public SpanExporter create(StructuredConfigProperties config) {
return LoggingSpanExporter.create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
io.opentelemetry.exporter.logging.internal.ConsoleMetricExporterComponentProvider
io.opentelemetry.exporter.logging.internal.ConsoleSpanExporterComponentProvider
io.opentelemetry.exporter.logging.internal.ConsoleLogRecordExporterComponentProvider
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.exporter.internal.ExporterBuilderUtil;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
import io.opentelemetry.sdk.autoconfigure.spi.internal.StructuredConfigProperties;
import io.opentelemetry.sdk.common.export.MemoryMode;
import io.opentelemetry.sdk.common.export.RetryPolicy;
import io.opentelemetry.sdk.metrics.Aggregation;
Expand Down Expand Up @@ -54,6 +55,11 @@
return config.getString("otel.exporter.otlp.protocol", PROTOCOL_GRPC);
}

/** Determine the configured OTLP protocol for the {@code dataType}. */
public static String getStructuredConfigOtlpProtocol(StructuredConfigProperties config) {
return config.getString("protocol", PROTOCOL_GRPC);
}

/** Invoke the setters with the OTLP configuration for the {@code dataType}. */
@SuppressWarnings("TooManyParameters")
public static void configureOtlpExporterBuilder(
Expand Down Expand Up @@ -134,9 +140,11 @@
determinePropertyByType(config, "otel.exporter.otlp", dataType, "client.certificate"));

if (clientKeyPath != null && clientKeyChainPath == null) {
throw new ConfigurationException("Client key provided but certification chain is missing");
throw new ConfigurationException(

Check warning on line 143 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L143

Added line #L143 was not covered by tests
"client key provided without client certificate - both client key and client certificate must be set");
} else if (clientKeyPath == null && clientKeyChainPath != null) {
throw new ConfigurationException("Client key chain provided but key is missing");
throw new ConfigurationException(

Check warning on line 146 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L146

Added line #L146 was not covered by tests
"client certificate provided without client key - both client key and client_certificate must be set");
}

byte[] certificateBytes = readFileBytes(certificatePath);
Expand All @@ -160,6 +168,81 @@
ExporterBuilderUtil.configureExporterMemoryMode(config, setMemoryMode);
}

/** Invoke the setters with the OTLP configuration for the {@code dataType}. */
@SuppressWarnings("TooManyParameters")
public static void configureOtlpExporterBuilder(
String dataType,
StructuredConfigProperties config,
Consumer<String> setEndpoint,
BiConsumer<String, String> addHeader,
Consumer<String> setCompression,
Consumer<Duration> setTimeout,
Consumer<byte[]> setTrustedCertificates,
BiConsumer<byte[], byte[]> setClientTls,
Consumer<RetryPolicy> setRetryPolicy,
Consumer<MemoryMode> setMemoryMode) {
String protocol = getStructuredConfigOtlpProtocol(config);
boolean isHttpProtobuf = protocol.equals(PROTOCOL_HTTP_PROTOBUF);
URL endpoint = validateEndpoint(config.getString("endpoint"), isHttpProtobuf);
if (endpoint != null && isHttpProtobuf) {
String path = endpoint.getPath();
if (!path.endsWith("/")) {
path += "/";
}
path += signalPath(dataType);
endpoint = createUrl(endpoint, path);
}
if (endpoint != null) {
setEndpoint.accept(endpoint.toString());
}

StructuredConfigProperties headers = config.getStructured("headers");
if (headers != null) {
headers
.getPropertyKeys()
.forEach(
header -> {
String value = headers.getString(header);
if (value != null) {
addHeader.accept(header, value);
}
});
}

String compression = config.getString("compression");
if (compression != null) {
setCompression.accept(compression);
}

Integer timeoutMs = config.getInt("timeout");
if (timeoutMs != null) {
setTimeout.accept(Duration.ofMillis(timeoutMs));
}

String certificatePath = config.getString("certificate");
String clientKeyPath = config.getString("client_key");
String clientKeyChainPath = config.getString("client_certificate");

if (clientKeyPath != null && clientKeyChainPath == null) {
throw new ConfigurationException(

Check warning on line 227 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L227

Added line #L227 was not covered by tests
"client_key provided without client_certificate - both client_key and client_certificate must be set");
} else if (clientKeyPath == null && clientKeyChainPath != null) {
throw new ConfigurationException(

Check warning on line 230 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L230

Added line #L230 was not covered by tests
"client_certificate provided without client_key - both client_key and client_certificate must be set");
}
byte[] certificateBytes = readFileBytes(certificatePath);
if (certificateBytes != null) {
setTrustedCertificates.accept(certificateBytes);
}
byte[] clientKeyBytes = readFileBytes(clientKeyPath);
byte[] clientKeyChainBytes = readFileBytes(clientKeyChainPath);
if (clientKeyBytes != null && clientKeyChainBytes != null) {
setClientTls.accept(clientKeyBytes, clientKeyChainBytes);
}

ExporterBuilderUtil.configureExporterMemoryMode(config, setMemoryMode);
}

/**
* Invoke the {@code aggregationTemporalitySelectorConsumer} with the configured {@link
* AggregationTemporality}.
Expand Down Expand Up @@ -188,6 +271,30 @@
aggregationTemporalitySelectorConsumer.accept(temporalitySelector);
}

public static void configureOtlpAggregationTemporality(
StructuredConfigProperties config,
Consumer<AggregationTemporalitySelector> aggregationTemporalitySelectorConsumer) {
String temporalityStr = config.getString("temporality_preference");
if (temporalityStr == null) {
return;
}
AggregationTemporalitySelector temporalitySelector;
switch (temporalityStr.toLowerCase(Locale.ROOT)) {
case "cumulative":
temporalitySelector = AggregationTemporalitySelector.alwaysCumulative();
break;

Check warning on line 285 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L284-L285

Added lines #L284 - L285 were not covered by tests
case "delta":
temporalitySelector = AggregationTemporalitySelector.deltaPreferred();
break;
case "lowmemory":
temporalitySelector = AggregationTemporalitySelector.lowMemory();
break;

Check warning on line 291 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L290-L291

Added lines #L290 - L291 were not covered by tests
default:
throw new ConfigurationException("Unrecognized temporality_preference: " + temporalityStr);

Check warning on line 293 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L293

Added line #L293 was not covered by tests
}
aggregationTemporalitySelectorConsumer.accept(temporalitySelector);
}

/**
* Invoke the {@code defaultAggregationSelectorConsumer} with the configured {@link
* DefaultAggregationSelector}.
Expand All @@ -212,6 +319,29 @@
}
}

/**
* Invoke the {@code defaultAggregationSelectorConsumer} with the configured {@link
* DefaultAggregationSelector}.
*/
public static void configureOtlpHistogramDefaultAggregation(
StructuredConfigProperties config,
Consumer<DefaultAggregationSelector> defaultAggregationSelectorConsumer) {
String defaultHistogramAggregation = config.getString("default_histogram_aggregation");
if (defaultHistogramAggregation == null) {
return;
}
if (AggregationUtil.aggregationName(Aggregation.base2ExponentialBucketHistogram())
.equalsIgnoreCase(defaultHistogramAggregation)) {
defaultAggregationSelectorConsumer.accept(
DefaultAggregationSelector.getDefault()
.with(InstrumentType.HISTOGRAM, Aggregation.base2ExponentialBucketHistogram()));
} else if (!AggregationUtil.aggregationName(explicitBucketHistogram())

Check warning on line 338 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L338

Added line #L338 was not covered by tests
.equalsIgnoreCase(defaultHistogramAggregation)) {
throw new ConfigurationException(

Check warning on line 340 in exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/all/src/main/java/io/opentelemetry/exporter/otlp/internal/OtlpConfigUtil.java#L340

Added line #L340 was not covered by tests
"Unrecognized default_histogram_aggregation: " + defaultHistogramAggregation);
}
}

private static URL createUrl(URL context, String spec) {
try {
return new URL(context, spec);
Expand Down
Loading
Loading