Skip to content

Commit 93e13ca

Browse files
committed
adding EnvironmentVariables linter
1 parent c659daf commit 93e13ca

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
@@ -37,7 +37,6 @@ public static String getOrDefault(@Nonnull String name, String defaultValue) {
3737
return defaultValue;
3838
}
3939
try {
40-
// String value = EnvironmentVariables.get(name);
4140
String value = System.getenv(name);
4241
return value == null ? defaultValue : value;
4342
} 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,7 +1,7 @@
11
package datadog.trace.bootstrap;
22

3+
import datadog.environment.ConfigHelper;
34
import datadog.json.JsonWriter;
4-
import datadog.trace.bootstrap.environment.EnvironmentVariables;
55
import de.thetaphi.forbiddenapis.SuppressForbidden;
66
import java.io.Closeable;
77
import java.io.OutputStream;
@@ -146,7 +146,7 @@ public void onError(Throwable t) {
146146
}
147147

148148
private int maxTags() {
149-
String maxTags = EnvironmentVariables.get("DD_TELEMETRY_FORWARDER_MAX_TAGS");
149+
String maxTags = ConfigHelper.getEnvironmentVariable("DD_TELEMETRY_FORWARDER_MAX_TAGS");
150150

151151
if (maxTags != null) {
152152
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)