-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathTracerFlarePoller.java
More file actions
185 lines (152 loc) · 5.05 KB
/
Copy pathTracerFlarePoller.java
File metadata and controls
185 lines (152 loc) · 5.05 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
176
177
178
179
180
181
182
183
184
185
package datadog.flare;
import com.squareup.moshi.Json;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import datadog.communication.ddagent.SharedCommunicationObjects;
import datadog.remoteconfig.ConfigurationPoller;
import datadog.remoteconfig.PollingRateHinter;
import datadog.remoteconfig.Product;
import datadog.remoteconfig.state.ConfigKey;
import datadog.remoteconfig.state.ProductListener;
import datadog.trace.api.Config;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okio.Okio;
public final class TracerFlarePoller {
private static final String FLARE_LOG_LEVEL = "flare-log-level";
private Runnable stopPreparer;
private Runnable stopSubmitter;
private TracerFlareService tracerFlareService;
private final Map<String, String> configAction = new HashMap<>();
private static TracerFlarePoller INSTANCE;
public static void start(SharedCommunicationObjects sco) {
if (null == INSTANCE) {
INSTANCE = new TracerFlarePoller();
}
INSTANCE.doStart(sco);
}
public static void stop() {
if (null != INSTANCE) {
INSTANCE.doStop();
}
}
private void doStart(SharedCommunicationObjects sco) {
Config config = Config.get();
stopPreparer = new Preparer().register(config, sco);
stopSubmitter = new Submitter().register(config, sco);
tracerFlareService = new TracerFlareService(config, sco.okHttpClient, sco.agentUrl);
}
private void doStop() {
if (null != stopPreparer) {
stopPreparer.run();
}
if (null != stopSubmitter) {
stopSubmitter.run();
}
}
final class Preparer implements ProductListener {
private final JsonAdapter<AgentConfigLayer> AGENT_CONFIG_LAYER_ADAPTER;
{
Moshi MOSHI = new Moshi.Builder().build();
AGENT_CONFIG_LAYER_ADAPTER = MOSHI.adapter(AgentConfigLayer.class);
}
public Runnable register(Config config, SharedCommunicationObjects sco) {
ConfigurationPoller poller = sco.configurationPoller(config);
if (null != poller) {
poller.addListener(Product.AGENT_CONFIG, this);
return poller::stop;
} else {
return null;
}
}
@Override
public void accept(ConfigKey configKey, byte[] content, PollingRateHinter hinter)
throws IOException {
AgentConfigLayer agentConfigLayer =
AGENT_CONFIG_LAYER_ADAPTER.fromJson(
Okio.buffer(Okio.source(new ByteArrayInputStream(content))));
if (null != agentConfigLayer
&& null != agentConfigLayer.config
&& null != agentConfigLayer.config.logLevel) {
configAction.put(configKey.getConfigId(), FLARE_LOG_LEVEL);
prepareForFlare(agentConfigLayer.config.logLevel);
} else {
cleanupAfterFlare();
}
}
@Override
public void remove(ConfigKey configKey, PollingRateHinter hinter) {
if (configAction.remove(configKey.getConfigId(), FLARE_LOG_LEVEL)) {
cleanupAfterFlare();
}
}
@Override
public void commit(PollingRateHinter hinter) {}
}
final class Submitter implements ProductListener {
private final JsonAdapter<AgentTask> AGENT_TASK_ADAPTER;
{
Moshi MOSHI = new Moshi.Builder().build();
AGENT_TASK_ADAPTER = MOSHI.adapter(AgentTask.class);
}
public Runnable register(Config config, SharedCommunicationObjects sco) {
ConfigurationPoller poller = sco.configurationPoller(config);
if (null != poller) {
poller.addListener(Product.AGENT_TASK, this);
return poller::stop;
} else {
return null;
}
}
@Override
public void accept(ConfigKey configKey, byte[] content, PollingRateHinter hinter)
throws IOException {
AgentTask agentTask =
AGENT_TASK_ADAPTER.fromJson(Okio.buffer(Okio.source(new ByteArrayInputStream(content))));
if (null != agentTask
&& null != agentTask.args
&& "tracer_flare".equals(agentTask.taskType)) {
sendFlare(agentTask.args);
}
}
@Override
public void remove(ConfigKey configKey, PollingRateHinter hinter) {}
@Override
public void commit(PollingRateHinter hinter) {}
}
void prepareForFlare(String logLevel) {
tracerFlareService.prepareForFlare(logLevel);
}
void cleanupAfterFlare() {
tracerFlareService.cleanupAfterFlare();
}
void sendFlare(AgentTaskArgs args) {
tracerFlareService.sendFlare(args.caseId, args.userHandle, args.hostname);
}
static final class AgentConfigLayer {
@Json(name = "name")
public String name;
@Json(name = "config")
public AgentConfig config;
}
static final class AgentConfig {
@Json(name = "log_level")
public String logLevel;
}
static final class AgentTask {
@Json(name = "task_type")
public String taskType;
@Json(name = "args")
public AgentTaskArgs args;
}
static final class AgentTaskArgs {
@Json(name = "case_id")
public String caseId;
@Json(name = "user_handle")
public String userHandle;
@Json(name = "hostname")
public String hostname;
}
}