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

Encapsulate logging #6543

Merged
merged 5 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -14,8 +14,6 @@ tasks.withType<ShadowJar>().configureEach {

exclude("**/module-info.class")

// Prevents conflict with other SLF4J instances. Important for premain.
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
Comment on lines -17 to -18
Copy link
Member

Choose a reason for hiding this comment

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

👍

// rewrite dependencies calling Logger.getLogger
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ class JavaagentTestArgumentsProvider(
"-Dotel.metrics.exporter=otlp",
// suppress repeated logging of "No metric data to export - skipping export."
// since PeriodicMetricReader is configured with a short interval
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.opentelemetry.sdk.metrics.export.PeriodicMetricReader=INFO",
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.opentelemetry.sdk.metrics.export.PeriodicMetricReader=INFO",
Copy link
Member

Choose a reason for hiding this comment

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

wow thats quite a property name 😂

// suppress a couple of verbose ClassNotFoundException stack traces logged at debug level
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ServerImplBuilder=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO",
"-Dio.opentelemetry.javaagent.slf4j.simpleLogger.log.io.grpc.Context=INFO"
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.grpc.internal.ServerImplBuilder=INFO",
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.grpc.internal.ManagedChannelImplBuilder=INFO",
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.perfmark.PerfMark=INFO",
"-Dio.opentelemetry.javaagent.logging.simple.slf4j.simpleLogger.log.io.grpc.Context=INFO"
)
}

Expand Down
2 changes: 0 additions & 2 deletions examples/distro/gradle/shadow.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
ext.relocatePackages = { shadowJar ->
// Prevents conflict with other SLF4J instances. Important for premain.
shadowJar.relocate 'org.slf4j', 'io.opentelemetry.javaagent.slf4j'
// rewrite dependencies calling Logger.getLogger
shadowJar.relocate 'java.util.logging.Logger', 'io.opentelemetry.javaagent.bootstrap.PatchLogger'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ tasks.withType<ShadowJar>().configureEach {

exclude("**/module-info.class")

// Prevents conflict with other SLF4J instances. Important for premain.
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
// rewrite dependencies calling Logger.getLogger
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")

Expand Down
4 changes: 0 additions & 4 deletions javaagent-bootstrap/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ plugins {

group = "io.opentelemetry.javaagent"

val agentSlf4jVersion = "2.0.0"

dependencies {
implementation(project(":instrumentation-api"))
implementation(project(":instrumentation-appender-api-internal"))
implementation("org.slf4j:slf4j-api:$agentSlf4jVersion")
implementation("org.slf4j:slf4j-simple:$agentSlf4jVersion")

testImplementation(project(":testing-common"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
*/
public class AgentClassLoader extends URLClassLoader {

// NOTE it's important not to use slf4j in this class, because this class is used before slf4j is
// configured, and so using slf4j here would initialize slf4j-simple before we have a chance to
// configure the logging levels
// NOTE it's important not to use logging in this class, because this class is used before logging
// is initialized

static {
ClassLoader.registerAsParallelCapable();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap;

import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;

public abstract class InternalLogger {

private static final AtomicReference<Factory> loggerFactory =
new AtomicReference<>(NoopLoggerFactory.INSTANCE);

public static void initialize(Factory factory) {
if (!loggerFactory.compareAndSet(NoopLoggerFactory.INSTANCE, factory)) {
factory
.create(InternalLogger.class.getName())
.log(
Level.WARN,
"Developer error: logging system has already been initialized once",
null);
}
}

static InternalLogger getLogger(String name) {
return loggerFactory.get().create(name);
}

protected abstract boolean isLoggable(Level level);

protected abstract void log(Level level, String message, @Nullable Throwable error);

protected abstract String name();

public enum Level {
ERROR,
WARN,
INFO,
DEBUG,
TRACE
}

@FunctionalInterface
public interface Factory {

InternalLogger create(String name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap;

import javax.annotation.Nullable;

final class NoopLoggerFactory implements InternalLogger.Factory {

static final InternalLogger.Factory INSTANCE = new NoopLoggerFactory();

private NoopLoggerFactory() {}

@Override
public InternalLogger create(String name) {
return new NoopLogger(name);
}

private static final class NoopLogger extends InternalLogger {

private final String name;

private NoopLogger(String name) {
this.name = name;
}

@Override
public boolean isLoggable(Level level) {
return false;
}

@Override
public void log(Level level, String message, @Nullable Throwable error) {}

@Override
protected String name() {
return name;
}
}
}
Loading