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

Logback appender needs to be re-initialized after spring application starts #10307

Closed
lmolkova opened this issue Jan 22, 2024 · 3 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@lmolkova
Copy link
Contributor

lmolkova commented Jan 22, 2024

Describe the bug

I have the following setup

  • manual instrumentation with opentelemetry-logback-appender-1.0
  • logback appender is initialized statically before SpringApplication.run is called
  • no logs are exported

Steps to reproduce

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {
	private final static Logger LOGGER = LoggerFactory.getLogger(DemoApplication.class);
	private final static OpenTelemetry OTEL = initOTel();

	private static OpenTelemetry initOTel() {
		System.setProperty("otel.traces.exporter", "none");
		System.setProperty("otel.metrics.exporter", "none");
		System.setProperty("otel.logs.exporter", "logging");

		OpenTelemetry otel = AutoConfiguredOpenTelemetrySdk.builder()
				.setResultAsGlobal()
				.build()
				.getOpenTelemetrySdk();
		OpenTelemetryAppender.install(otel);

		return otel;
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@Override
	public void run(ApplicationArguments args) {
		// uncomment next line to make things work
		// OpenTelemetryAppender.install(OTEL);
		LOGGER.info("Starting demo application");
	}
}

full code: demo.zip

Expected behavior

if I uncomment OpenTelemetryAppender.install(OTEL), things work as expected:

2024-01-22T21:47:20.22Z INFO 'Starting demo application' : 00000000000000000000000000000000 0000000000000000 [scopeInfo: com.example.demo.DemoApplication:] {}

Actual behavior

nothing is logged

Javaagent or library instrumentation version

1.34.1

Environment

JDK: 21
OS: windows

Additional context

Spring re-creates log appenders during SpringApplication.run call

image

As a result, this new OTelAppender never gets configured with otel instance

I believe it's possible to apply global otel in the constructor or default to it until another instance is provided.

Otherwise, this behavior should at least be documented (ideally as a spring-boot example with proper way to config otel there).

@lmolkova lmolkova added bug Something isn't working needs triage New issue that requires triage labels Jan 22, 2024
@laurit
Copy link
Contributor

laurit commented Jan 23, 2024

@jeanbisutti could you take a look

@jeanbisutti jeanbisutti self-assigned this Jan 23, 2024
@jeanbisutti
Copy link
Member

As you mentioned, Spring Boot re-initializes the logging system. So, the way to proceed with a programmatic use of the SDK is to install OpenTelemetry in the OpenTelemetry appender after this re-initialization.

However, with Spring Boot, the OpenTelemetry documentation recommends using the OpenTelemetry Java agent or the OpenTelemetry starter, not a programmatic use of the SDK.

The OpenTelemetry starter automatically injects an OpenTelemetry instance into the OpenTelemetry Logback appender. A PR was created to remove the need to add the OpenTelemetry appender in the Logback xml file. So, with the OpenTelemetry starter having this PR code, the logging instrumentation will be available without configuration for Logback.

The opentelemetry-java-examples/spring-native shows an example with the OpenTelemetry starter.

@jeanbisutti jeanbisutti added needs author feedback Waiting for additional feedback from the author and removed needs triage New issue that requires triage labels Jan 23, 2024
@lmolkova
Copy link
Contributor Author

nice! thank you @jeanbisutti for the detailed response and sharing the example.

@github-actions github-actions bot removed the needs author feedback Waiting for additional feedback from the author label Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants