1717import datadog .trace .api .DDTags ;
1818import datadog .trace .bootstrap .config .provider .ConfigProvider ;
1919import datadog .trace .util .PidHelper ;
20- import datadog .trace .util .RandomUtils ;
2120import de .thetaphi .forbiddenapis .SuppressForbidden ;
21+ import edu .umd .cs .findbugs .annotations .NonNull ;
2222import java .io .*;
2323import java .nio .charset .Charset ;
2424import java .nio .charset .StandardCharsets ;
@@ -65,30 +65,36 @@ public final class CrashUploader {
6565 MediaType .parse ("application/octet-stream" );
6666
6767 private final Config config ;
68+ private final ConfigManager .StoredConfig storedConfig ;
6869
6970 private final OkHttpClient telemetryClient ;
7071 private final HttpUrl telemetryUrl ;
7172 private final boolean agentless ;
7273 private final String tags ;
7374
74- public CrashUploader () {
75- this (Config .get ());
75+ public CrashUploader (@ Nonnull final ConfigManager . StoredConfig storedConfig ) {
76+ this (Config .get (), storedConfig );
7677 }
7778
78- CrashUploader (final Config config ) {
79+ CrashUploader (
80+ @ NonNull final Config config , @ Nonnull final ConfigManager .StoredConfig storedConfig ) {
7981 this .config = config ;
82+ this .storedConfig = storedConfig ;
8083
8184 telemetryUrl = HttpUrl .get (config .getFinalCrashTrackingTelemetryUrl ());
8285 agentless = config .isCrashTrackingAgentless ();
8386
84- final Map <String , String > tagsMap = new HashMap <>(config .getMergedCrashTrackingTags ());
85- tagsMap .put (VersionInfo .LIBRARY_VERSION_TAG , VersionInfo .VERSION );
87+ final StringBuilder tagsBuilder =
88+ new StringBuilder (storedConfig .tags != null ? storedConfig .tags : "" );
89+ if (!tagsBuilder .toString ().isEmpty ()) {
90+ tagsBuilder .append ("," );
91+ }
92+ tagsBuilder .append (VersionInfo .LIBRARY_VERSION_TAG ).append ('=' ).append (VersionInfo .VERSION );
8693 // PID can be empty if we cannot find it out from the system
8794 if (!PidHelper .getPid ().isEmpty ()) {
88- tagsMap . put ( DDTags .PID_TAG , PidHelper .getPid ());
95+ tagsBuilder . append ( "," ). append ( DDTags .PID_TAG ). append ( '=' ). append ( PidHelper .getPid ());
8996 }
90- // Comma separated tags string for V2.4 format
91- tags = tagsToString (tagsMap );
97+ tags = tagsBuilder .toString ();
9298
9399 ConfigProvider configProvider = config .configProvider ();
94100
@@ -108,13 +114,6 @@ public CrashUploader() {
108114 CRASH_TRACKING_UPLOAD_TIMEOUT , CRASH_TRACKING_UPLOAD_TIMEOUT_DEFAULT )));
109115 }
110116
111- private String tagsToString (final Map <String , String > tags ) {
112- return tags .entrySet ().stream ()
113- .filter (e -> e .getValue () != null && !e .getValue ().isEmpty ())
114- .map (e -> e .getKey () + ":" + e .getValue ())
115- .collect (Collectors .joining ("," ));
116- }
117-
118117 public void upload (@ Nonnull List <Path > files ) throws IOException {
119118 for (Path file : files ) {
120119 uploadToLogs (file );
@@ -141,7 +140,9 @@ void uploadToLogs(@Nonnull String message, @Nonnull PrintStream out) throws IOEx
141140 writer .name ("ddsource" ).value ("crashtracker" );
142141 writer .name ("ddtags" ).value (tags );
143142 writer .name ("hostname" ).value (config .getHostName ());
144- writer .name ("service" ).value (config .getServiceName ());
143+ writer .name ("service" ).value (storedConfig .service );
144+ writer .name ("version" ).value (storedConfig .version );
145+ writer .name ("env" ).value (storedConfig .env );
145146 writer .name ("message" ).value (message );
146147 writer .name ("level" ).value ("ERROR" );
147148 writer .name ("error" );
@@ -282,11 +283,7 @@ private RequestBody makeTelemetryRequestBody(@Nonnull String content) throws IOE
282283 writer .beginObject ();
283284 writer .name ("api_version" ).value (TELEMETRY_API_VERSION );
284285 writer .name ("request_type" ).value ("logs" );
285- writer
286- .name ("runtime_id" )
287- // this is unknowable at this point because the process has crashed
288- // though we may be able to save it in the tmpdir
289- .value (RandomUtils .randomUUID ().toString ());
286+ writer .name ("runtime_id" ).value (storedConfig .runtimeId );
290287 writer .name ("tracer_time" ).value (Instant .now ().getEpochSecond ());
291288 writer .name ("seq_id" ).value (1 );
292289 writer .name ("debug" ).value (true );
@@ -303,22 +300,25 @@ private RequestBody makeTelemetryRequestBody(@Nonnull String content) throws IOE
303300 writer .endArray ();
304301 writer .name ("application" );
305302 writer .beginObject ();
306- writer .name ("env" ).value (config . getEnv () );
303+ writer .name ("env" ).value (storedConfig . env );
307304 writer .name ("language_name" ).value ("jvm" );
308305 writer
309306 .name ("language_version" )
310307 .value (SystemProperties .getOrDefault ("java.version" , "unknown" ));
311- writer .name ("service_name" ).value (config . getServiceName () );
312- writer .name ("service_version" ).value (config . getVersion () );
308+ writer .name ("service_name" ).value (storedConfig . service );
309+ writer .name ("service_version" ).value (storedConfig . version );
313310 writer .name ("tracer_version" ).value (VersionInfo .VERSION );
311+ if (storedConfig .processTags != null ) {
312+ writer .name ("process_tags" ).value (storedConfig .processTags );
313+ }
314314 writer .endObject ();
315315 writer .name ("host" );
316316 writer .beginObject ();
317317 if (ContainerInfo .get ().getContainerId () != null ) {
318318 writer .name ("container_id" ).value (ContainerInfo .get ().getContainerId ());
319319 }
320320 writer .name ("hostname" ).value (config .getHostName ());
321- writer .name ("env" ).value (config . getEnv () );
321+ writer .name ("env" ).value (storedConfig . env );
322322 writer .endObject ();
323323 writer .endObject ();
324324 }
@@ -327,23 +327,11 @@ private RequestBody makeTelemetryRequestBody(@Nonnull String content) throws IOE
327327 }
328328 }
329329
330- private String readContent (InputStream file ) throws IOException {
331- try (InputStreamReader reader = new InputStreamReader (file , StandardCharsets .UTF_8 )) {
332- int read ;
333- char [] buffer = new char [1 << 14 ];
334- StringBuilder sb = new StringBuilder ();
335- while ((read = reader .read (buffer , 0 , buffer .length )) > 0 ) {
336- sb .append (buffer , 0 , read );
337- }
338- return sb .toString ();
339- }
340- }
341-
342330 private void handleCall (final Call call ) {
343331 try (Response response = call .execute ()) {
344332 handleSuccess (call , response );
345333 } catch (IOException e ) {
346- handleFailure (call , e );
334+ handleFailure (e );
347335 }
348336 }
349337
@@ -364,7 +352,7 @@ private void handleSuccess(final Call call, final Response response) throws IOEx
364352 }
365353 }
366354
367- private void handleFailure (final Call call , final IOException exception ) {
368- log .error ("Failed to upload crash files, got exception: {}" , exception . getMessage () , exception );
355+ private void handleFailure (final IOException exception ) {
356+ log .error ("Failed to upload crash files, got exception" , exception );
369357 }
370358}
0 commit comments