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,27 +87,29 @@ 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 ) {
107112 onPoint ("library_entrypoint.abort" , "reason:" + reasonCode );
108-
109113 markIncomplete ();
110114 }
111115
@@ -117,7 +121,6 @@ public void onError(Throwable t) {
117121 @ Override
118122 public void onFatalError (Throwable t ) {
119123 onError (t );
120-
121124 markIncomplete ();
122125 }
123126
@@ -126,71 +129,57 @@ public void onError(String reasonCode) {
126129 onPoint ("library_entrypoint.error" , "error_type:" + reasonCode );
127130 }
128131
129- @ Override
130- 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 ();
132+ private void onPoint (String name , String tag ) {
133+ synchronized (this .points ) {
134+ this .points .add (name );
135+ this .points .add (tag );
139136 }
140137 }
141138
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- }
139+ @ Override
140+ public void markIncomplete () {
141+ this .incomplete = true ;
158142 }
159143
160144 @ Override
161145 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 );
146+ try (JsonWriter writer = new JsonWriter ()) {
147+ writer .beginObject ();
148+ writer .name ("metadata" ).beginObject ();
149+ synchronized (this .meta ) {
150+ for (int i = 0 ; i + 1 < this .meta .size (); i = i + 2 ) {
151+ writer .name (this .meta .get (i ));
152+ writer .value (this .meta .get (i + 1 ));
153+ }
154+ }
155+ writer .endObject ();
156+
157+ writer .name ("points" ).beginArray ();
158+ synchronized (this .points ) {
159+ for (int i = 0 ; i + 1 < this .points .size (); i = i + 2 ) {
160+ writer .beginObject ();
161+ writer .name ("name" ).value (this .points .get (i ));
162+ writer .name ("tags" ).beginArray ().value (this .points .get (i + 1 )).endArray ();
163+ writer .endObject ();
164+ }
165+ this .points .clear ();
166+ }
167+ if (!this .incomplete ) {
168+ writer .beginObject ().name ("name" ).value ("library_entrypoint.complete" ).endObject ();
169+ }
170+ writer .endArray ();
171+ writer .endObject ();
172+
173+ this .sender .send (writer .toByteArray ());
185174 } catch (Throwable t ) {
186175 // Since this is the reporting mechanism, there's little recourse here
187176 // Decided to simply ignore - arguably might want to write to stderr
188177 }
189178 }
190179 }
191180
192- public static interface JsonSender {
193- public abstract void send (JsonBuffer buffer ) throws IOException ;
181+ public interface JsonSender {
182+ void send (byte [] payload ) throws IOException ;
194183 }
195184
196185 public static final class ForwarderJsonSender implements JsonSender {
@@ -201,12 +190,12 @@ public static final class ForwarderJsonSender implements JsonSender {
201190 }
202191
203192 @ Override
204- public void send (JsonBuffer buffer ) throws IOException {
193+ public void send (byte [] payload ) throws IOException {
205194 ProcessBuilder builder = new ProcessBuilder (forwarderPath , "library_entrypoint" );
206195
207196 Process process = builder .start ();
208197 try (OutputStream out = process .getOutputStream ()) {
209- out .write (buffer . toByteArray () );
198+ out .write (payload );
210199 }
211200
212201 try {
0 commit comments