55import static java .lang .String .format ;
66
77import java .time .OffsetDateTime ;
8- import java .util .Arrays ;
9- import java .util .Collections ;
10- import java .util .List ;
8+ import java .util .*;
119
1210import com .datadog .api .client .ApiException ;
1311import com .datadog .api .client .v2 .api .MetricsApi ;
@@ -27,75 +25,81 @@ public class DatadogUtils {
2725 private static final Logger logger = LoggerFactory .getLogger (DatadogMiddleware .class );
2826
2927 protected static void submitClientDurationMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
30- final double durationInMillis , final ApiHttpResponse <byte []> response ) throws ApiException {
31- submitClientDurationMetric (request , apiInstance , durationInMillis , response .getStatusCode ());
28+ final double durationInMillis , final ApiHttpResponse <byte []> response , final Collection < String > tags ) throws ApiException {
29+ submitClientDurationMetric (request , apiInstance , durationInMillis , response .getStatusCode (), tags );
3230 }
3331
3432 protected static void submitClientDurationMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
35- final double durationInMillis , final int statusCode ) throws ApiException {
33+ final double durationInMillis , final int statusCode , final Collection < String > tags ) throws ApiException {
3634 final String name = PREFIX + "." + CLIENT_DURATION ;
3735 final MetricIntakeType type = MetricIntakeType .UNSPECIFIED ;
38- submitMetricWithHttpTags (name , durationInMillis , type , "ms" , request , apiInstance , statusCode );
36+ submitMetricWithHttpTags (name , durationInMillis , type , "ms" , request , apiInstance , statusCode , tags );
3937 }
4038
4139 protected static void submitErrorRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
42- final ApiHttpResponse <byte []> response ) throws ApiException {
43- submitErrorRequestsMetric (request , apiInstance , response .getStatusCode ());
40+ final ApiHttpResponse <byte []> response , final Collection < String > tags ) throws ApiException {
41+ submitErrorRequestsMetric (request , apiInstance , response .getStatusCode (), tags );
4442 }
4543
4644 protected static void submitErrorRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
47- final int statusCode ) throws ApiException {
45+ final int statusCode , final Collection < String > tags ) throws ApiException {
4846 final String name = PREFIX + "." + CLIENT_REQUEST_ERROR ;
4947 final MetricIntakeType count = MetricIntakeType .COUNT ;
50- submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode );
48+ submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode , tags );
5149 }
5250
5351 protected static void submitTotalRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
54- final ApiHttpResponse <byte []> response ) throws ApiException {
55- submitTotalRequestsMetric (request , apiInstance , response .getStatusCode ());
52+ final ApiHttpResponse <byte []> response , final Collection < String > tags ) throws ApiException {
53+ submitTotalRequestsMetric (request , apiInstance , response .getStatusCode (), tags );
5654 }
5755
5856 protected static void submitTotalRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
59- final int statusCode ) throws ApiException {
57+ final int statusCode , final Collection < String > tags ) throws ApiException {
6058 final String name = PREFIX + "." + CLIENT_REQUEST_TOTAL ;
6159 final MetricIntakeType count = MetricIntakeType .COUNT ;
62- submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode );
60+ submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode , tags );
6361 }
6462
6563 private static void submitMetricWithHttpTags (final String name , final double value , final MetricIntakeType type ,
6664 final String unit , final ApiHttpRequest request , final MetricsApi apiInstance ,
67- final ApiHttpResponse <byte []> response ) throws ApiException {
68- submitMetricWithHttpTags (name , value , type , unit , request , apiInstance , response .getStatusCode ());
65+ final ApiHttpResponse <byte []> response , final Collection < String > tags ) throws ApiException {
66+ submitMetricWithHttpTags (name , value , type , unit , request , apiInstance , response .getStatusCode (), tags );
6967 }
7068
7169 private static void submitMetricWithHttpTags (final String name , final double value , final MetricIntakeType type ,
72- final String unit , final ApiHttpRequest request , final MetricsApi apiInstance , final int statusCode )
70+ final String unit , final ApiHttpRequest request , final MetricsApi apiInstance , final int statusCode , final Collection < String > tags )
7371 throws ApiException {
74- final List <String > tags = Arrays .asList (format ("%s:%s" , HTTP_RESPONSE_STATUS_CODE , statusCode ),
75- format ("%s:%s" , HTTP_REQUEST_METHOD , request .getMethod ().name ()),
76- format ("%s:%s" , SERVER_ADDRESS , request .getUri ().getHost ()));
72+ final List <String > currentTags = new ArrayList <>(tags );
73+ currentTags .add (format ("%s:%s" , HTTP_RESPONSE_STATUS_CODE , statusCode ));
74+ currentTags .add (format ("%s:%s" , HTTP_REQUEST_METHOD , request .getMethod ().name ()));
75+ currentTags .add (format ("%s:%s" , SERVER_ADDRESS , request .getUri ().getHost ()));
7776 if (request .getUri ().getPort () > 0 ) {
78- tags .add (format ("%s:%s" , SERVER_PORT , request .getUri ().getPort ()));
77+ currentTags .add (format ("%s:%s" , SERVER_PORT , request .getUri ().getPort ()));
7978 }
80- submitMetric (apiInstance , name , value , type , unit , tags );
79+ submitMetric (apiInstance , name , value , type , unit , currentTags );
8180 }
8281
8382 protected static void submitJsonSerializationMetric (final MetricsApi apiInstance , final double durationInMillis ,
84- final String responseBodyType ) {
83+ final String responseBodyType , final Collection < String > tags ) {
8584 try {
85+ final List <String > currentTags = new ArrayList <>(tags );
86+ currentTags .add (format ("%s:%s" , RESPONSE_BODY_TYPE , responseBodyType ));
8687 submitMetric (apiInstance , PREFIX + "." + JSON_SERIALIZATION , durationInMillis , MetricIntakeType .UNSPECIFIED ,
87- "ms" , Arrays . asList ( format ( "%s:%s" , RESPONSE_BODY_TYPE , responseBodyType )) );
88+ "ms" , currentTags );
8889 }
8990 catch (ApiException exception ) {
9091 logger .warn ("Failed to submit commercetools json serialization metric" , exception );
9192 }
9293 }
9394
9495 protected static void submitJsonDeserializationMetric (final MetricsApi apiInstance , final double durationInMillis ,
95- final String requestBodyType ) {
96+ final String requestBodyType , final Collection < String > tags ) {
9697 try {
98+ final List <String > currentTags = new ArrayList <>(tags );
99+ currentTags .add (format ("%s:%s" , REQUEST_BODY_TYPE , requestBodyType ));
100+
97101 submitMetric (apiInstance , PREFIX + "." + JSON_DESERIALIZATION , durationInMillis ,
98- MetricIntakeType .UNSPECIFIED , "ms" , Arrays . asList ( format ( "%s:%s" , REQUEST_BODY_TYPE , requestBodyType )) );
102+ MetricIntakeType .UNSPECIFIED , "ms" , currentTags );
99103 }
100104 catch (ApiException exception ) {
101105 logger .warn ("Failed to submit commercetools json deserialization metric" , exception );
0 commit comments