Skip to content

Commit

Permalink
Encapsulate actual logging implementation better
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek committed Sep 5, 2022
1 parent 78b3fae commit 00b2fa7
Show file tree
Hide file tree
Showing 25 changed files with 597 additions and 550 deletions.
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")
// 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",
// 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

0 comments on commit 00b2fa7

Please sign in to comment.