30
30
import datadog .trace .api .gateway .Flow ;
31
31
import datadog .trace .api .telemetry .LogCollector ;
32
32
import datadog .trace .api .telemetry .WafMetricCollector ;
33
+ import datadog .trace .api .telemetry .WafTruncatedType ;
33
34
import datadog .trace .api .time .SystemTimeSource ;
34
35
import datadog .trace .bootstrap .instrumentation .api .AgentSpan ;
35
36
import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
@@ -445,6 +446,7 @@ public void onDataAvailable(
445
446
if (!reqCtx .isAdditiveClosed ()) {
446
447
log .error ("Error calling WAF" , e );
447
448
}
449
+ WafMetricCollector .get ().wafRequestError ();
448
450
return ;
449
451
} catch (AbstractPowerwafException e ) {
450
452
if (gwCtx .isRasp ) {
@@ -460,6 +462,27 @@ public void onDataAvailable(
460
462
long elapsed = System .currentTimeMillis () - start ;
461
463
StandardizedLogging .finishedExecutionWAF (log , elapsed );
462
464
}
465
+ if (!gwCtx .isRasp ) {
466
+ PowerwafMetrics wafMetrics = reqCtx .getWafMetrics ();
467
+ if (wafMetrics != null ) {
468
+ final long stringTooLong = wafMetrics .getTruncatedStringTooLongCount ();
469
+ final long listMapTooLarge = wafMetrics .getTruncatedListMapTooLargeCount ();
470
+ final long objectTooDeep = wafMetrics .getTruncatedObjectTooDeepCount ();
471
+
472
+ if (stringTooLong > 0 ) {
473
+ WafMetricCollector .get ()
474
+ .wafInputTruncated (WafTruncatedType .STRING_TOO_LONG , stringTooLong );
475
+ }
476
+ if (listMapTooLarge > 0 ) {
477
+ WafMetricCollector .get ()
478
+ .wafInputTruncated (WafTruncatedType .LIST_MAP_TOO_LARGE , listMapTooLarge );
479
+ }
480
+ if (objectTooDeep > 0 ) {
481
+ WafMetricCollector .get ()
482
+ .wafInputTruncated (WafTruncatedType .OBJECT_TOO_DEEP , objectTooDeep );
483
+ }
484
+ }
485
+ }
463
486
}
464
487
465
488
StandardizedLogging .inAppWafReturn (log , resultWithData );
@@ -495,29 +518,35 @@ public void onDataAvailable(
495
518
}
496
519
} else {
497
520
log .info ("Ignoring action with type {}" , actionInfo .type );
521
+ WafMetricCollector .get ().wafRequestBlockFailure ();
498
522
}
499
523
}
500
524
Collection <AppSecEvent > events = buildEvents (resultWithData );
501
525
502
- if (!events .isEmpty () && !reqCtx .isThrottled (rateLimiter )) {
503
- AgentSpan activeSpan = AgentTracer .get ().activeSpan ();
504
- if (activeSpan != null ) {
505
- log .debug ("Setting force-keep tag on the current span" );
506
- // Keep event related span, because it could be ignored in case of
507
- // reduced datadog sampling rate.
508
- activeSpan .getLocalRootSpan ().setTag (Tags .ASM_KEEP , true );
509
- // If APM is disabled, inform downstream services that the current
510
- // distributed trace contains at least one ASM event and must inherit
511
- // the given force-keep priority
512
- activeSpan
513
- .getLocalRootSpan ()
514
- .setTag (Tags .PROPAGATED_TRACE_SOURCE , ProductTraceSource .ASM );
526
+ if (!events .isEmpty ()) {
527
+ if (!reqCtx .isThrottled (rateLimiter )) {
528
+ AgentSpan activeSpan = AgentTracer .get ().activeSpan ();
529
+ if (activeSpan != null ) {
530
+ log .debug ("Setting force-keep tag on the current span" );
531
+ // Keep event related span, because it could be ignored in case of
532
+ // reduced datadog sampling rate.
533
+ activeSpan .getLocalRootSpan ().setTag (Tags .ASM_KEEP , true );
534
+ // If APM is disabled, inform downstream services that the current
535
+ // distributed trace contains at least one ASM event and must inherit
536
+ // the given force-keep priority
537
+ activeSpan
538
+ .getLocalRootSpan ()
539
+ .setTag (Tags .PROPAGATED_TRACE_SOURCE , ProductTraceSource .ASM );
540
+ } else {
541
+ // If active span is not available the ASM_KEEP tag will be set in the GatewayBridge
542
+ // when the request ends
543
+ log .debug ("There is no active span available" );
544
+ }
545
+ reqCtx .reportEvents (events );
515
546
} else {
516
- // If active span is not available the ASK_KEEP tag will be set in the GatewayBridge
517
- // when the request ends
518
- log .debug ("There is no active span available" );
547
+ log .debug ("Rate limited WAF events" );
548
+ WafMetricCollector .get ().wafRequestRateLimited ();
519
549
}
520
- reqCtx .reportEvents (events );
521
550
}
522
551
523
552
if (flow .isBlocking ()) {
@@ -551,6 +580,7 @@ private Flow.Action.RequestBlockingAction createBlockRequestAction(ActionInfo ac
551
580
return new Flow .Action .RequestBlockingAction (statusCode , blockingContentType );
552
581
} catch (RuntimeException cce ) {
553
582
log .warn ("Invalid blocking action data" , cce );
583
+ WafMetricCollector .get ().wafRequestBlockFailure ();
554
584
return null ;
555
585
}
556
586
}
@@ -576,6 +606,7 @@ private Flow.Action.RequestBlockingAction createRedirectRequestAction(ActionInfo
576
606
return Flow .Action .RequestBlockingAction .forRedirect (statusCode , location );
577
607
} catch (RuntimeException cce ) {
578
608
log .warn ("Invalid blocking action data" , cce );
609
+ WafMetricCollector .get ().wafRequestBlockFailure ();
579
610
return null ;
580
611
}
581
612
}
0 commit comments