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

Added support for collecting statistics about dropped exit spans #2505

Merged
merged 7 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -18,7 +18,11 @@
*/
package co.elastic.apm.agent.impl.transaction;

import co.elastic.apm.agent.objectpool.Allocator;
import co.elastic.apm.agent.objectpool.ObjectPool;
import co.elastic.apm.agent.objectpool.Recyclable;
import co.elastic.apm.agent.objectpool.impl.QueueBasedObjectPool;
import org.jctools.queues.atomic.MpmcAtomicArrayQueue;

import java.util.Iterator;
import java.util.Map;
Expand All @@ -28,15 +32,20 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

public class DroppedSpanStats implements Iterable<Map.Entry<DroppedSpanStats.StatsKey, DroppedSpanStats.StatsValue>>, Recyclable {
public class DroppedSpanStats implements Iterable<Map.Entry<DroppedSpanStats.StatsKey, DroppedSpanStats.Stats>>, Recyclable {

public static class StatsKey {
private final String destinationServiceResource;
private final Outcome outcome;
public static class StatsKey implements Recyclable {
private String destinationServiceResource;
private Outcome outcome;

public StatsKey(CharSequence destinationServiceResource, Outcome outcome) {
public StatsKey() {

}

public StatsKey init(CharSequence destinationServiceResource, Outcome outcome) {
this.destinationServiceResource = destinationServiceResource.toString();
this.outcome = outcome;
return this;
}

public String getDestinationServiceResource() {
Expand All @@ -47,6 +56,12 @@ public Outcome getOutcome() {
return outcome;
}

@Override
public void resetState() {
destinationServiceResource = null;
outcome = null;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -61,7 +76,7 @@ public int hashCode() {
}
}

public static class StatsValue {
public static class Stats implements Recyclable {
private final AtomicInteger count = new AtomicInteger(0);
private final AtomicLong sum = new AtomicLong(0L);

Expand All @@ -72,12 +87,33 @@ public int getCount() {
public long getSum() {
return sum.get();
}

@Override
public void resetState() {
count.set(0);
sum.set(0L);
}
}

private final ConcurrentMap<StatsKey, StatsValue> statsMap = new ConcurrentHashMap<>();
private static final ObjectPool<StatsKey> statsKeyObjectPool = QueueBasedObjectPool.<StatsKey>ofRecyclable(new MpmcAtomicArrayQueue<StatsKey>(512), false, new Allocator<StatsKey>() {
tobiasstadler marked this conversation as resolved.
Show resolved Hide resolved
@Override
public StatsKey createInstance() {
return new StatsKey();
}
});

private static ObjectPool<Stats> statsObjectPool = QueueBasedObjectPool.<Stats>ofRecyclable(new MpmcAtomicArrayQueue<Stats>(512), false, new Allocator<Stats>() {
tobiasstadler marked this conversation as resolved.
Show resolved Hide resolved
@Override
public Stats createInstance() {
return new Stats();
}
});

StatsValue getStats(String destinationServiceResource, Outcome outcome) {
return statsMap.get(new StatsKey(destinationServiceResource, outcome));
private final ConcurrentMap<StatsKey, Stats> statsMap = new ConcurrentHashMap<>();

//only used during testing
Stats getStats(String destinationServiceResource, Outcome outcome) {
return statsMap.get(new StatsKey().init(destinationServiceResource, outcome));
}

public void captureDroppedSpan(Span span) {
Expand All @@ -86,7 +122,8 @@ public void captureDroppedSpan(Span span) {
return;
}

StatsValue stats = getStats(new StatsKey(resource, span.getOutcome()));
StatsKey statsKey = statsKeyObjectPool.createInstance().init(resource, span.getOutcome());
Stats stats = getOrCreateStats(statsKey);
if (stats == null) {
return;
}
Expand All @@ -99,33 +136,39 @@ public void captureDroppedSpan(Span span) {
stats.sum.addAndGet(span.getDuration());
}

private StatsValue getStats(StatsKey statsKey) {
StatsValue statsValue = statsMap.get(statsKey);
if (statsValue != null) {
return statsValue;
private Stats getOrCreateStats(StatsKey statsKey) {
tobiasstadler marked this conversation as resolved.
Show resolved Hide resolved
Stats stats = statsMap.get(statsKey);
if (stats != null) {
statsKeyObjectPool.recycle(statsKey);
return stats;
}

synchronized (this) {
statsValue = statsMap.get(statsKey);
if (statsValue != null) {
return statsValue;
stats = statsMap.get(statsKey);
if (stats != null) {
statsKeyObjectPool.recycle(statsKey);
return stats;
}
if (statsMap.size() < 128) {
statsValue = new StatsValue();
statsMap.put(statsKey, statsValue);
return statsValue;
stats = statsObjectPool.createInstance();
statsMap.put(statsKey, stats);
return stats;
}
}
return null;
}

@Override
public Iterator<Map.Entry<StatsKey, StatsValue>> iterator() {
public Iterator<Map.Entry<StatsKey, Stats>> iterator() {
return statsMap.entrySet().iterator();
}

@Override
public void resetState() {
for (Map.Entry<StatsKey, Stats> e : statsMap.entrySet()) {
statsKeyObjectPool.recycle(e.getKey());
statsObjectPool.recycle(e.getValue());
}
statsMap.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ private void serializeSpanCount(final SpanCount spanCount) {
private void serializeDroppedSpanStats(final DroppedSpanStats droppedSpanStats) {
writeFieldName("dropped_spans_stats");
jw.writeByte(ARRAY_START);
for (Map.Entry<DroppedSpanStats.StatsKey, DroppedSpanStats.StatsValue> stats : droppedSpanStats) {
for (Map.Entry<DroppedSpanStats.StatsKey, DroppedSpanStats.Stats> stats : droppedSpanStats) {
jw.writeByte(OBJECT_START);
writeField("destination_service_resource", stats.getKey().getDestinationServiceResource());
writeField("outcome", stats.getKey().getOutcome().toString());
Expand Down