10
10
import java .util .stream .Stream ;
11
11
12
12
import cyclops .control .Maybe ;
13
+ import lombok .extern .slf4j .Slf4j ;
13
14
import org .coursera .metrics .datadog .DatadogReporter ;
14
15
import org .coursera .metrics .datadog .transport .HttpTransport ;
15
16
import org .springframework .beans .factory .annotation .Autowired ;
24
25
25
26
@ Configuration
26
27
@ EnableMetrics
28
+ @ Slf4j
27
29
public class DatadogMetricsConfigurer extends MetricsConfigurerAdapter {
28
30
29
31
private String apiKey ;
@@ -36,19 +38,20 @@ public class DatadogMetricsConfigurer extends MetricsConfigurerAdapter {
36
38
private final String host ;
37
39
38
40
@ Autowired
39
- public DatadogMetricsConfigurer (@ Value ("${datadog.apikey}" ) String apiKey ,
40
- @ Value ("${datadog.tags:{\" stage:dev\" }}" ) String tags , @ Value ("${datadog.report.period:1}" ) int period ,
41
- @ Value ("${datadog.report.timeunit:SECONDS}" ) TimeUnit timeUnit ,
42
- @ Value ("${datadog.report.expansions:#{null}}" ) String expStr ,
43
- @ Value ("${host.address:#{null}}" ) String host ){
41
+ public DatadogMetricsConfigurer (@ Value ("${datadog.apikey:#{null}}" ) String apiKey ,
42
+ @ Value ("${datadog.tags:{\" stage:dev\" }}" ) String tags ,
43
+ @ Value ("${datadog.report.period:1}" ) int period ,
44
+ @ Value ("${datadog.report.timeunit:SECONDS}" ) TimeUnit timeUnit ,
45
+ @ Value ("${datadog.report.expansions:#{null}}" ) String expStr ,
46
+ @ Value ("${host.address:#{null}}" ) String host ){
44
47
this .apiKey = apiKey ;
45
48
this .tags = Arrays .asList (Optional .ofNullable (tags )
46
49
.orElse ("" )
47
50
.split ("," ));
48
51
this .period = period ;
49
52
this .timeUnit = timeUnit ;
50
53
this .expansions = expansions (expStr );
51
- this .host = host ;
54
+ this .host = host ;
52
55
}
53
56
54
57
private EnumSet <DatadogReporter .Expansion > expansions (String expStr ) {
@@ -65,10 +68,14 @@ private EnumSet<DatadogReporter.Expansion> expansions(String expStr) {
65
68
66
69
@ Override
67
70
public void configureReporters (MetricRegistry metricRegistry ) {
71
+ if (Objects .isNull (apiKey )) {
72
+ log .error ("The 'datadog.apikey' is null. Datadog reporting will be ignored." );
73
+ return ;
74
+ }
68
75
HttpTransport httpTransport = new HttpTransport .Builder ().withApiKey (apiKey )
69
76
.build ();
70
77
EnumSet <DatadogReporter .Expansion > expansions = DatadogReporter .Expansion .ALL ;
71
- DatadogReporter .Builder builder = DatadogReporter .forRegistry (metricRegistry )
78
+ DatadogReporter .Builder builder = DatadogReporter .forRegistry (metricRegistry )
72
79
.withTransport (httpTransport )
73
80
.withExpansions (expansions )
74
81
.withTags (tags );
0 commit comments