-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathRumInjector.java
More file actions
175 lines (158 loc) · 5.03 KB
/
RumInjector.java
File metadata and controls
175 lines (158 loc) · 5.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package datadog.trace.api.rum;
import datadog.trace.api.Config;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.cache.DDCache;
import datadog.trace.api.cache.DDCaches;
import java.util.function.Function;
import javax.annotation.Nullable;
public final class RumInjector {
private static final RumInjector INSTANCE =
new RumInjector(Config.get(), InstrumenterConfig.get());
private static final String MARKER = "</head>";
private static final char[] MARKER_CHARS = MARKER.toCharArray();
private static final Function<String, byte[]> MARKER_BYTES =
charset -> {
try {
return MARKER.getBytes(charset);
} catch (Throwable t) {
return null;
}
};
private final boolean enabled;
private final String snippet;
private final char[] snippetChars;
private final DDCache<String, byte[]> snippetCache;
private final DDCache<String, byte[]> markerCache;
private final Function<String, byte[]> snippetBytes;
private static volatile RumTelemetryCollector telemetryCollector = RumTelemetryCollector.NO_OP;
RumInjector(Config config, InstrumenterConfig instrumenterConfig) {
boolean rumEnabled = instrumenterConfig.isRumEnabled();
RumInjectorConfig injectorConfig = config.getRumInjectorConfig();
// If both RUM is enabled and injector config is valid
if (rumEnabled && injectorConfig != null) {
this.enabled = true;
this.snippet = injectorConfig.getSnippet();
this.snippetCache = DDCaches.newFixedSizeCache(16);
this.markerCache = DDCaches.newFixedSizeCache(16);
this.snippetChars = this.snippet.toCharArray();
this.snippetBytes =
charset -> {
try {
return snippet.getBytes(charset);
} catch (Throwable t) {
return null;
}
};
} else {
this.enabled = false;
this.snippet = null;
this.snippetCache = null;
this.markerCache = null;
this.snippetBytes = null;
this.snippetChars = null;
}
}
public static RumInjector get() {
return INSTANCE;
}
/**
* Checks whether RUM injection is enabled and ready to inject.
*
* @return {@code true} if enabled, {@code otherwise}.
*/
public boolean isEnabled() {
return this.enabled;
}
/**
* Gets the HTML snippet to inject RUM SDK
*
* @return The HTML snippet chars to inject, {@code null} if RUM injection is disabled.
*/
@Nullable
public char[] getSnippetChars() {
if (!this.enabled) {
return null;
}
return this.snippetChars;
}
/**
* Gets the HTML snippet to inject RUM SDK
*
* @return The HTML snippet to inject, {@code null} if RUM injection is disabled.
*/
@Nullable
public byte[] getSnippetBytes(String encoding) {
if (!this.enabled) {
return null;
}
return this.snippetCache.computeIfAbsent(encoding, this.snippetBytes);
}
/**
* Gets the marker chars to inject RUM SDK after.
*
* @return The marker chars, {@code null} if RUM injection is disabled.
*/
@Nullable
public char[] getMarkerChars() {
if (!this.enabled) {
return null;
}
return MARKER_CHARS;
}
/**
* Gets the marker bytes to inject RUM SDK after.
*
* @param encoding The encoding to get the marker bytes from.
* @return The marker bytes, {@code null} if RUM injection is disabled.
*/
@Nullable
public byte[] getMarkerBytes(String encoding) {
if (!this.enabled) {
return null;
}
return this.markerCache.computeIfAbsent(encoding, MARKER_BYTES);
}
/** Starts telemetry collection if RUM injection is enabled. */
public static void enableTelemetry() {
if (INSTANCE.isEnabled()) {
RumInjectorMetrics metrics = new RumInjectorMetrics();
telemetryCollector = metrics;
telemetryCollector.onInitializationSucceed();
} else {
telemetryCollector = RumTelemetryCollector.NO_OP;
}
}
/** Shuts down telemetry collection and resets the telemetry collector to NO_OP. */
public static void shutdownTelemetry() {
telemetryCollector.close();
telemetryCollector = RumTelemetryCollector.NO_OP;
}
/**
* Sets the telemetry collector. This is used for testing purposes only.
*
* @param collector The telemetry collector to set or {@code null} to reset to NO_OP.
*/
public static void setTelemetryCollector(RumTelemetryCollector collector) {
telemetryCollector = collector != null ? collector : RumTelemetryCollector.NO_OP;
}
/**
* Gets the telemetry collector.
*
* @return The telemetry collector used to report telemetry.
*/
public static RumTelemetryCollector getTelemetryCollector() {
return telemetryCollector;
}
/**
* Gets the concrete RumInjectorMetrics instance.
*
* @return The RumInjectorMetrics instance or null if telemetry is NO_OP.
*/
public static RumInjectorMetrics getMetricsInstance() {
RumTelemetryCollector collector = telemetryCollector;
if (collector instanceof RumInjectorMetrics) {
return (RumInjectorMetrics) collector;
}
return null;
}
}