Skip to content

Commit ca20a57

Browse files
authored
[FLINK-36456] feat(datadog): prefer DD_API_KEY env var, update tests and docs
1 parent 1596f1c commit ca20a57

4 files changed

Lines changed: 80 additions & 12 deletions

File tree

docs/content.zh/docs/deployment/metric_reporters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ metrics.reporter.stsd.interval: 60 SECONDS
270270

271271
参数:
272272

273-
- `apikey` - Datadog 的 API KEY。
273+
- `apikey` - Datadog 的 API KEY。(或者设置环境变量 `DD_API_KEY` 来覆盖配置)
274274
- `proxyHost` - (可选的)发送到 Datadog 时使用的代理主机。
275275
- `proxyPort` - (可选的)发送到 Datadog 时使用的代理端口,默认为 8080。
276276
- `dataCenter` - (可选的)要连接的数据中心(`EU`/`US`),默认为 `US`。

docs/content/docs/deployment/metric_reporters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ In contrast to Datadog-provided Histograms the reported aggregations are not com
254254

255255
Parameters:
256256

257-
- `apikey` - the Datadog API key
257+
- `apikey` - the Datadog API key (or set environment variable `DD_API_KEY` to override)
258258
- `proxyHost` - (optional) The proxy host to use when sending to Datadog.
259259
- `proxyPort` - (optional) The proxy port to use when sending to Datadog, defaults to 8080.
260260
- `dataCenter` - (optional) The data center (`EU`/`US`) to connect to, defaults to `US`.

flink-metrics/flink-metrics-datadog/src/main/java/org/apache/flink/metrics/datadog/DatadogHttpReporterFactory.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class DatadogHttpReporterFactory implements MetricReporterFactory {
3232
private static final Logger LOG = LoggerFactory.getLogger(DatadogHttpReporterFactory.class);
3333

3434
private static final String API_KEY = "apikey";
35-
private static final String API_KEY_ENV_VAR = "DATADOG_API_KEY";
35+
private static final String API_KEY_ENV_VAR = "DD_API_KEY";
3636
private static final String PROXY_HOST = "proxyHost";
3737
private static final String PROXY_PORT = "proxyPort";
3838
private static final String DATA_CENTER = "dataCenter";
@@ -68,23 +68,27 @@ public MetricReporter createMetricReporter(Properties config) {
6868
}
6969

7070
/**
71-
* Retrieves the Datadog API key from configuration or environment variable.
72-
*
73-
* <p>The API key is resolved in the following order:
74-
*
75-
* <ol>
76-
* <li>Configuration property "apikey"
77-
* <li>Environment variable "DATADOG_API_KEY"
78-
* </ol>
71+
* Retrieves the Datadog API key from environment variable or configuration.
72+
*
73+
* <p>The API key is resolved in the following order:
74+
*
75+
* <ol>
76+
* <li>Environment variable "DD_API_KEY"
77+
* <li>Configuration property "apikey"
78+
* </ol>
7979
*
8080
* @param config the configuration properties
8181
* @return the Datadog API key, or null if not found in either location
8282
*/
8383
private String getApiKey(Properties config) {
84+
final String envApiKey = System.getenv(API_KEY_ENV_VAR);
85+
if (envApiKey != null && !envApiKey.isEmpty()) {
86+
return envApiKey;
87+
}
8488
String apiKey = config.getProperty(API_KEY, null);
8589
if (apiKey != null && !apiKey.isEmpty()) {
8690
return apiKey;
8791
}
88-
return System.getenv(API_KEY_ENV_VAR);
92+
return null;
8993
}
9094
}

flink-metrics/flink-metrics-datadog/src/test/java/org/apache/flink/metrics/datadog/DatadogHttpReporterFactoryTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ void testNoConfigurationPropertyReturnsNull() throws Exception {
5555
assertThat(resolvedApiKey).isNull();
5656
}
5757

58+
@Test
59+
void testApiKeyFromEnvironmentVariable() throws Exception {
60+
final String envKey = "env-api-key";
61+
setEnv("DD_API_KEY", envKey);
62+
try {
63+
final String resolvedApiKey = getApiKeyViaReflection(createConfig());
64+
assertThat(resolvedApiKey).isEqualTo(envKey);
65+
} finally {
66+
setEnv("DD_API_KEY", null);
67+
}
68+
}
69+
70+
@Test
71+
void testEnvironmentVariableTakesPrecedenceOverConfiguration() throws Exception {
72+
final String envKey = "env-api-key";
73+
final String configKey = "config-api-key";
74+
setEnv("DD_API_KEY", envKey);
75+
try {
76+
final String resolvedApiKey = getApiKeyViaReflection(createConfig("apikey", configKey));
77+
assertThat(resolvedApiKey).isEqualTo(envKey);
78+
} finally {
79+
setEnv("DD_API_KEY", null);
80+
}
81+
}
82+
5883
private Properties createConfig(String key, String value) {
5984
Properties config = new Properties();
6085
config.setProperty(key, value);
@@ -73,4 +98,43 @@ private String getApiKeyViaReflection(Properties config) throws Exception {
7398
getApiKeyMethod.setAccessible(true);
7499
return (String) getApiKeyMethod.invoke(factory, config);
75100
}
101+
102+
@SuppressWarnings("unchecked")
103+
private void setEnv(String key, String value) throws Exception {
104+
try {
105+
Map<String, String> env = System.getenv();
106+
Class<?> cl = env.getClass();
107+
java.lang.reflect.Field field = cl.getDeclaredField("m");
108+
field.setAccessible(true);
109+
Map<String, String> writableEnv = (Map<String, String>) field.get(env);
110+
if (value == null) {
111+
writableEnv.remove(key);
112+
} else {
113+
writableEnv.put(key, value);
114+
}
115+
} catch (NoSuchFieldException e) {
116+
// Fallback for other JVM implementations
117+
try {
118+
Class<?> pe = Class.forName("java.lang.ProcessEnvironment");
119+
java.lang.reflect.Field theEnvironmentField = pe.getDeclaredField("theEnvironment");
120+
theEnvironmentField.setAccessible(true);
121+
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
122+
if (value == null) {
123+
env.remove(key);
124+
} else {
125+
env.put(key, value);
126+
}
127+
java.lang.reflect.Field theCaseInsensitiveEnvironmentField = pe.getDeclaredField("theCaseInsensitiveEnvironment");
128+
theCaseInsensitiveEnvironmentField.setAccessible(true);
129+
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
130+
if (value == null) {
131+
cienv.remove(key);
132+
} else {
133+
cienv.put(key, value);
134+
}
135+
} catch (ClassNotFoundException | NoSuchFieldException ex) {
136+
// ignore; best-effort environment mutation for tests
137+
}
138+
}
139+
}
76140
}

0 commit comments

Comments
 (0)