Skip to content

spring starter: add thread details instrumentation / configure logging span exporter #14117

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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,38 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.logging;

import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.instrumentation.internal.logging.LoggingSpanExporterConfigurer;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.SdkEnabled;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
@Conditional(SdkEnabled.class)
// for backward compatibility with declarative configuration
@ConditionalOnProperty(name = "otel.debug", havingValue = "true")
@ConditionalOnClass(LoggingSpanExporter.class)
@Configuration
public class LoggingExporterAutoConfiguration {

@Bean
public AutoConfigurationCustomizerProvider loggingOtelCustomizer() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only traces and not also metrics and logs? (maybe split the debug behavior out into separate PR? I reviewed the thread stuff and it all looks good to me)

return p ->
p.addTracerProviderCustomizer(
(builder, config) -> {
LoggingSpanExporterConfigurer.enableLoggingExporter(builder, config);
return builder;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.thread;

import io.opentelemetry.instrumentation.internal.thread.AddThreadDetailsSpanProcessor;
import io.opentelemetry.instrumentation.spring.autoconfigure.internal.ConditionalOnEnabledInstrumentation;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
@ConditionalOnEnabledInstrumentation(module = "common.thread-details", enabledByDefault = false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent enables this by default

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trask please check if thread details should be disabled by default

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I forgot to raise it in semconv meeting, I've added it to next week's agenda so I won't forget

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trask do you have an update?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it should ideally be opt-in

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then it looks like we can leave it as is

@Configuration
@SuppressWarnings("OtelPrivateConstructorForUtilityClass")
public class ThreadDetailsAutoConfiguration {

@Bean
public AutoConfigurationCustomizerProvider threadDetailOtelCustomizer() {
return p ->
p.addTracerProviderCustomizer(
(builder, config) -> {
builder.addSpanProcessor(new AddThreadDetailsSpanProcessor());
return builder;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import io.opentelemetry.api.internal.ConfigUtil;
import io.opentelemetry.exporter.otlp.internal.OtlpConfigUtil;
import io.opentelemetry.instrumentation.resources.ResourceProviderPropertiesCustomizer;
import io.opentelemetry.instrumentation.internal.resources.ResourceProviderPropertiesCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import java.time.Duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.w
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.scheduling.SpringSchedulingInstrumentationAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.runtimemetrics.RuntimeMetricsAutoConfiguration,\
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.runtimemetrics.Java8RuntimeMetricsProvider,\
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.runtimemetrics.Java17RuntimeMetricsProvider
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.runtimemetrics.Java17RuntimeMetricsProvider,\
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.thread.ThreadDetailsAutoConfiguration

org.springframework.context.ApplicationListener=\
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.logging.LogbackAppenderApplicationListener
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.s
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.runtimemetrics.RuntimeMetricsAutoConfiguration
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.runtimemetrics.Java8RuntimeMetricsProvider
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.runtimemetrics.Java17RuntimeMetricsProvider
io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.thread.ThreadDetailsAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void specialListProperties() {
"io.opentelemetry.contrib.aws.resource.LambdaResourceProvider",
"io.opentelemetry.contrib.gcp.resource.GCPResourceProvider",
"io.opentelemetry.contrib.cloudfoundry.resources.CloudFoundryResourceProvider",
"io.opentelemetry.instrumentation.resources.ResourceProviderPropertiesCustomizerTest$Provider");
"io.opentelemetry.instrumentation.internal.resources.ResourceProviderPropertiesCustomizerTest$Provider");
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@

package io.opentelemetry.javaagent.tooling;

import static io.opentelemetry.instrumentation.internal.logging.LoggingSpanExporterConfigurer.enableLoggingExporter;
import static io.opentelemetry.javaagent.tooling.AgentInstaller.JAVAAGENT_ENABLED_CONFIG;
import static java.util.Collections.emptyList;

import com.google.auto.service.AutoService;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.instrumentation.internal.thread.AddThreadDetailsSpanProcessor;
import io.opentelemetry.javaagent.tooling.config.AgentConfig;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;

@AutoService(AutoConfigurationCustomizerProvider.class)
public class AgentTracerProviderConfigurer implements AutoConfigurationCustomizerProvider {
Expand All @@ -40,22 +39,10 @@ private static SdkTracerProviderBuilder configure(
sdkTracerProviderBuilder.addSpanProcessor(new AddThreadDetailsSpanProcessor());
}

maybeEnableLoggingExporter(sdkTracerProviderBuilder, config);

return sdkTracerProviderBuilder;
}

private static void maybeEnableLoggingExporter(
SdkTracerProviderBuilder builder, ConfigProperties config) {
if (AgentConfig.isDebugModeEnabled(config)) {
// don't install another instance if the user has already explicitly requested it.
if (loggingExporterIsNotAlreadyConfigured(config)) {
builder.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()));
}
enableLoggingExporter(sdkTracerProviderBuilder, config);
}
}

private static boolean loggingExporterIsNotAlreadyConfigured(ConfigProperties config) {
return !config.getList("otel.traces.exporter", emptyList()).contains("logging");
return sdkTracerProviderBuilder;
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions sdk-autoconfigure-support/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ plugins {
group = "io.opentelemetry.instrumentation"

dependencies {
compileOnly("io.opentelemetry:opentelemetry-exporter-logging")
api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")

compileOnly("com.google.code.findbugs:annotations")
testCompileOnly("com.google.code.findbugs:annotations")
testImplementation(project(":testing-common"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.internal.logging;

import static java.util.Collections.emptyList;

import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public class LoggingSpanExporterConfigurer {

private LoggingSpanExporterConfigurer() {}

public static void enableLoggingExporter(
SdkTracerProviderBuilder builder, ConfigProperties config) {
// don't install another instance if the user has already explicitly requested it.
if (loggingExporterIsNotAlreadyConfigured(config)) {
builder.addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create()));
}
}

private static boolean loggingExporterIsNotAlreadyConfigured(ConfigProperties config) {
return !config.getList("otel.traces.exporter", emptyList()).contains("logging");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.resources;
package io.opentelemetry.instrumentation.internal.resources;

import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
Expand All @@ -17,6 +17,10 @@
import java.util.Set;
import javax.annotation.Nullable;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public class ResourceProviderPropertiesCustomizer implements AutoConfigurationCustomizerProvider {

private static final Map<String, String> DISABLED_BY_DEFAULT_RESOURCE_PROVIDERS = new HashMap<>();
Expand Down Expand Up @@ -49,7 +53,7 @@ public class ResourceProviderPropertiesCustomizer implements AutoConfigurationCu
"cloudfoundry");
// for testing
DISABLED_BY_DEFAULT_RESOURCE_PROVIDERS.put(
"io.opentelemetry.instrumentation.resources.ResourceProviderPropertiesCustomizerTest$Provider",
"io.opentelemetry.instrumentation.internal.resources.ResourceProviderPropertiesCustomizerTest$Provider",
"test");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling;
package io.opentelemetry.instrumentation.internal.thread;

import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public class AddThreadDetailsSpanProcessor implements SpanProcessor {

// attributes are not stable yet
public static final AttributeKey<Long> THREAD_ID = longKey("thread.id");
public static final AttributeKey<String> THREAD_NAME = stringKey("thread.name");

@Override
public void onStart(Context context, ReadWriteSpan span) {
Thread currentThread = Thread.currentThread();
span.setAttribute(ThreadIncubatingAttributes.THREAD_ID, currentThread.getId());
span.setAttribute(ThreadIncubatingAttributes.THREAD_NAME, currentThread.getName());
span.setAttribute(THREAD_ID, currentThread.getId());
span.setAttribute(THREAD_NAME, currentThread.getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.opentelemetry.instrumentation.resources.ResourceProviderPropertiesCustomizer
io.opentelemetry.instrumentation.internal.resources.ResourceProviderPropertiesCustomizer
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.resources;
package io.opentelemetry.instrumentation.internal.resources;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -58,7 +58,7 @@ private EnabledTestCase(
@TestFactory
Stream<DynamicTest> enabledTestCases() {
String className =
"io.opentelemetry.instrumentation.resources.ResourceProviderPropertiesCustomizerTest$Provider";
"io.opentelemetry.instrumentation.internal.resources.ResourceProviderPropertiesCustomizerTest$Provider";
return Stream.of(
new EnabledTestCase(
"explicitEnabled", true, Collections.emptySet(), Collections.emptySet(), true),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.internal.thread;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes;
import org.junit.jupiter.api.Test;

class AddThreadDetailsSpanProcessorTest {

private final ReadWriteSpan span = mock(ReadWriteSpan.class);

private final SpanProcessor spanProcessor = new AddThreadDetailsSpanProcessor();

@Test
void onStart() {
assertThat(spanProcessor.isStartRequired()).isTrue();
}

@Test
void setThreadAttributes() {
Thread thread = Thread.currentThread();
spanProcessor.onStart(Context.root(), span);

verify(span).setAttribute(ThreadIncubatingAttributes.THREAD_ID, thread.getId());
verify(span).setAttribute(ThreadIncubatingAttributes.THREAD_NAME, thread.getName());
verifyNoMoreInteractions(span);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.opentelemetry.instrumentation.resources.ResourceProviderPropertiesCustomizerTest$Provider
io.opentelemetry.instrumentation.internal.resources.ResourceProviderPropertiesCustomizerTest$Provider
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
// The headers are simply set here to make sure that headers can be parsed
"otel.exporter.otlp.headers.c=3",
"otel.instrumentation.runtime-telemetry.emit-experimental-telemetry=true",
"otel.instrumentation.common.thread_details.enabled=true",
})
class OtelSpringStarterSmokeTest extends AbstractOtelSpringStarterSmokeTest {}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
AbstractKafkaSpringStarterSmokeTest.KafkaConfig.class
},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnabledInNativeImage // see JvmMongodbSpringStarterSmokeTest for the JVM test
@EnabledInNativeImage // see AbstractJvmKafkaSpringStarterSmokeTest for the JVM test
@RequiresDockerCompose
public class GraalVmNativeKafkaSpringStarterSmokeTest extends AbstractKafkaSpringStarterSmokeTest {}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@SpringBootTest(
classes = {OtelSpringStarterSmokeTestApplication.class, SpringSmokeOtelConfiguration.class},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnabledInNativeImage // see JvmMongodbSpringStarterSmokeTest for the JVM test
@EnabledInNativeImage // see AbstractJvmMongodbSpringStarterSmokeTest for the JVM test
@RequiresDockerCompose
public class GraalVmNativeMongodbSpringStarterSmokeTest
extends AbstractMongodbSpringStarterSmokeTest {}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"otel.exporter.otlp.headers.c=3",
"otel.instrumentation.runtime-telemetry.emit-experimental-telemetry=true",
"otel.instrumentation.runtime-telemetry-java17.enable-all=true",
"otel.instrumentation.common.thread_details.enabled=true",
})
class OtelSpringStarterSmokeTest extends AbstractOtelSpringStarterSmokeTest {

Expand Down
Loading
Loading