Skip to content

Commit 8dd50e0

Browse files
committed
adding EnvironmentVariables linter
1 parent 06ed2fd commit 8dd50e0

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

components/environment/src/main/java/datadog/environment/EnvironmentVariables.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public static String getOrDefault(String name, String defaultValue) {
3636
return defaultValue;
3737
}
3838
try {
39-
// String value = EnvironmentVariables.get(name);
4039
String value = System.getenv(name);
4140
return value == null ? defaultValue : value;
4241
} catch (SecurityException e) {

dd-java-agent/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ tasks.compileJava.dependsOn compileMain_java6Java
3535

3636
dependencies {
3737
main_java6CompileOnly 'de.thetaphi:forbiddenapis:3.8'
38+
implementation project(':components:environment')
3839
testImplementation sourceSets.main_java6.output
3940
}
4041

dd-java-agent/src/main/java/datadog/trace/bootstrap/BootstrapInitializationTelemetry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package datadog.trace.bootstrap;
22

3+
import datadog.environment.ConfigHelper;
34
import static java.util.Collections.emptyList;
45
import static java.util.Collections.singletonList;
56

67
import datadog.json.JsonWriter;
7-
import datadog.trace.bootstrap.environment.EnvironmentVariables;
88
import de.thetaphi.forbiddenapis.SuppressForbidden;
99
import java.io.Closeable;
1010
import java.io.OutputStream;
@@ -147,7 +147,7 @@ public void onError(String reasonCode) {
147147
}
148148

149149
private int maxTags() {
150-
String maxTags = EnvironmentVariables.get("DD_TELEMETRY_FORWARDER_MAX_TAGS");
150+
String maxTags = ConfigHelper.getEnvironmentVariable("DD_TELEMETRY_FORWARDER_MAX_TAGS");
151151

152152
if (maxTags != null) {
153153
try {

gradle/logEnvVarUsages.gradle

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,48 @@ tasks.register('logEnvVarUsages') {
6565
}
6666
}
6767

68+
tasks.register('checkEnvironmentVariablesUsage') {
69+
group = "verification"
70+
description = "Scans src/main/java for direct usages of EnvironmentVariables.get(...)"
71+
72+
doLast {
73+
def pattern = ~/EnvironmentVariables\.get\s*\(/
74+
def repoRoot = projectDir.toPath()
75+
76+
def javaFiles = fileTree(projectDir) {
77+
include '**/src/main/java/**/*.java'
78+
79+
// Exclude specific files (relative to projectDir)
80+
exclude 'components/environment/src/main/java/datadog/environment/ConfigHelper.java'
81+
}
82+
83+
def matches = []
84+
85+
javaFiles.each { file ->
86+
def relativePath = repoRoot.relativize(file.toPath())
87+
file.eachLine { line, index ->
88+
if (line =~ pattern) {
89+
matches << "${relativePath}:${index + 1} -> ${line.trim()}"
90+
}
91+
}
92+
}
93+
94+
if (!matches.isEmpty()) {
95+
println "\n❌ Found forbidden usages of EnvironmentVariables.get(...):"
96+
matches.each { println it }
97+
throw new GradleException("Forbidden usage of EnvironmentVariables.get(...) found in Java files.")
98+
} else {
99+
println "✅ No forbidden EnvironmentVariables.get(...) usages found in src/main/java."
100+
}
101+
}
102+
}
103+
68104
tasks.named('spotlessCheck') {
69105
dependsOn tasks.named('logEnvVarUsages')
106+
dependsOn tasks.named('checkEnvironmentVariablesUsage')
70107
}
71108

72109
tasks.named('spotlessApply') {
73110
dependsOn tasks.named('logEnvVarUsages')
111+
dependsOn tasks.named('checkEnvironmentVariablesUsage')
74112
}

0 commit comments

Comments
 (0)