forked from elastic/apm-agent-java
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Log correlation remake suggestions #4
Merged
eyalkoren
merged 2 commits into
eyalkoren:log-correlation-remake
from
felixbarny:log-correlation-remake-suggestions
Feb 22, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 0 additions & 52 deletions
52
.../src/main/java/co/elastic/apm/agent/log/shader/AbstractLogCorrelationInstrumentation.java
This file was deleted.
Oops, something went wrong.
178 changes: 178 additions & 0 deletions
178
...-plugin-common/src/main/java/co/elastic/apm/agent/log/shader/CorrelationIdMapAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package co.elastic.apm.agent.log.shader; | ||
|
||
import co.elastic.apm.agent.impl.GlobalTracer; | ||
import co.elastic.apm.agent.impl.Tracer; | ||
import co.elastic.apm.agent.impl.transaction.Transaction; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.AbstractMap; | ||
import java.util.AbstractSet; | ||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
import java.util.Set; | ||
import java.util.concurrent.Callable; | ||
|
||
public class CorrelationIdMapAdapter extends AbstractMap<String, String> { | ||
|
||
public static final String TRACE_ID = "trace.id"; | ||
public static final String TRANSACTION_ID = "transaction.id"; | ||
public static final String SPAN_ID = "span.id"; | ||
|
||
private static final CorrelationIdMapAdapter INSTANCE = new CorrelationIdMapAdapter(); | ||
private static final Set<Entry<String, String>> ENTRY_SET = new TraceIdentifierEntrySet(); | ||
private static final List<String> ALL_KEYS = Arrays.asList(TRACE_ID, TRANSACTION_ID/*, SPAN_ID*/); | ||
private static final Tracer tracer = GlobalTracer.get(); | ||
private static final List<Entry<String, String>> ENTRIES = Arrays.<Entry<String, String>>asList( | ||
new LazyEntry(TRACE_ID, new Callable<String>() { | ||
@Override | ||
@Nullable | ||
public String call() { | ||
Transaction transaction = tracer.currentTransaction(); | ||
if (transaction == null) { | ||
return null; | ||
} | ||
return transaction.getTraceContext().getTraceId().toString(); | ||
} | ||
}), | ||
new LazyEntry(TRANSACTION_ID, new Callable<String>() { | ||
@Override | ||
@Nullable | ||
public String call() { | ||
Transaction transaction = tracer.currentTransaction(); | ||
if (transaction == null) { | ||
return null; | ||
} | ||
return transaction.getTraceContext().getId().toString(); | ||
} | ||
})/*, | ||
new LazyEntry(SPAN_ID, new Callable<String>() { | ||
@Override | ||
@Nullable | ||
public String call() { | ||
Span span = tracer.getActiveSpan(); | ||
if (span == null) { | ||
return null; | ||
} | ||
return span.getTraceContext().getId().toString(); | ||
} | ||
})*/ | ||
); | ||
|
||
public static Map<String, String> get() { | ||
return INSTANCE; | ||
} | ||
|
||
public static Iterable<String> allKeys() { | ||
return ALL_KEYS; | ||
} | ||
|
||
private CorrelationIdMapAdapter() { | ||
} | ||
|
||
@Override | ||
public Set<Entry<String, String>> entrySet() { | ||
return ENTRY_SET; | ||
} | ||
|
||
private static class TraceIdentifierEntrySet extends AbstractSet<Entry<String, String>> { | ||
|
||
@Override | ||
public int size() { | ||
int size = 0; | ||
for (Entry<String, String> ignored : this) { | ||
size++; | ||
} | ||
return size; | ||
} | ||
|
||
@Override | ||
public Iterator<Entry<String, String>> iterator() { | ||
return new Iterator<Entry<String, String>>() { | ||
private int i = 0; | ||
@Nullable | ||
private Entry<String, String> next = findNext(); | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return next != null; | ||
} | ||
|
||
@Override | ||
public Entry<String, String> next() { | ||
if (next != null) { | ||
try { | ||
return next; | ||
} finally { | ||
next = findNext(); | ||
} | ||
} else { | ||
throw new NoSuchElementException(); | ||
} | ||
} | ||
|
||
@Nullable | ||
private Entry<String, String> findNext() { | ||
Entry<String, String> next = null; | ||
while (next == null && i < ENTRIES.size()) { | ||
next = ENTRIES.get(i++); | ||
if (next.getValue() == null) { | ||
next = null; | ||
} | ||
} | ||
return next; | ||
} | ||
}; | ||
} | ||
|
||
} | ||
|
||
private static class LazyEntry implements Entry<String, String> { | ||
private final String key; | ||
private final Callable<String> valueSupplier; | ||
|
||
private LazyEntry(String key, Callable<String> valueSupplier) { | ||
this.key = key; | ||
this.valueSupplier = valueSupplier; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return this.key; | ||
} | ||
|
||
@Override | ||
public String getValue() { | ||
try { | ||
return this.valueSupplier.call(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public String setValue(String value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that an issue related to us instrumenting our own logging?
If that so, the proper handling is to avoid instrumenting it. I did it through
getClassLoaderMatcher
.I am not opposed to this safety check, but it should be treated as such - log an error before returning
null
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, we'll also want to instrument our logging so that we also have correlation IDs in the agent logs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having correlation IDs within the agent logs is a great idea, but I think the proper way to achieve that is through direct addition to the MDC. We can use an
ActivationListener
for that.Instrumenting classes loaded by the agent class loader can open a can of worms. It would create a plugin CL for it and treat the agent CL as the target CL. This deviates from anything our complicated class loading hierarchy is designed for, so I really think we should avoid that.