Skip to content

Commit

Permalink
we can keep it simple
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainJuge committed Mar 28, 2023
1 parent 4f98d2d commit fd7db68
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent;
import co.elastic.apm.agent.sdk.weakconcurrent.WeakSet;
import co.elastic.apm.agent.tracer.GlobalTracer;

import javax.annotation.Nullable;
import java.util.Objects;
Expand All @@ -36,16 +37,17 @@ public class EcsLoggingUtils {
private static final WeakSet<Object> nameChecked = WeakConcurrent.buildSet();
private static final WeakSet<Object> versionChecked = WeakConcurrent.buildSet();

private static final ElasticApmTracer tracer = GlobalTracer.get().require(ElasticApmTracer.class);

@Nullable
public static String getServiceName(ElasticApmTracer tracer) {
public static String getServiceName() {
ServiceInfo serviceInfo = tracer.getServiceInfoForClassLoader(Thread.currentThread().getContextClassLoader());
String configuredServiceName = tracer.getConfig(CoreConfiguration.class).getServiceName();
return serviceInfo != null ? serviceInfo.getServiceName() : configuredServiceName;
}

@Nullable
public static String getServiceVersion(ElasticApmTracer tracer) {
public static String getServiceVersion() {
ServiceInfo serviceInfo = tracer.getServiceInfoForClassLoader(Thread.currentThread().getContextClassLoader());
String configuredServiceVersion = tracer.getConfig(CoreConfiguration.class).getServiceVersion();
return serviceInfo != null ? serviceInfo.getServiceVersion() : configuredServiceVersion;
Expand All @@ -58,27 +60,27 @@ private static void warnIfMisConfigured(String key, @Nullable String configuredV
}

@Nullable
public static String getOrWarnServiceVersion(ElasticApmTracer tracer,Object target, @Nullable String value) {
public static String getOrWarnServiceVersion(Object target, @Nullable String value) {
if (!versionChecked.add(target)) {
return value;
}
if (value == null) {
return getServiceVersion(tracer);
return getServiceVersion();
} else {
warnIfMisConfigured("service.version", value, getServiceVersion(tracer));
warnIfMisConfigured("service.version", value, getServiceVersion());
return value;
}
}

@Nullable
public static String getOrWarnServiceName(ElasticApmTracer tracer, Object target, @Nullable String value) {
public static String getOrWarnServiceName(Object target, @Nullable String value) {
if (!nameChecked.add(target)) {
return value;
}
if (value == null) {
return getServiceName(tracer);
return getServiceName();
} else {
warnIfMisConfigured("service.name", value, getServiceName(tracer));
warnIfMisConfigured("service.name", value, getServiceName());
return value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation;
import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.logging.jboss.logmanager.EcsFormatter;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField;
Expand Down Expand Up @@ -59,7 +58,7 @@ public static class AdviceClass {
public static String onEnter(@Advice.This Object formatter,
@Advice.FieldValue("serviceName") @Nullable String serviceName) {

return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), formatter, serviceName);
return EcsLoggingUtils.getOrWarnServiceName(formatter, serviceName);
}
}

Expand All @@ -82,7 +81,7 @@ public static class AdviceClass {
public static String onEnter(@Advice.This Object formatter,
@Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) {

return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), formatter, serviceVersion);
return EcsLoggingUtils.getOrWarnServiceVersion(formatter, serviceVersion);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation;
import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField;
import net.bytebuddy.description.method.MethodDescription;
Expand Down Expand Up @@ -59,7 +58,7 @@ public static String onEnter(@Advice.This Object formatter,
@Advice.FieldValue("serviceName") @Nullable String serviceName) {

// setServiceName is protected in earlier versions, thus setting field directly
return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), formatter, serviceName);
return EcsLoggingUtils.getOrWarnServiceName(formatter, serviceName);
}
}

Expand All @@ -82,7 +81,7 @@ public static class AdviceClass {
public static String onEnter(@Advice.This Object formatter,
@Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) {

return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), formatter, serviceVersion);
return EcsLoggingUtils.getOrWarnServiceVersion(formatter, serviceVersion);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation;
import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.logging.log4j.EcsLayout;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField;
Expand Down Expand Up @@ -59,7 +58,7 @@ public static class AdviceClass {
public static String onEnter(@Advice.This Object layout,
@Advice.FieldValue("serviceName") @Nullable String serviceName) {

return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), layout, serviceName);
return EcsLoggingUtils.getOrWarnServiceName(layout, serviceName);
}
}

Expand All @@ -82,7 +81,7 @@ public static class AdviceClass {
public static String onEnter(@Advice.This Object layout,
@Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) {

return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), layout, serviceVersion);
return EcsLoggingUtils.getOrWarnServiceVersion(layout, serviceVersion);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation;
import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.logging.log4j2.EcsLayout;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField;
Expand Down Expand Up @@ -58,7 +57,7 @@ public static class AdviceClass {
public static String onEnter(@Advice.This Object builder,
@Advice.FieldValue("serviceName") @Nullable String serviceName) {

return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), builder, serviceName);
return EcsLoggingUtils.getOrWarnServiceName(builder, serviceName);
}
}
}
Expand All @@ -80,7 +79,7 @@ public static class AdviceClass {
public static String onEnter(@Advice.This Object builder,
@Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) {

return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), builder, serviceVersion);
return EcsLoggingUtils.getOrWarnServiceVersion(builder, serviceVersion);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation;
import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.logging.logback.EcsEncoder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField;
Expand Down Expand Up @@ -58,7 +57,7 @@ public static class AdviceClass {
public static String onExit(@Advice.This Object encoder,
@Advice.FieldValue("serviceName") @Nullable String serviceName) {

return EcsLoggingUtils.getOrWarnServiceName(tracer.require(ElasticApmTracer.class), encoder, serviceName);
return EcsLoggingUtils.getOrWarnServiceName(encoder, serviceName);
}
}
}
Expand All @@ -80,7 +79,7 @@ public static class AdviceClass {
public static String onExit(@Advice.This Object encoder,
@Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) {

return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), encoder, serviceVersion);
return EcsLoggingUtils.getOrWarnServiceVersion(encoder, serviceVersion);
}
}

Expand All @@ -94,7 +93,7 @@ public static class VersionAdvice {
public static String onExit(@Advice.This Object encoder,
@Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) {

return EcsLoggingUtils.getOrWarnServiceVersion(tracer.require(ElasticApmTracer.class), encoder, serviceVersion);
return EcsLoggingUtils.getOrWarnServiceVersion(encoder, serviceVersion);
}
}

Expand Down

0 comments on commit fd7db68

Please sign in to comment.