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,82 @@ 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 )
29+ throws ApiException {
30+ submitClientDurationMetric (request , apiInstance , durationInMillis , response .getStatusCode (), tags );
3231 }
3332
3433 protected static void submitClientDurationMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
35- final double durationInMillis , final int statusCode ) throws ApiException {
34+ final double durationInMillis , final int statusCode , final Collection < String > tags ) throws ApiException {
3635 final String name = PREFIX + "." + CLIENT_DURATION ;
3736 final MetricIntakeType type = MetricIntakeType .UNSPECIFIED ;
38- submitMetricWithHttpTags (name , durationInMillis , type , "ms" , request , apiInstance , statusCode );
37+ submitMetricWithHttpTags (name , durationInMillis , type , "ms" , request , apiInstance , statusCode , tags );
3938 }
4039
4140 protected static void submitErrorRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
42- final ApiHttpResponse <byte []> response ) throws ApiException {
43- submitErrorRequestsMetric (request , apiInstance , response .getStatusCode ());
41+ final ApiHttpResponse <byte []> response , final Collection < String > tags ) throws ApiException {
42+ submitErrorRequestsMetric (request , apiInstance , response .getStatusCode (), tags );
4443 }
4544
4645 protected static void submitErrorRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
47- final int statusCode ) throws ApiException {
46+ final int statusCode , final Collection < String > tags ) throws ApiException {
4847 final String name = PREFIX + "." + CLIENT_REQUEST_ERROR ;
4948 final MetricIntakeType count = MetricIntakeType .COUNT ;
50- submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode );
49+ submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode , tags );
5150 }
5251
5352 protected static void submitTotalRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
54- final ApiHttpResponse <byte []> response ) throws ApiException {
55- submitTotalRequestsMetric (request , apiInstance , response .getStatusCode ());
53+ final ApiHttpResponse <byte []> response , final Collection < String > tags ) throws ApiException {
54+ submitTotalRequestsMetric (request , apiInstance , response .getStatusCode (), tags );
5655 }
5756
5857 protected static void submitTotalRequestsMetric (final ApiHttpRequest request , final MetricsApi apiInstance ,
59- final int statusCode ) throws ApiException {
58+ final int statusCode , final Collection < String > tags ) throws ApiException {
6059 final String name = PREFIX + "." + CLIENT_REQUEST_TOTAL ;
6160 final MetricIntakeType count = MetricIntakeType .COUNT ;
62- submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode );
61+ submitMetricWithHttpTags (name , 1.0 , count , "count" , request , apiInstance , statusCode , tags );
6362 }
6463
6564 private static void submitMetricWithHttpTags (final String name , final double value , final MetricIntakeType type ,
6665 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 ());
66+ final ApiHttpResponse <byte []> response , final Collection < String > tags ) throws ApiException {
67+ submitMetricWithHttpTags (name , value , type , unit , request , apiInstance , response .getStatusCode (), tags );
6968 }
7069
7170 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 )
73- 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 ()));
71+ final String unit , final ApiHttpRequest request , final MetricsApi apiInstance , final int statusCode ,
72+ final Collection <String > tags ) throws ApiException {
73+ final List <String > currentTags = new ArrayList <>(tags );
74+ currentTags .add (format ("%s:%s" , HTTP_RESPONSE_STATUS_CODE , statusCode ));
75+ currentTags .add (format ("%s:%s" , HTTP_REQUEST_METHOD , request .getMethod ().name ()));
76+ currentTags .add (format ("%s:%s" , SERVER_ADDRESS , request .getUri ().getHost ()));
7777 if (request .getUri ().getPort () > 0 ) {
78- tags .add (format ("%s:%s" , SERVER_PORT , request .getUri ().getPort ()));
78+ currentTags .add (format ("%s:%s" , SERVER_PORT , request .getUri ().getPort ()));
7979 }
80- submitMetric (apiInstance , name , value , type , unit , tags );
80+ submitMetric (apiInstance , name , value , type , unit , currentTags );
8181 }
8282
8383 protected static void submitJsonSerializationMetric (final MetricsApi apiInstance , final double durationInMillis ,
84- final String responseBodyType ) {
84+ final String responseBodyType , final Collection < String > tags ) {
8585 try {
86+ final List <String > currentTags = new ArrayList <>(tags );
87+ currentTags .add (format ("%s:%s" , RESPONSE_BODY_TYPE , responseBodyType ));
8688 submitMetric (apiInstance , PREFIX + "." + JSON_SERIALIZATION , durationInMillis , MetricIntakeType .UNSPECIFIED ,
87- "ms" , Arrays . asList ( format ( "%s:%s" , RESPONSE_BODY_TYPE , responseBodyType )) );
89+ "ms" , currentTags );
8890 }
8991 catch (ApiException exception ) {
9092 logger .warn ("Failed to submit commercetools json serialization metric" , exception );
9193 }
9294 }
9395
9496 protected static void submitJsonDeserializationMetric (final MetricsApi apiInstance , final double durationInMillis ,
95- final String requestBodyType ) {
97+ final String requestBodyType , final Collection < String > tags ) {
9698 try {
99+ final List <String > currentTags = new ArrayList <>(tags );
100+ currentTags .add (format ("%s:%s" , REQUEST_BODY_TYPE , requestBodyType ));
101+
97102 submitMetric (apiInstance , PREFIX + "." + JSON_DESERIALIZATION , durationInMillis ,
98- MetricIntakeType .UNSPECIFIED , "ms" , Arrays . asList ( format ( "%s:%s" , REQUEST_BODY_TYPE , requestBodyType )) );
103+ MetricIntakeType .UNSPECIFIED , "ms" , currentTags );
99104 }
100105 catch (ApiException exception ) {
101106 logger .warn ("Failed to submit commercetools json deserialization metric" , exception );
0 commit comments