Skip to content

Commit 99aed16

Browse files
authored
set service name & version in ecs-logging (#3064)
1 parent 6b28cba commit 99aed16

File tree

63 files changed

+1825
-410
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1825
-410
lines changed

CHANGELOG.asciidoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ endif::[]
2323
[[release-notes-1.37.0]]
2424
==== 1.37.0 - YYYY/MM/DD
2525
26+
[float]
27+
===== Potentially breaking changes
28+
* The list of instrumentation groups for log-related features have been simplified to `logging`, `log-reformatting`, `log-correlation` and `log-error`,
29+
as a consequence any agent configuration with `enable_instrumentations` or `disable_instrumentations` might require adjustments as the following instrumentation
30+
groups no longer exist: `jul-ecs`, `jul-error`, `log4j1-correlation`, `log4j1-ecs`, `log4j1-error`, `log4j2-correlation`, `log4j2-ecs`, `log4j2-error`,
31+
`logback-correlation`, `logback-ecs` and `slf4j-error`.
32+
2633
[float]
2734
===== Features
2835
* Add the <<config-disable-outgoing-tracecontext-headers>> config option to disable injection of `tracecontext` on outgoing
@@ -32,6 +39,7 @@ communication - {pull}2996[#2996]
3239
* Added agent.activation_method telemetry - {pull}2926[#2926]
3340
* Allow creation of exit spans with `@CaptureSpan` and `@Traced` annotations - {pull}3046[#3046]
3441
* Add the <<config-long-field-max-length>> config to enable capturing larger values for specific fields - {pull}3027[#3027]
42+
* Provide fallback correlation when `ecs-logging-java` is used - {pull}3064[#3064]
3543
3644
[float]
3745
===== Bug fixes
@@ -49,6 +57,7 @@ communication - {pull}2996[#2996]
4957
[[release-notes-1.36.0]]
5058
==== 1.36.0 - 2023/01/27
5159
60+
[float]
5261
===== Potentially breaking changes
5362
* Previously, agent instrumented all implementations of `javax.jms.MessageListener`, which could cause performance issues.
5463
From now on, the instrumentation is limited to implementations that fit at least one of the following criteria:

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ See [`apm-agent-plugins/README.md`](apm-agent-plugins/README.md)
256256

257257
HTML Documentation is generated from text files stored in `docs` folder using [AsciiDoc](http://asciidoc.org/) format.
258258
The `configuration.asciidoc` file is generated from running `co.elastic.apm.agent.configuration.ConfigurationExporter`
259-
(e.g. via `./mvnw test -Dtest="ConfigurationExporterTest" -DfailIfNoTests=false`). All the other asciidoc text files
259+
(e.g. via `./mvnw -Dsurefire.failIfNoTests=false -Dtest=ConfigurationExporterTest -pl apm-agent -am clean test`). All the other asciidoc text files
260260
are written manually.
261261

262262
A preview of the documentation is generated for each pull-request.

apm-agent-core/src/test/java/co/elastic/apm/agent/testutils/TestClassWithDependencyRunner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public void run() {
9898
System.out.println(failure);
9999
failure.getException().printStackTrace();
100100
}
101-
assertThat(listener.getSummary().getTestsFailedCount()).isZero();
101+
assertThat(listener.getSummary().getTestsFailedCount())
102+
.describedAs("at least one test failure reported, see stack trace for investigation")
103+
.isZero();
102104
}
103105

104106
}

apm-agent-plugins/apm-ecs-logging-plugin/pom.xml

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,82 @@
1616
</properties>
1717

1818
<dependencies>
19+
20+
<!-- log4j 1.x -->
1921
<dependency>
2022
<groupId>co.elastic.logging</groupId>
2123
<artifactId>log4j-ecs-layout</artifactId>
2224
<version>${version.ecs.logging}</version>
2325
<scope>provided</scope>
2426
</dependency>
27+
<dependency>
28+
<groupId>log4j</groupId>
29+
<artifactId>log4j</artifactId>
30+
<version>1.2.17</version>
31+
<scope>provided</scope>
32+
<optional>true</optional>
33+
</dependency>
34+
35+
<!-- log4j 2.x -->
2536
<dependency>
2637
<groupId>co.elastic.logging</groupId>
2738
<artifactId>log4j2-ecs-layout</artifactId>
2839
<version>${version.ecs.logging}</version>
2940
<scope>provided</scope>
3041
</dependency>
42+
<dependency>
43+
<groupId>org.apache.logging.log4j</groupId>
44+
<artifactId>log4j-core</artifactId>
45+
<version>2.20.0</version>
46+
<scope>provided</scope>
47+
<optional>true</optional>
48+
</dependency>
49+
50+
<!-- JUL -->
3151
<dependency>
3252
<groupId>co.elastic.logging</groupId>
3353
<artifactId>jul-ecs-formatter</artifactId>
3454
<version>${version.ecs.logging}</version>
3555
<scope>provided</scope>
3656
</dependency>
57+
58+
<!-- Jboss logmanager -->
3759
<dependency>
38-
<groupId>co.elastic.apm</groupId>
39-
<artifactId>apm-logging-plugin-common</artifactId>
40-
<version>${project.version}</version>
60+
<groupId>co.elastic.logging</groupId>
61+
<artifactId>jboss-logmanager-ecs-formatter</artifactId>
62+
<version>${version.ecs.logging}</version>
4163
<scope>provided</scope>
4264
</dependency>
4365
<dependency>
44-
<groupId>org.apache.logging.log4j</groupId>
45-
<artifactId>log4j-core</artifactId>
46-
<version>2.20.0</version>
66+
<groupId>org.jboss.logmanager</groupId>
67+
<artifactId>jboss-logmanager</artifactId>
68+
<version>2.1.15.Final</version>
69+
<scope>provided</scope>
70+
<optional>true</optional>
71+
</dependency>
72+
73+
<!-- logback -->
74+
<dependency>
75+
<groupId>co.elastic.logging</groupId>
76+
<artifactId>logback-ecs-encoder</artifactId>
77+
<version>${version.ecs.logging}</version>
4778
<scope>provided</scope>
4879
</dependency>
80+
<dependency>
81+
<groupId>ch.qos.logback</groupId>
82+
<artifactId>logback-classic</artifactId>
83+
<version>1.2.8</version>
84+
<scope>provided</scope>
85+
<optional>true</optional>
86+
</dependency>
87+
88+
<dependency>
89+
<groupId>co.elastic.apm</groupId>
90+
<artifactId>apm-logging-plugin-common</artifactId>
91+
<version>${project.version}</version>
92+
<scope>provided</scope>
93+
</dependency>
94+
4995
<dependency>
5096
<groupId>org.apache.ivy</groupId>
5197
<artifactId>ivy</artifactId>
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,23 @@
1818
*/
1919
package co.elastic.apm.agent.ecs_logging;
2020

21-
import co.elastic.apm.agent.bci.TracerAwareInstrumentation;
2221
import co.elastic.apm.agent.bci.bytebuddy.CustomElementMatchers;
23-
import net.bytebuddy.description.method.MethodDescription;
24-
import net.bytebuddy.description.type.TypeDescription;
22+
import co.elastic.apm.agent.loginstr.AbstractLogIntegrationInstrumentation;
2523
import net.bytebuddy.matcher.ElementMatcher;
2624

27-
import java.util.Arrays;
28-
import java.util.Collection;
29-
30-
import static net.bytebuddy.matcher.ElementMatchers.named;
3125
import static net.bytebuddy.matcher.ElementMatchers.not;
3226

33-
public class AbstractLog4j2ServiceInstrumentation extends TracerAwareInstrumentation {
34-
35-
@Override
36-
public ElementMatcher.Junction<ClassLoader> getClassLoaderMatcher() {
37-
return not(CustomElementMatchers.isAgentClassLoader());
38-
}
27+
public abstract class EcsLoggingInstrumentation extends AbstractLogIntegrationInstrumentation {
3928

4029
@Override
41-
public ElementMatcher.Junction<? super TypeDescription> getTypeMatcher() {
42-
return named("co.elastic.logging.log4j2.EcsLayout$Builder");
30+
protected String getLoggingInstrumentationGroupName() {
31+
// so far all ECS logging instrumentation is for log correlation, at trace and service levels.
32+
return LOG_CORRELATION;
4333
}
4434

4535
@Override
46-
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
47-
return named("build");
48-
}
49-
50-
@Override
51-
public Collection<String> getInstrumentationGroupNames() {
52-
return Arrays.asList("logging", "log4j2-ecs");
36+
public ElementMatcher.Junction<ClassLoader> getClassLoaderMatcher() {
37+
// ECS formatter that is loaded within the agent should not be instrumented
38+
return not(CustomElementMatchers.isInternalPluginClassLoader());
5339
}
5440
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.apm.agent.ecs_logging;
20+
21+
import co.elastic.apm.agent.configuration.CoreConfiguration;
22+
import co.elastic.apm.agent.configuration.ServiceInfo;
23+
import co.elastic.apm.agent.impl.ElasticApmTracer;
24+
import co.elastic.apm.agent.sdk.logging.Logger;
25+
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
26+
import co.elastic.apm.agent.sdk.weakconcurrent.WeakConcurrent;
27+
import co.elastic.apm.agent.sdk.weakconcurrent.WeakSet;
28+
import co.elastic.apm.agent.tracer.GlobalTracer;
29+
30+
import javax.annotation.Nullable;
31+
import java.util.Objects;
32+
33+
public class EcsLoggingUtils {
34+
35+
private static final Logger log = LoggerFactory.getLogger(EcsLoggingUtils.class);
36+
37+
private static final WeakSet<Object> nameChecked = WeakConcurrent.buildSet();
38+
private static final WeakSet<Object> versionChecked = WeakConcurrent.buildSet();
39+
40+
private static final ElasticApmTracer tracer = GlobalTracer.get().require(ElasticApmTracer.class);
41+
42+
@Nullable
43+
public static String getServiceName() {
44+
ServiceInfo serviceInfo = tracer.getServiceInfoForClassLoader(Thread.currentThread().getContextClassLoader());
45+
String configuredServiceName = tracer.getConfig(CoreConfiguration.class).getServiceName();
46+
return serviceInfo != null ? serviceInfo.getServiceName() : configuredServiceName;
47+
}
48+
49+
@Nullable
50+
public static String getServiceVersion() {
51+
ServiceInfo serviceInfo = tracer.getServiceInfoForClassLoader(Thread.currentThread().getContextClassLoader());
52+
String configuredServiceVersion = tracer.getConfig(CoreConfiguration.class).getServiceVersion();
53+
return serviceInfo != null ? serviceInfo.getServiceVersion() : configuredServiceVersion;
54+
}
55+
56+
private static void warnIfMisConfigured(String key, @Nullable String configuredValue, @Nullable String agentValue) {
57+
if (!Objects.equals(agentValue, configuredValue)) {
58+
log.warn("configuration values differ for '{}': ecs-logging='{}', agent='{}', traces and logs might not correlate properly", key, configuredValue, agentValue);
59+
}
60+
}
61+
62+
@Nullable
63+
public static String getOrWarnServiceVersion(Object target, @Nullable String value) {
64+
if (!versionChecked.add(target)) {
65+
return value;
66+
}
67+
if (value == null) {
68+
return getServiceVersion();
69+
} else {
70+
warnIfMisConfigured("service.version", value, getServiceVersion());
71+
return value;
72+
}
73+
}
74+
75+
@Nullable
76+
public static String getOrWarnServiceName(Object target, @Nullable String value) {
77+
if (!nameChecked.add(target)) {
78+
return value;
79+
}
80+
if (value == null) {
81+
return getServiceName();
82+
} else {
83+
warnIfMisConfigured("service.name", value, getServiceName());
84+
return value;
85+
}
86+
}
87+
}

apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/Log4j2ServiceNameInstrumentation.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/Log4j2ServiceVersionInstrumentation.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)