Skip to content

Commit

Permalink
Update deps add reflection based accessor (#1355)
Browse files Browse the repository at this point in the history
* update protobuf and otel bom versions

* add reflection hack to access methods.

* update license file

* use reflection method

* fix test
  • Loading branch information
breedx-splk authored Jul 11, 2023
1 parent c9e6c55 commit b8ee3a6
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.splunk.opentelemetry;

import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.tooling.BeforeAgentListener;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
Expand All @@ -41,7 +43,7 @@ public RealmAccessTokenChecker() {

@Override
public void beforeAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
ConfigProperties config = autoConfiguredOpenTelemetrySdk.getConfig();
ConfigProperties config = getConfig(autoConfiguredOpenTelemetrySdk);

if (isRealmConfigured(config) && !isAccessTokenConfigured(config)) {
logWarn.accept(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.splunk.opentelemetry.micrometer;

import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource;

import com.google.auto.service.AutoService;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Metrics;
Expand All @@ -33,8 +36,8 @@ public class MicrometerInstaller implements BeforeAgentListener {

@Override
public void beforeAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
ConfigProperties config = autoConfiguredOpenTelemetrySdk.getConfig();
Resource resource = autoConfiguredOpenTelemetrySdk.getResource();
ConfigProperties config = getConfig(autoConfiguredOpenTelemetrySdk);
Resource resource = getResource(autoConfiguredOpenTelemetrySdk);
SplunkMetricsConfig splunkMetricsConfig = new SplunkMetricsConfig(config, resource);

if (splunkMetricsConfig.enabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.splunk.opentelemetry.resource;

import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource;

import com.google.auto.service.AutoService;
import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.javaagent.extension.AgentListener;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;

/**
* This component exposes String resource attributes as system properties with <code>otel.resource.
Expand All @@ -33,7 +36,8 @@ public class ResourceAttributesToSystemProperties implements AgentListener {

@Override
public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
Attributes attributes = autoConfiguredOpenTelemetrySdk.getResource().getAttributes();
Resource resource = getResource(autoConfiguredOpenTelemetrySdk);
Attributes attributes = resource.getAttributes();
attributes.forEach(
(k, v) -> {
if (k.getType() == AttributeType.STRING) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.splunk.opentelemetry.servertiming;

import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.AgentListener;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
Expand All @@ -27,7 +29,7 @@ public class ServerTimingHeaderActivator implements AgentListener {

@Override
public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
ConfigProperties config = autoConfiguredOpenTelemetrySdk.getConfig();
ConfigProperties config = getConfig(autoConfiguredOpenTelemetrySdk);
if (config.getBoolean(EMIT_RESPONSE_HEADERS, true)) {
ServerTimingHeaderCustomizer.enabled = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package com.splunk.opentelemetry.servicename;

import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getResource;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.tooling.BeforeAgentListener;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
Expand Down Expand Up @@ -44,8 +47,8 @@ public ServiceNameChecker() {

@Override
public void beforeAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
ConfigProperties config = autoConfiguredOpenTelemetrySdk.getConfig();
Resource resource = autoConfiguredOpenTelemetrySdk.getResource();
ConfigProperties config = getConfig(autoConfiguredOpenTelemetrySdk);
Resource resource = getResource(autoConfiguredOpenTelemetrySdk);
if (serviceNameNotConfigured(config, resource)) {
logWarn.accept(
"Resource attribute 'service.name' is not set: your service is unnamed and will be difficult to identify."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright Splunk Inc.
*
* 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 io.opentelemetry.sdk.autoconfigure;

import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.resources.Resource;

/**
* This class is a hack to allows us to call getResource() and getConfig() on the
* AutoConfiguredOpenTelemetrySdk. This is merely here as a stop-gap measure until other means are
* in place.
*
* <p>See the discussion here:
* https://github.com/open-telemetry/opentelemetry-java/pull/5467#discussion_r1239559127
*
* <p>This class is internal and is not intended for public use.
*/
public final class AutoConfigureUtil {

private AutoConfigureUtil() {}

/** Returns the {@link ConfigProperties} used for auto-configuration. */
public static ConfigProperties getConfig(AutoConfiguredOpenTelemetrySdk sdk) {
return sdk.getConfig();
}

public static Resource getResource(AutoConfiguredOpenTelemetrySdk sdk) {
return sdk.getResource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class RealmAccessTokenCheckerTest {
@Test
void shouldNotLogWarnWhenNoRealmSet() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(false).build();
var autoConfiguredSdk = AutoConfiguredOpenTelemetrySdk.builder().build();
var underTest = new RealmAccessTokenChecker(logWarn);

// when
Expand All @@ -54,7 +53,6 @@ void shouldNotLogWarnWhenRealmIsNone() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> Map.of(SPLUNK_REALM_PROPERTY, SPLUNK_REALM_NONE))
.build();
var underTest = new RealmAccessTokenChecker(logWarn);
Expand All @@ -71,7 +69,6 @@ void shouldNotLogWarnWhenAccessTokenIsConfigured() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(
() -> Map.of(SPLUNK_REALM_PROPERTY, "test0", SPLUNK_ACCESS_TOKEN, "token"))
.build();
Expand All @@ -89,7 +86,6 @@ void shouldLogWarnWhenOnlyRealmIsConfigured() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> Map.of(SPLUNK_REALM_PROPERTY, "test0"))
.build();
var underTest = new RealmAccessTokenChecker(logWarn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.splunk.opentelemetry.SplunkConfiguration.METRICS_IMPLEMENTATION;
import static com.splunk.opentelemetry.SplunkConfiguration.PROFILER_MEMORY_ENABLED_PROPERTY;
import static com.splunk.opentelemetry.SplunkConfiguration.SPLUNK_REALM_NONE;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -148,15 +149,15 @@ private static ConfigProperties configuration() {

private static ConfigProperties configuration(
Supplier<Map<String, String>> testPropertiesSupplier) {
return AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
// don't create the SDK
.addPropertiesSupplier(() -> Map.of("otel.experimental.sdk.enabled", "false"))
// run in a customizer so that it executes after SplunkConfiguration#defaultProperties()
.addPropertiesCustomizer(config -> testPropertiesSupplier.get())
// implicitly loads SplunkConfiguration through SPI
.build()
.getConfig();
AutoConfiguredOpenTelemetrySdk sdk =
AutoConfiguredOpenTelemetrySdk.builder()
// don't create the SDK
.addPropertiesSupplier(() -> Map.of("otel.experimental.sdk.enabled", "false"))
// run in a customizer so that it executes after SplunkConfiguration#defaultProperties()
.addPropertiesCustomizer(config -> testPropertiesSupplier.get())
// implicitly loads SplunkConfiguration through SPI
.build();
return getConfig(sdk);
}

private static void verifyThatOtelMetricsInstrumentationsAreDisabled(ConfigProperties config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.splunk.opentelemetry.SplunkConfiguration.SPLUNK_REALM_NONE;
import static com.splunk.opentelemetry.SplunkConfiguration.SPLUNK_REALM_PROPERTY;
import static com.splunk.opentelemetry.micrometer.SplunkMetricsConfig.DEFAULT_METRICS_ENDPOINT;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -42,10 +43,9 @@ class SplunkMetricsConfigTest {
@Test
void testDefaultValues() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(false).build();
var autoConfiguredSdk = AutoConfiguredOpenTelemetrySdk.builder().build();
var resource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "test-service"));
var splunkMetricsConfig = new SplunkMetricsConfig(autoConfiguredSdk.getConfig(), resource);
var splunkMetricsConfig = new SplunkMetricsConfig(getConfig(autoConfiguredSdk), resource);

// when & then
assertFalse(splunkMetricsConfig.enabled());
Expand All @@ -60,7 +60,6 @@ void testDefaultValues() {
void testCustomValues() {
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
// make it a customizer so that it overrides SplunkConfiguration
.addPropertiesCustomizer(
config ->
Expand All @@ -78,7 +77,7 @@ void testCustomValues() {
.build();

var resource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "test-service"));
var splunkMetricsConfig = new SplunkMetricsConfig(autoConfiguredSdk.getConfig(), resource);
var splunkMetricsConfig = new SplunkMetricsConfig(getConfig(autoConfiguredSdk), resource);

// when & then
assertTrue(splunkMetricsConfig.enabled());
Expand All @@ -92,10 +91,9 @@ void testCustomValues() {
@Test
void emptyServiceNameIsNotValid() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(false).build();
var autoConfiguredSdk = AutoConfiguredOpenTelemetrySdk.builder().build();
var resource = Resource.empty();
var splunkMetricsConfig = new SplunkMetricsConfig(autoConfiguredSdk.getConfig(), resource);
var splunkMetricsConfig = new SplunkMetricsConfig(getConfig(autoConfiguredSdk), resource);

// when
Validated<?> validated = splunkMetricsConfig.validate();
Expand All @@ -109,11 +107,10 @@ void emptyEndpointIsNotValid() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> Map.of(METRICS_ENDPOINT_PROPERTY, ""))
.build();
var resource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "test-service"));
var splunkMetricsConfig = new SplunkMetricsConfig(autoConfiguredSdk.getConfig(), resource);
var splunkMetricsConfig = new SplunkMetricsConfig(getConfig(autoConfiguredSdk), resource);

// when
Validated<?> validated = splunkMetricsConfig.validate();
Expand All @@ -126,10 +123,9 @@ void emptyEndpointIsNotValid() {
void usesRealmUrlDefaultIfRealmDefined() {
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> Map.of(SPLUNK_REALM_PROPERTY, "test0"))
.build();
var config = new SplunkMetricsConfig(autoConfiguredSdk.getConfig(), Resource.getDefault());
var config = new SplunkMetricsConfig(getConfig(autoConfiguredSdk), Resource.getDefault());

assertEquals(config.uri(), "https://ingest.test0.signalfx.com");
}
Expand All @@ -138,10 +134,9 @@ void usesRealmUrlDefaultIfRealmDefined() {
void usesLocalUrlDefaultIfRealmIsNone() {
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> Map.of(SPLUNK_REALM_PROPERTY, SPLUNK_REALM_NONE))
.build();
var config = new SplunkMetricsConfig(autoConfiguredSdk.getConfig(), Resource.getDefault());
var config = new SplunkMetricsConfig(getConfig(autoConfiguredSdk), Resource.getDefault());

assertEquals(config.uri(), DEFAULT_METRICS_ENDPOINT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class ServiceNameCheckerTest {
@Test
void shouldLogWarnWhenNeitherServiceNameNorResourceAttributeIsConfigured() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder().setResultAsGlobal(false).build();
var autoConfiguredSdk = AutoConfiguredOpenTelemetrySdk.builder().build();

var underTest = new ServiceNameChecker(logWarn);

Expand All @@ -52,7 +51,6 @@ void shouldNotLogWarnWhenServiceNameIsConfigured() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> Map.of("otel.service.name", "test"))
.build();

Expand All @@ -70,7 +68,6 @@ void shouldNotLogWarnWhenResourceAttributeIsConfigured() {
// given
var autoConfiguredSdk =
AutoConfiguredOpenTelemetrySdk.builder()
.setResultAsGlobal(false)
.addPropertiesSupplier(() -> Map.of("otel.resource.attributes", "service.name=test"))
.build();

Expand Down
4 changes: 2 additions & 2 deletions dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
`java-platform`
}

val otelVersion = "1.27.0"
val otelVersion = "1.28.0"
val otelAlphaVersion = otelVersion.replaceFirst("(-SNAPSHOT)?$".toRegex(), "-alpha$1")
val otelInstrumentationVersion = "1.28.0-SNAPSHOT"
val otelInstrumentationAlphaVersion = otelInstrumentationVersion.replaceFirst("(-SNAPSHOT)?$".toRegex(), "-alpha$1")
Expand All @@ -28,7 +28,7 @@ dependencies {

// BOMs
api(enforcedPlatform("com.fasterxml.jackson:jackson-bom:2.15.2"))
api(enforcedPlatform("com.google.protobuf:protobuf-bom:3.23.2"))
api(enforcedPlatform("com.google.protobuf:protobuf-bom:3.23.4"))
api(enforcedPlatform("com.squareup.okhttp3:okhttp-bom:4.11.0"))
api(enforcedPlatform("io.grpc:grpc-bom:1.56.1"))
api(platform("io.micrometer:micrometer-bom:$micrometerVersion"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.splunk.opentelemetry.SplunkConfiguration.METRICS_ENABLED_PROPERTY;
import static com.splunk.opentelemetry.SplunkConfiguration.METRICS_IMPLEMENTATION;
import static com.splunk.opentelemetry.SplunkConfiguration.PROFILER_MEMORY_ENABLED_PROPERTY;
import static io.opentelemetry.sdk.autoconfigure.AutoConfigureUtil.getConfig;

import com.google.auto.service.AutoService;
import com.splunk.opentelemetry.instrumentation.jvmmetrics.micrometer.MicrometerAllocatedMemoryMetrics;
Expand All @@ -41,7 +42,7 @@ public class JvmMetricsInstaller implements AgentListener {

@Override
public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredOpenTelemetrySdk) {
ConfigProperties config = autoConfiguredOpenTelemetrySdk.getConfig();
ConfigProperties config = getConfig(autoConfiguredOpenTelemetrySdk);
boolean metricsEnabled = config.getBoolean(METRICS_ENABLED_PROPERTY, false);
if (!config.getBoolean("otel.instrumentation.jvm-metrics.splunk.enabled", metricsEnabled)) {
return;
Expand Down
Loading

0 comments on commit b8ee3a6

Please sign in to comment.