-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathContextSetter.java
More file actions
72 lines (60 loc) · 2.03 KB
/
Copy pathContextSetter.java
File metadata and controls
72 lines (60 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.datadoghq.profiler;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ContextSetter {
private static final int TAGS_STORAGE_LIMIT = 10;
private final List<String> attributes;
private final JavaProfiler profiler;
public ContextSetter(JavaProfiler profiler, List<String> attributes) {
this.profiler = profiler;
Set<String> unique = new HashSet<>(attributes);
this.attributes = new ArrayList<>(unique.size());
for (int i = 0; i < Math.min(attributes.size(), TAGS_STORAGE_LIMIT); i++) {
String attribute = attributes.get(i);
if (unique.remove(attribute)) {
this.attributes.add(attribute);
}
}
}
public int[] snapshotTags() {
int[] snapshot = new int[attributes.size()];
snapshotTags(snapshot);
return snapshot;
}
public void snapshotTags(int[] snapshot) {
if (snapshot.length <= attributes.size()) {
profiler.copyTags(snapshot);
}
}
public int offsetOf(String attribute) {
return attributes.indexOf(attribute);
}
public boolean setContextValue(String attribute, String value) {
return setContextValue(offsetOf(attribute), value);
}
public boolean setContextValue(int offset, String value) {
if (offset >= 0) {
return profiler.setContextAttribute(offset, value);
}
return false;
}
public boolean clearContextValue(String attribute) {
return clearContextValue(offsetOf(attribute));
}
public boolean clearContextValue(int offset) {
if (offset >= 0) {
profiler.clearContextAttribute(offset);
return true;
}
return false;
}
public String readContextValue(int offset) {
if (offset < 0) {
return null;
}
ThreadContext ctx = profiler.getThreadContext();
return ctx != null ? ctx.readContextAttribute(offset) : null;
}
}