1
1
package datadog .trace .bootstrap ;
2
2
3
+ import datadog .json .JsonWriter ;
3
4
import java .io .IOException ;
4
5
import java .io .OutputStream ;
6
+ import java .util .ArrayList ;
7
+ import java .util .List ;
5
8
import java .util .concurrent .TimeUnit ;
6
9
7
10
/** Thread safe telemetry class used to relay information about tracer activation. */
8
11
public abstract class BootstrapInitializationTelemetry {
9
12
/** Returns a singleton no op instance of initialization telemetry */
10
- public static final BootstrapInitializationTelemetry noOpInstance () {
13
+ public static BootstrapInitializationTelemetry noOpInstance () {
11
14
return NoOp .INSTANCE ;
12
15
}
13
16
@@ -17,8 +20,7 @@ public static final BootstrapInitializationTelemetry noOpInstance() {
17
20
*
18
21
* @param forwarderPath - a String - path to forwarding executable
19
22
*/
20
- public static final BootstrapInitializationTelemetry createFromForwarderPath (
21
- String forwarderPath ) {
23
+ public static BootstrapInitializationTelemetry createFromForwarderPath (String forwarderPath ) {
22
24
return new JsonBased (new ForwarderJsonSender (forwarderPath ));
23
25
}
24
26
@@ -85,112 +87,101 @@ public void finish() {}
85
87
public static final class JsonBased extends BootstrapInitializationTelemetry {
86
88
private final JsonSender sender ;
87
89
88
- private JsonBuffer metaBuffer = new JsonBuffer () ;
89
- private JsonBuffer pointsBuffer = new JsonBuffer () ;
90
+ private final List < String > meta ;
91
+ private final List < String > points ;
90
92
91
93
// one way false to true
92
94
private volatile boolean incomplete = false ;
93
95
94
96
JsonBased (JsonSender sender ) {
95
97
this .sender = sender ;
98
+ this .meta = new ArrayList <>();
99
+ this .points = new ArrayList <>();
96
100
}
97
101
98
102
@ Override
99
103
public void initMetaInfo (String attr , String value ) {
100
- synchronized (metaBuffer ) {
101
- metaBuffer .name (attr ).value (value );
104
+ synchronized (this .meta ) {
105
+ this .meta .add (attr );
106
+ this .meta .add (value );
102
107
}
103
108
}
104
109
105
110
@ Override
106
111
public void onAbort (String reasonCode ) {
107
- onPoint ("library_entrypoint.abort" , "reason:" + reasonCode );
108
-
112
+ synchronized (this .points ) {
113
+ this .points .add ("library_entrypoint.abort" );
114
+ this .points .add ("reason:" + reasonCode );
115
+ }
109
116
markIncomplete ();
110
117
}
111
118
112
119
@ Override
113
120
public void onError (Throwable t ) {
114
- onPoint ("library_entrypoint.error" , "error_type:" + t .getClass ().getName ());
121
+ synchronized (this .points ) {
122
+ this .points .add ("library_entrypoint.error" );
123
+ this .points .add ("error_type:" + t .getClass ().getName ());
124
+ }
115
125
}
116
126
117
127
@ Override
118
128
public void onFatalError (Throwable t ) {
119
129
onError (t );
120
-
121
130
markIncomplete ();
122
131
}
123
132
124
133
@ Override
125
134
public void onError (String reasonCode ) {
126
- onPoint ("library_entrypoint.error" , "error_type:" + reasonCode );
135
+ synchronized (this .points ) {
136
+ this .points .add ("library_entrypoint.error" );
137
+ this .points .add ("error_type:" + reasonCode );
138
+ }
127
139
}
128
140
129
141
@ Override
130
142
public void markIncomplete () {
131
- incomplete = true ;
132
- }
133
-
134
- void onPoint (String pointName ) {
135
- synchronized (pointsBuffer ) {
136
- pointsBuffer .beginObject ();
137
- pointsBuffer .name ("name" ).value (pointName );
138
- pointsBuffer .endObject ();
139
- }
140
- }
141
-
142
- void onPoint (String pointName , String tag ) {
143
- synchronized (pointsBuffer ) {
144
- pointsBuffer .beginObject ();
145
- pointsBuffer .name ("name" ).value (pointName );
146
- pointsBuffer .name ("tags" ).array (tag );
147
- pointsBuffer .endObject ();
148
- }
149
- }
150
-
151
- void onPoint (String pointName , String [] tags ) {
152
- synchronized (pointsBuffer ) {
153
- pointsBuffer .beginObject ();
154
- pointsBuffer .name ("name" ).value (pointName );
155
- pointsBuffer .name ("tags" ).array (tags );
156
- pointsBuffer .endObject ();
157
- }
143
+ this .incomplete = true ;
158
144
}
159
145
160
146
@ Override
161
147
public void finish () {
162
- if (!incomplete ) {
163
- onPoint ("library_entrypoint.complete" );
164
- }
165
-
166
- JsonBuffer buffer = new JsonBuffer ();
167
- buffer .beginObject ();
168
-
169
- buffer .name ("metadata" );
170
- synchronized (metaBuffer ) {
171
- buffer .object (metaBuffer );
172
- }
173
-
174
- buffer .name ("points" );
175
- synchronized (pointsBuffer ) {
176
- buffer .array (pointsBuffer );
177
-
178
- pointsBuffer .reset ();
179
- }
180
-
181
- buffer .endObject ();
182
-
183
- try {
184
- sender .send (buffer );
148
+ try (JsonWriter writer = new JsonWriter ()) {
149
+ writer .beginObject ();
150
+ writer .name ("metadata" ).beginObject ();
151
+ synchronized (this .meta ) {
152
+ for (int i = 0 ; i + 1 < this .meta .size (); i = i + 2 ) {
153
+ writer .name (this .meta .get (i ));
154
+ writer .value (this .meta .get (i + 1 ));
155
+ }
156
+ }
157
+ writer .endObject ();
158
+
159
+ writer .name ("points" ).beginArray ();
160
+ synchronized (this .points ) {
161
+ for (int i = 0 ; i + 1 < this .points .size (); i = i + 2 ) {
162
+ writer .beginObject ();
163
+ writer .name ("name" ).value (this .points .get (i ));
164
+ writer .name ("tags" ).beginArray ().value (this .points .get (i + 1 )).endArray ();
165
+ writer .endObject ();
166
+ }
167
+ this .points .clear ();
168
+ }
169
+ if (!this .incomplete ) {
170
+ writer .beginObject ().name ("name" ).value ("library_entrypoint.complete" ).endObject ();
171
+ }
172
+ writer .endArray ();
173
+ writer .endObject ();
174
+
175
+ this .sender .send (writer .toByteArray ());
185
176
} catch (Throwable t ) {
186
177
// Since this is the reporting mechanism, there's little recourse here
187
178
// Decided to simply ignore - arguably might want to write to stderr
188
179
}
189
180
}
190
181
}
191
182
192
- public static interface JsonSender {
193
- public abstract void send (JsonBuffer buffer ) throws IOException ;
183
+ public interface JsonSender {
184
+ void send (byte [] payload ) throws IOException ;
194
185
}
195
186
196
187
public static final class ForwarderJsonSender implements JsonSender {
@@ -201,12 +192,12 @@ public static final class ForwarderJsonSender implements JsonSender {
201
192
}
202
193
203
194
@ Override
204
- public void send (JsonBuffer buffer ) throws IOException {
195
+ public void send (byte [] payload ) throws IOException {
205
196
ProcessBuilder builder = new ProcessBuilder (forwarderPath , "library_entrypoint" );
206
197
207
198
Process process = builder .start ();
208
199
try (OutputStream out = process .getOutputStream ()) {
209
- out .write (buffer . toByteArray () );
200
+ out .write (payload );
210
201
}
211
202
212
203
try {
0 commit comments