Skip to content

Commit 84455f8

Browse files
committed
Polish "Add properties for Dynatrace metrics API v2 ingest with Micrometer"
1 parent 3161164 commit 84455f8

File tree

6 files changed

+218
-141
lines changed

6 files changed

+218
-141
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/dynatrace/DynatraceProperties.java

Lines changed: 104 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
import java.util.Map;
2020

21-
import io.micrometer.dynatrace.DynatraceApiVersion;
22-
2321
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
2422
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2524

2625
/**
2726
* {@link ConfigurationProperties @ConfigurationProperties} for configuring Dynatrace
@@ -34,73 +33,21 @@
3433
@ConfigurationProperties(prefix = "management.metrics.export.dynatrace")
3534
public class DynatraceProperties extends StepRegistryProperties {
3635

37-
/**
38-
* The Dynatrace metrics API version that metrics should be sent to. Defaults to v1.
39-
* Required to define which API is used for export.
40-
*/
41-
private DynatraceApiVersion apiVersion = DynatraceApiVersion.V1;
36+
private final V1 v1 = new V1();
37+
38+
private final V2 v2 = new V2();
4239

4340
/**
4441
* Dynatrace authentication token.
45-
*
46-
* API v1: required, API v2: optional
4742
*/
4843
private String apiToken;
4944

50-
/**
51-
* ID of the custom device that is exporting metrics to Dynatrace.
52-
*
53-
* API v1: required, API v2: not applicable (ignored)
54-
*/
55-
private String deviceId;
56-
57-
/**
58-
* Technology type for exported metrics. Used to group metrics under a logical
59-
* technology name in the Dynatrace UI.
60-
*
61-
* API v1: required, API v2: not applicable (ignored)
62-
*/
63-
private String technologyType = "java";
64-
6545
/**
6646
* URI to ship metrics to. Should be used for SaaS, self managed instances or to
6747
* en-route through an internal proxy.
68-
*
69-
* API v1: required, API v2: optional
7048
*/
7149
private String uri;
7250

73-
/**
74-
* Group for exported metrics. Used to specify custom device group name in the
75-
* Dynatrace UI.
76-
*
77-
* API v1: required, API v2: not applicable (ignored)
78-
*/
79-
private String group;
80-
81-
/**
82-
* An optional prefix string that is added to all metrics exported.
83-
*
84-
* API v1: not applicable (ignored), API v2: optional
85-
*/
86-
private String metricKeyPrefix;
87-
88-
/**
89-
* An optional Boolean that allows enabling of the Dynatrace metadata export. On by
90-
* default.
91-
*
92-
* API v1: not applicable (ignored), API v2: optional
93-
*/
94-
private Boolean enrichWithDynatraceMetadata = true;
95-
96-
/**
97-
* Optional default dimensions that are added to all metrics in the form of key-value
98-
* pairs. These are overwritten by Micrometer tags if they use the same key.
99-
*
100-
* API v1: not applicable (ignored), API v2: optional
101-
*/
102-
private Map<String, String> defaultDimensions;
103-
10451
public String getApiToken() {
10552
return this.apiToken;
10653
}
@@ -109,20 +56,26 @@ public void setApiToken(String apiToken) {
10956
this.apiToken = apiToken;
11057
}
11158

59+
@Deprecated
60+
@DeprecatedConfigurationProperty(replacement = "management.metrics.export.dynatrace.v1.device-id")
11261
public String getDeviceId() {
113-
return this.deviceId;
62+
return this.v1.getDeviceId();
11463
}
11564

65+
@Deprecated
11666
public void setDeviceId(String deviceId) {
117-
this.deviceId = deviceId;
67+
this.v1.setDeviceId(deviceId);
11868
}
11969

70+
@Deprecated
71+
@DeprecatedConfigurationProperty(replacement = "management.metrics.export.dynatrace.v1.technology-type")
12072
public String getTechnologyType() {
121-
return this.technologyType;
73+
return this.v1.getTechnologyType();
12274
}
12375

76+
@Deprecated
12477
public void setTechnologyType(String technologyType) {
125-
this.technologyType = technologyType;
78+
this.v1.setTechnologyType(technologyType);
12679
}
12780

