Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
add oats test
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Dec 18, 2023
1 parent 7be5c13 commit 00eb3e2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
import java.util.Map;
import java.util.Optional;

public record ConnectionProperties(Optional<String> endpoint, Map<String, String> headers, Duration metricExportInterval) {}
public record ConnectionProperties(
Optional<String> endpoint, Map<String, String> headers, Duration metricExportInterval) {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.grafana.opentelemetry;

import io.micrometer.core.instrument.config.validate.DurationValidator;
import io.micrometer.registry.otlp.OtlpConfig;

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/com/grafana/opentelemetry/OpenTelemetryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

import io.micrometer.common.util.StringUtils;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.config.validate.DurationValidator;
import io.micrometer.core.instrument.config.validate.InvalidReason;
import io.micrometer.core.instrument.config.validate.PropertyValidator;
import io.micrometer.core.instrument.config.validate.Validated;
import io.micrometer.core.instrument.logging.LoggingMeterRegistry;
import io.micrometer.registry.otlp.OtlpMeterRegistry;
import io.opentelemetry.api.OpenTelemetry;
Expand All @@ -29,7 +25,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.logging.log4j.util.Strings;
Expand Down Expand Up @@ -120,8 +115,10 @@ public AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk(
GrafanaProperties properties,
ConnectionProperties connectionProperties,
@Value("${spring.application.name:#{null}}") String applicationName) {
// the log record exporter uses the global instance, so we need to set it as global to avoid a warning
AutoConfiguredOpenTelemetrySdkBuilder builder = AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal();
// the log record exporter uses the global instance, so we need to set it as global to avoid a
// warning
AutoConfiguredOpenTelemetrySdkBuilder builder =
AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal();

Map<String, String> configProperties = getConfigProperties(properties, connectionProperties);
builder.addPropertiesSupplier(() -> configProperties);
Expand Down Expand Up @@ -169,16 +166,16 @@ private Map<String, String> getConfigProperties(
public ConnectionProperties connectionProperties(
GrafanaProperties properties,
@Value("${otel.exporter.otlp.endpoint:#{null}}") String otlpEndpoint,
@Value("${otel.metric.export.interval:60000}") String metricExportInterval
) {
@Value("${otel.metric.export.interval:60000}") String metricExportInterval) {
GrafanaProperties.CloudProperties cloud = properties.getCloud();
Map<String, String> headers = getHeaders(cloud.getInstanceId(), cloud.getApiKey());
if (StringUtils.isBlank(otlpEndpoint)) {
otlpEndpoint = properties.getOnPrem().getEndpoint();
}
Optional<String> endpoint = getEndpoint(otlpEndpoint, cloud.getZone(), headers);

return new ConnectionProperties(endpoint, headers, Duration.of(Integer.parseInt(metricExportInterval), ChronoUnit.MILLIS));
return new ConnectionProperties(
endpoint, headers, Duration.of(Integer.parseInt(metricExportInterval), ChronoUnit.MILLIS));
}

static Map<String, String> maskAuthHeader(Map<String, String> configProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
import static org.junit.jupiter.api.Named.named;

import io.micrometer.core.instrument.Clock;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.sdk.resources.Resource;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.util.ReflectionUtils;

public class ResourceAttributesTest {

Expand All @@ -31,15 +34,22 @@ public TestCase withRunner(
}
}

private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(OpenTelemetryConfig.class))
.withBean(Clock.class, () -> Clock.SYSTEM);
@AfterEach
void setUp() throws Exception {
GlobalOpenTelemetry.resetForTest();
ReflectionUtils.invokeMethod(
Class.forName("io.opentelemetry.api.events.GlobalEventEmitterProvider")
.getMethod("resetForTest"),
null);
}

@ParameterizedTest
@MethodSource("testCases")
void readResourceAttributes(TestCase testCase) {
ApplicationContextRunner runner = contextRunner;
ApplicationContextRunner runner =
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(OpenTelemetryConfig.class))
.withBean(Clock.class, () -> Clock.SYSTEM);
if (testCase.runner != null) {
runner = testCase.runner.apply(runner);
}
Expand Down

0 comments on commit 00eb3e2

Please sign in to comment.