11package datadog .trace .bootstrap ;
22
3+ import datadog .json .JsonWriter ;
34import java .io .IOException ;
45import java .io .OutputStream ;
6+ import java .util .ArrayList ;
7+ import java .util .List ;
58import java .util .concurrent .TimeUnit ;
69
710/** Thread safe telemetry class used to relay information about tracer activation. */
811public abstract class BootstrapInitializationTelemetry {
912 /** Returns a singleton no op instance of initialization telemetry */
10- public static final BootstrapInitializationTelemetry noOpInstance () {
13+ public static BootstrapInitializationTelemetry noOpInstance () {
1114 return NoOp .INSTANCE ;
1215 }
1316
@@ -17,8 +20,7 @@ public static final BootstrapInitializationTelemetry noOpInstance() {
1720 *
1821 * @param forwarderPath - a String - path to forwarding executable
1922 */
20- public static final BootstrapInitializationTelemetry createFromForwarderPath (
21- String forwarderPath ) {
23+ public static BootstrapInitializationTelemetry createFromForwarderPath (String forwarderPath ) {
2224 return new JsonBased (new ForwarderJsonSender (forwarderPath ));
2325 }
2426
@@ -85,112 +87,101 @@ public void finish() {}
8587 public static final class JsonBased extends BootstrapInitializationTelemetry {
8688 private final JsonSender sender ;
8789
88- private JsonBuffer metaBuffer = new JsonBuffer () ;
89- private JsonBuffer pointsBuffer = new JsonBuffer () ;
90+ private final List < String > meta ;
91+ private final List < String > points ;
9092
9193 // one way false to true
9294 private volatile boolean incomplete = false ;
9395
9496 JsonBased (JsonSender sender ) {
9597 this .sender = sender ;
98+ this .meta = new ArrayList <>();
99+ this .points = new ArrayList <>();
96100 }
97101
98102 @ Override
99103 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 );
102107 }
103108 }
104109
105110 @ Override
106111 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+ }
109116 markIncomplete ();
110117 }
111118
112119 @ Override
113120 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+ }
115125 }
116126
117127 @ Override
118128 public void onFatalError (Throwable t ) {
119129 onError (t );
120-
121130 markIncomplete ();
122131 }
123132
124133 @ Override
125134 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+ }
127139 }
128140
129141 @ Override
130142 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 ;
158144 }
159145
160146 @ Override
161147 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 ());
185176 } catch (Throwable t ) {
186177 // Since this is the reporting mechanism, there's little recourse here
187178 // Decided to simply ignore - arguably might want to write to stderr
188179 }
189180 }
190181 }
191182
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 ;
194185 }
195186
196187 public static final class ForwarderJsonSender implements JsonSender {
@@ -201,12 +192,12 @@ public static final class ForwarderJsonSender implements JsonSender {
201192 }
202193
203194 @ Override
204- public void send (JsonBuffer buffer ) throws IOException {
195+ public void send (byte [] payload ) throws IOException {
205196 ProcessBuilder builder = new ProcessBuilder (forwarderPath , "library_entrypoint" );
206197
207198 Process process = builder .start ();
208199 try (OutputStream out = process .getOutputStream ()) {
209- out .write (buffer . toByteArray () );
200+ out .write (payload );
210201 }
211202
212203 try {
0 commit comments