12881
public String getUri() {
@@ -133,44 +86,112 @@ public void setUri(String uri) {
13386
this.uri = uri;
13487
}
13588

89+
@Deprecated
90+
@DeprecatedConfigurationProperty(replacement = "management.metrics.export.dynatrace.v1.group")
13691
public String getGroup() {
137-
return this.group;
92+
return this.v1.getGroup();
13893
}
13994

95+
@Deprecated
14096
public void setGroup(String group) {
141-
this.group = group;
97+
this.v1.setGroup(group);
14298
}
14399

144-
public String getMetricKeyPrefix() {
145-
return this.metricKeyPrefix;
100+
public V1 getV1() {
101+
return this.v1;
146102
}
147103

148-
public void setMetricKeyPrefix(String metricKeyPrefix) {
149-
this.metricKeyPrefix = metricKeyPrefix;
104+
public V2 getV2() {
105+
return this.v2;
150106
}
151107

152-
public Boolean getEnrichWithDynatraceMetadata() {
153-
return this.enrichWithDynatraceMetadata;
154-
}
108+
public static class V1 {
155109

156-
public void setEnrichWithDynatraceMetadata(Boolean enrichWithDynatraceMetadata) {
157-
this.enrichWithDynatraceMetadata = enrichWithDynatraceMetadata;
158-
}
110+
/**
111+
* ID of the custom device that is exporting metrics to Dynatrace.
112+
*/
113+
private String deviceId;
159114

160-
public Map<String, String> getDefaultDimensions() {
161-
return this.defaultDimensions;
162-
}
115+
/**
116+
* Group for exported metrics. Used to specify custom device group name in the
117+
* Dynatrace UI.
118+
*/
119+
private String group;
163120

164-
public void setDefaultDimensions(Map<String, String> defaultDimensions) {
165-
this.defaultDimensions = defaultDimensions;
166-
}
121+
/**
122+
* Technology type for exported metrics. Used to group metrics under a logical
123+
* technology name in the Dynatrace UI.
124+
*/
125+
private String technologyType = "java";
126+
127+
public String getDeviceId() {
128+
return this.deviceId;
129+
}
130+
131+
public void setDeviceId(String deviceId) {
132+
this.deviceId = deviceId;
133+
}
134+
135+
public String getGroup() {
136+
return this.group;
137+
}
138+
139+
public void setGroup(String group) {
140+
this.group = group;
141+
}
142+
143+
public String getTechnologyType() {
144+
return this.technologyType;
145+
}
146+
147+
public void setTechnologyType(String technologyType) {
148+
this.technologyType = technologyType;
149+
}
167150

168-
public DynatraceApiVersion getApiVersion() {
169-
return this.apiVersion;
170151
}
171152

172-
public void setApiVersion(DynatraceApiVersion apiVersion) {
173-
this.apiVersion = apiVersion;
153+
public static class V2 {
154+
155+
/**
156+
* Default dimensions that are added to all metrics in the form of key-value
157+
* pairs. These are overwritten by Micrometer tags if they use the same key.
158+
*/
159+
private Map<String, String> defaultDimensions;
160+
161+
/**
162+
* Whether to enable Dynatrace metadata export.
163+
*/
164+
private boolean enrichWithDynatraceMetadata = true;
165+
166+
/**
167+
* Prefix string that is added to all exported metrics.
168+
*/
169+
private String metricKeyPrefix;
170+
171+
public Map<String, String> getDefaultDimensions() {
172+
return this.defaultDimensions;
173+
}
174+
175+
public void setDefaultDimensions(Map<String, String> defaultDimensions) {
176+
this.defaultDimensions = defaultDimensions;
177+
}
178+
179+
public boolean isEnrichWithDynatraceMetadata() {
180+
return this.enrichWithDynatraceMetadata;
181+
}
182+
183+
public void setEnrichWithDynatraceMetadata(Boolean enrichWithDynatraceMetadata) {
184+
this.enrichWithDynatraceMetadata = enrichWithDynatraceMetadata;
185+
}
186+
187+
public String getMetricKeyPrefix() {
188+
return this.metricKeyPrefix;
189+
}
190+
191+
public void setMetricKeyPrefix(String metricKeyPrefix) {
192+
this.metricKeyPrefix = metricKeyPrefix;
193+
}
194+
174195
}
175196

176197
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/dynatrace/DynatracePropertiesConfigAdapter.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.dynatrace;
1818

1919
import java.util.Map;
20+
import java.util.function.Function;
2021

2122
import io.micrometer.dynatrace.DynatraceApiVersion;
2223
import io.micrometer.dynatrace.DynatraceConfig;
2324

25+
import org.springframework.boot.actuate.autoconfigure.metrics.export.dynatrace.DynatraceProperties.V1;
26+
import org.springframework.boot.actuate.autoconfigure.metrics.export.dynatrace.DynatraceProperties.V2;
2427
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter;
2528

2629
/**
@@ -48,12 +51,12 @@ public String apiToken() {
4851

4952
@Override
5053
public String deviceId() {
51-
return get(DynatraceProperties::getDeviceId, DynatraceConfig.super::deviceId);
54+
return get(v1(V1::getDeviceId), DynatraceConfig.super::deviceId);
5255
}
5356

5457
@Override
5558
public String technologyType() {
56-
return get(DynatraceProperties::getTechnologyType, DynatraceConfig.super::technologyType);
59+
return get(v1(V1::getTechnologyType), DynatraceConfig.super::technologyType);
5760
}
5861

5962
@Override
@@ -63,27 +66,36 @@ public String uri() {
6366

6467
@Override
6568
public String group() {
66-
return get(DynatraceProperties::getGroup, DynatraceConfig.super::group);
69+
return get(v1(V1::getGroup), DynatraceConfig.super::group);
6770
}
6871

6972
@Override
7073
public DynatraceApiVersion apiVersion() {
71-
return get(DynatraceProperties::getApiVersion, DynatraceConfig.super::apiVersion);
74+
return get((properties) -> (properties.getV1().getDeviceId() != null) ? DynatraceApiVersion.V1
75+
: DynatraceApiVersion.V2, DynatraceConfig.super::apiVersion);
7276
}
7377

7478
@Override
7579
public String metricKeyPrefix() {
76-
return get(DynatraceProperties::getMetricKeyPrefix, DynatraceConfig.super::metricKeyPrefix);
80+
return get(v2(V2::getMetricKeyPrefix), DynatraceConfig.super::metricKeyPrefix);
7781
}
7882

7983
@Override
8084
public Map<String, String> defaultDimensions() {
81-
return get(DynatraceProperties::getDefaultDimensions, DynatraceConfig.super::defaultDimensions);
85+
return get(v2(V2::getDefaultDimensions), DynatraceConfig.super::defaultDimensions);
8286
}
8387

8488
@Override
8589
public boolean enrichWithDynatraceMetadata() {
86-
return get(DynatraceProperties::getEnrichWithDynatraceMetadata,
87-
DynatraceConfig.super::enrichWithDynatraceMetadata);
90+
return get(v2(V2::isEnrichWithDynatraceMetadata), DynatraceConfig.super::enrichWithDynatraceMetadata);
8891
}
92+
93+
private <V> Function<DynatraceProperties, V> v1(Function<V1, V> getter) {
94+
return (properties) -> getter.apply(properties.getV1());
95+
}
96+
97+
private <V> Function<DynatraceProperties, V> v2(Function<V2, V> getter) {
98+
return (properties) -> getter.apply(properties.getV2());
99+
}
100+
89101
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/dynatrace/DynatraceMetricsExportAutoConfigurationTests.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ void backsOffWithoutAClock() {
4848
}
4949

5050
@Test
51-
void failsWithoutAUri() {
51+
void failsWithADeviceIdWithoutAUri() {
5252
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
53+
.withPropertyValues("management.metrics.export.dynatrace.device-id:dev-1")
5354
.run((context) -> assertThat(context).hasFailed());
5455
}
5556

5657
@Test
5758
void autoConfiguresConfigAndMeterRegistry() {
58-
this.contextRunner.withUserConfiguration(BaseConfiguration.class).with(mandatoryProperties())
59+
this.contextRunner.withUserConfiguration(BaseConfiguration.class).with(v1MandatoryProperties())
5960
.run((context) -> assertThat(context).hasSingleBean(DynatraceMeterRegistry.class)
6061
.hasSingleBean(DynatraceConfig.class));
6162
}
@@ -85,22 +86,33 @@ void allowsCustomConfigToBeUsed() {
8586

8687
@Test
8788
void allowsCustomRegistryToBeUsed() {
88-
this.contextRunner.withUserConfiguration(CustomRegistryConfiguration.class).with(mandatoryProperties())
89+
this.contextRunner.withUserConfiguration(CustomRegistryConfiguration.class).with(v1MandatoryProperties())
8990
.run((context) -> assertThat(context).hasSingleBean(DynatraceMeterRegistry.class)
9091
.hasBean("customRegistry").hasSingleBean(DynatraceConfig.class));
9192
}
9293

9394
@Test
94-
void stopsMeterRegistryWhenContextIsClosed() {
95-
this.contextRunner.withUserConfiguration(BaseConfiguration.class).with(mandatoryProperties()).run((context) -> {
95+
void stopsMeterRegistryForV1ApiWhenContextIsClosed() {
96+
this.contextRunner.withUserConfiguration(BaseConfiguration.class).with(v1MandatoryProperties())
97+
.run((context) -> {
98+
DynatraceMeterRegistry registry = context.getBean(DynatraceMeterRegistry.class);
99+
assertThat(registry.isClosed()).isFalse();
100+
context.close();
101+
assertThat(registry.isClosed()).isTrue();
102+
});
103+
}
104+
105+
@Test
106+
void stopsMeterRegistryForV2ApiWhenContextIsClosed() {
107+
this.contextRunner.withUserConfiguration(BaseConfiguration.class).run((context) -> {
96108
DynatraceMeterRegistry registry = context.getBean(DynatraceMeterRegistry.class);
97109
assertThat(registry.isClosed()).isFalse();
98110
context.close();
99111
assertThat(registry.isClosed()).isTrue();
100112
});
101113
}
102114

103-
private Function<ApplicationContextRunner, ApplicationContextRunner> mandatoryProperties() {
115+
private Function<ApplicationContextRunner, ApplicationContextRunner> v1MandatoryProperties() {
104116
return (runner) -> runner.withPropertyValues(
105117
"management.metrics.export.dynatrace.uri=https://dynatrace.example.com",
106118
"management.metrics.export.dynatrace.api-token=abcde",

0 commit comments

Comments
 (0)