1616package io .micrometer .dynatrace .v2 ;
1717
1818import com .dynatrace .metric .util .*;
19+
1920import io .micrometer .core .instrument .*;
2021import io .micrometer .core .instrument .distribution .HistogramSnapshot ;
2122import io .micrometer .core .instrument .distribution .ValueAtPercentile ;
2223import io .micrometer .core .instrument .util .AbstractPartition ;
24+ import io .micrometer .core .instrument .util .StringUtils ;
2325import io .micrometer .core .ipc .http .HttpSender ;
2426import io .micrometer .core .util .internal .logging .InternalLogger ;
2527import io .micrometer .core .util .internal .logging .InternalLoggerFactory ;
28+ import io .micrometer .core .util .internal .logging .WarnThenDebugLogger ;
2629import io .micrometer .dynatrace .AbstractDynatraceExporter ;
2730import io .micrometer .dynatrace .DynatraceConfig ;
2831import io .micrometer .dynatrace .types .DynatraceSummarySnapshot ;
@@ -55,8 +58,11 @@ public final class DynatraceExporterV2 extends AbstractDynatraceExporter {
5558 private static final Pattern EXTRACT_LINES_OK = Pattern .compile ("\" linesOk\" :\\ s?(\\ d+)" );
5659 private static final Pattern EXTRACT_LINES_INVALID = Pattern .compile ("\" linesInvalid\" :\\ s?(\\ d+)" );
5760 private static final Pattern IS_NULL_ERROR_RESPONSE = Pattern .compile ("\" error\" :\\ s?null" );
61+ private static final int LOG_RESPONSE_BODY_TRUNCATION_LIMIT = 1_000 ;
62+ private static final String LOG_RESPONSE_BODY_TRUNCATION_INDICATOR = " (truncated)" ;
5863
5964 private final InternalLogger logger = InternalLoggerFactory .getInstance (DynatraceExporterV2 .class );
65+ private static final WarnThenDebugLogger warnThenDebugLoggerSendStack = new WarnThenDebugLogger (DynatraceExporterV2 .class );
6066 private static final Map <String , String > staticDimensions = Collections .singletonMap ("dt.metrics.source" , "micrometer" );
6167
6268 private final MetricBuilderFactory metricBuilderFactory ;
@@ -310,9 +316,12 @@ private void send(List<String> metricLines) {
310316 .withPlainText (body )
311317 .send ()
312318 .onSuccess (response -> handleSuccess (metricLines .size (), response ))
313- .onError (response -> logger .error ("Failed metric ingestion: Error Code={}, Response Body={}" , response .code (), response .body ()));
319+ .onError (response -> logger .error ("Failed metric ingestion: Error Code={}, Response Body={}" ,
320+ response .code (),
321+ StringUtils .truncate (response .body (), LOG_RESPONSE_BODY_TRUNCATION_LIMIT , LOG_RESPONSE_BODY_TRUNCATION_INDICATOR )));
314322 } catch (Throwable throwable ) {
315- logger .error ("Failed metric ingestion: " + throwable .getMessage (), throwable );
323+ logger .warn ("Failed metric ingestion: " + throwable );
324+ warnThenDebugLoggerSendStack .log ("Stack trace for previous 'Failed metric ingestion' warning log: " , throwable );
316325 }
317326 }
318327
@@ -325,16 +334,18 @@ private void handleSuccess(int totalSent, HttpSender.Response response) {
325334 logger .debug ("Sent {} metric lines, linesOk: {}, linesInvalid: {}." ,
326335 totalSent , linesOkMatchResult .group (1 ), linesInvalidMatchResult .group (1 ));
327336 } else {
328- logger .warn ("Unable to parse response: {}" , response .body ());
337+ logger .warn ("Unable to parse response: {}" ,
338+ StringUtils .truncate (response .body (), LOG_RESPONSE_BODY_TRUNCATION_LIMIT , LOG_RESPONSE_BODY_TRUNCATION_INDICATOR ));
329339 }
330340 } else {
331- logger .warn ("Unable to parse response: {}" , response .body ());
341+ logger .warn ("Unable to parse response: {}" ,
342+ StringUtils .truncate (response .body (), LOG_RESPONSE_BODY_TRUNCATION_LIMIT , LOG_RESPONSE_BODY_TRUNCATION_INDICATOR ));
332343 }
333344 } else {
334345 // common pitfall if URI is supplied in V1 format (without endpoint path)
335346 logger .error ("Expected status code 202, got {}.\n Response Body={}\n Did you specify the ingest path (e.g.: /api/v2/metrics/ingest)?" ,
336347 response .code (),
337- response .body ()
348+ StringUtils . truncate ( response .body (), LOG_RESPONSE_BODY_TRUNCATION_LIMIT , LOG_RESPONSE_BODY_TRUNCATION_INDICATOR )
338349 );
339350 }
340351 }
0 commit comments