Skip to content

Commit a756eb2

Browse files
committed
init: adding System.getenv forbiddenAPI, ConfigHelper, ParseSupportedConfigurations, and DD_ linting logger
1 parent fd62009 commit a756eb2

File tree

20 files changed

+1290
-90
lines changed

20 files changed

+1290
-90
lines changed

build.gradle

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
buildscript {
2+
dependencies {
3+
classpath "pl.allegro.tech.build:axion-release-plugin:1.14.4"
4+
}
5+
6+
configurations.all {
7+
resolutionStrategy.dependencySubstitution {
8+
substitute module("com.jcraft:jsch") using module("com.github.mwiede:jsch:0.2.17") because "jcraft is unmaintained"
9+
substitute module("com.jcraft:jsch.agentproxy") using module("com.github.mwiede:jsch:0.2.17") because "jcraft is unmaintained"
10+
substitute module("com.jcraft:jzlib") using module("com.github.mwiede:jsch:0.2.17") because "jcraft is unmaintained"
11+
}
12+
}
13+
}
14+
15+
plugins {
16+
id 'datadog.gradle-debug'
17+
id 'datadog.dependency-locking'
18+
19+
id 'com.diffplug.spotless' version '6.13.0'
20+
id 'com.github.spotbugs' version '5.0.14'
21+
id 'de.thetaphi.forbiddenapis' version '3.8'
22+
23+
id 'pl.allegro.tech.build.axion-release' version '1.14.4'
24+
id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
25+
26+
id 'com.gradleup.shadow' version '8.3.6' apply false
27+
id 'me.champeau.jmh' version '0.7.0' apply false
28+
id 'org.gradle.playframework' version '0.13' apply false
29+
id 'info.solidsoft.pitest' version '1.9.11' apply false
30+
}
31+
32+
description = 'dd-trace-java'
33+
34+
def isCI = System.getenv("CI") != null
35+
36+
apply from: "$rootDir/gradle/repositories.gradle"
37+
apply from: "$rootDir/gradle/scm.gradle"
38+
39+
spotless {
40+
// only resolve the spotless dependencies once in the build
41+
predeclareDeps()
42+
}
43+
44+
spotlessPredeclare {
45+
// these need to align with the types and versions in gradle/spotless.gradle
46+
java {
47+
removeUnusedImports()
48+
49+
// This is the last Google Java Format version that supports Java 8
50+
googleJavaFormat('1.7')
51+
}
52+
groovyGradle {
53+
greclipse()
54+
}
55+
groovy {
56+
greclipse()
57+
}
58+
kotlinGradle {
59+
ktlint('0.41.0')
60+
}
61+
kotlin {
62+
ktlint('0.41.0')
63+
}
64+
scala {
65+
scalafmt('2.7.5')
66+
}
67+
}
68+
apply from: "$rootDir/gradle/spotless.gradle"
69+
apply from: "$rootDir/gradle/logEnvVarUsages.gradle"
70+
71+
def compileTask = tasks.register("compile")
72+
73+
allprojects {
74+
group = 'com.datadoghq'
75+
version = scmVersion.version
76+
77+
if (isCI) {
78+
buildDir = "$rootDir/workspace/${projectDir.path.replace(rootDir.path, '')}/build/"
79+
}
80+
81+
apply from: "$rootDir/gradle/dependencies.gradle"
82+
apply from: "$rootDir/gradle/util.gradle"
83+
84+
compileTask.configure {
85+
dependsOn tasks.withType(AbstractCompile)
86+
}
87+
}
88+
89+
tasks.register("latestDepTest")
90+
91+
nexusPublishing {
92+
repositories {
93+
def forceLocal = project.hasProperty('forceLocal') && forceLocal
94+
if (forceLocal && !isCI) {
95+
local {
96+
// For testing, use with https://hub.docker.com/r/sonatype/nexus
97+
// docker run --rm -d -p 8081:8081 --name nexus sonatype/nexus:oss
98+
// ./gradlew publishToLocal
99+
// Doesn't work for testing releases though... (due to staging)
100+
nexusUrl = uri("http://localhost:8081/nexus/content/repositories/releases/")
101+
snapshotRepositoryUrl = uri("http://localhost:8081/nexus/content/repositories/snapshots/")
102+
username = "admin"
103+
password = "admin123"
104+
allowInsecureProtocol = true
105+
}
106+
} else {
107+
// see https://github.com/gradle-nexus/publish-plugin#publishing-to-maven-central-via-sonatype-central
108+
// For official documentation:
109+
// staging repo publishing https://central.sonatype.org/publish/publish-portal-ossrh-staging-api/#configuration
110+
// snapshot publishing https://central.sonatype.org/publish/publish-portal-snapshots/#publishing-via-other-methods
111+
sonatype {
112+
nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
113+
snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
114+
username = System.getenv("MAVEN_CENTRAL_USERNAME")
115+
password = System.getenv("MAVEN_CENTRAL_PASSWORD")
116+
}
117+
}
118+
}
119+
}
120+
121+
def writeMainVersionFileTask = tasks.register('writeMainVersionFile') {
122+
def versionFile = file("${rootProject.buildDir}/main.version")
123+
inputs.property "version", scmVersion.version
124+
outputs.file versionFile
125+
126+
doFirst {
127+
assert versionFile.parentFile.mkdirs() || versionFile.parentFile.directory
128+
versionFile.text = "${inputs.properties.version}"
129+
}
130+
}
131+
132+
allprojects {
133+
tasks.withType(JavaForkOptions).configureEach {
134+
maxHeapSize = System.properties["datadog.forkedMaxHeapSize"]
135+
minHeapSize = System.properties["datadog.forkedMinHeapSize"]
136+
jvmArgs "-XX:ErrorFile=/tmp/hs_err_pid%p.log"
137+
jvmArgs "-XX:+HeapDumpOnOutOfMemoryError"
138+
jvmArgs "-XX:HeapDumpPath=/tmp"
139+
}
140+
141+
tasks.withType(PublishToMavenLocal).configureEach {
142+
it.finalizedBy(writeMainVersionFileTask)
143+
}
144+
}
145+
146+
apply from: "$rootDir/gradle/ci_jobs.gradle"

components/environment/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ plugins {
33
id("com.gradleup.shadow")
44
}
55

6+
dependencies {
7+
implementation(project(":components:json"))
8+
implementation("org.slf4j:slf4j-api:1.7.36")
9+
}
10+
611
apply(from = "$rootDir/gradle/java.gradle")
712

813
/*
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package datadog.environment;
2+
3+
import de.thetaphi.forbiddenapis.SuppressForbidden;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
@SuppressForbidden
9+
public class ConfigHelper {
10+
// Parse the supported-configurations JSON once using ParseSupportedConfigurations.
11+
12+
public static Map<String, String> getEnvironmentVariables() {
13+
// TODO: Remove once JSON parsing is separate from data access
14+
ParseSupportedConfigurations.loadSupportedConfigurations("supported-configurations.json");
15+
16+
Map<String, String> env = System.getenv();
17+
Map<String, String> configs = new LinkedHashMap<>();
18+
for (Map.Entry<String, String> entry : env.entrySet()) {
19+
String key = entry.getKey();
20+
String value = entry.getValue();
21+
if (key.startsWith("DD_")
22+
|| key.startsWith("OTEL_")
23+
|| ParseSupportedConfigurations.aliasMapping.containsKey(key)) {
24+
if (ParseSupportedConfigurations.supportedConfigurations.contains(key)) {
25+
configs.put(key, value);
26+
// If this environment variable is the alias of another, and we haven't processed the
27+
// original environment variable yet, handle it here.
28+
} else if (ParseSupportedConfigurations.aliasMapping.containsKey(key)
29+
&& !configs.containsKey(ParseSupportedConfigurations.aliasMapping.get(key))) {
30+
List<String> aliasList =
31+
ParseSupportedConfigurations.aliases.get(
32+
ParseSupportedConfigurations.aliasMapping.get(key));
33+
for (String alias : aliasList) {
34+
if (env.containsKey(alias)) {
35+
configs.put(ParseSupportedConfigurations.aliasMapping.get(key), env.get(alias));
36+
break;
37+
}
38+
}
39+
}
40+
// if (deprecatedConfigurations.containsKey(key)) {
41+
// String warning = "Environment variable " + key + " is deprecated. " +
42+
// (aliasMapping.containsKey(key)
43+
// ? "Please use " + aliasMapping.get(key) + " instead."
44+
// : deprecatedConfigurations.get(key));
45+
// System.err.println(warning); // TODO: REPLACE with log call
46+
// }
47+
} else {
48+
configs.put(key, value);
49+
}
50+
}
51+
return configs;
52+
}
53+
54+
public static String getEnvironmentVariable(String name) {
55+
// TODO: Remove once JSON parsing is separate from data access
56+
ParseSupportedConfigurations.loadSupportedConfigurations("supported-configurations.json");
57+
58+
if ((name.startsWith("DD_")
59+
|| name.startsWith("OTEL_")
60+
|| ParseSupportedConfigurations.aliasMapping.containsKey(name))
61+
&& !ParseSupportedConfigurations.supportedConfigurations.contains(name)) {
62+
throw new IllegalArgumentException(
63+
"Missing " + name + " env/configuration in supported-configurations.json file.");
64+
}
65+
String config = System.getenv(name);
66+
if (config == null && ParseSupportedConfigurations.aliases.containsKey(name)) {
67+
for (String alias : ParseSupportedConfigurations.aliases.get(name)) {
68+
String aliasValue = System.getenv(alias);
69+
if (aliasValue != null) {
70+
return aliasValue;
71+
}
72+
}
73+
}
74+
return config;
75+
}
76+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static String getOrDefault(String name, String defaultValue) {
3636
return defaultValue;
3737
}
3838
try {
39-
String value = System.getenv(name);
39+
String value = ConfigHelper.getEnvironmentVariable(name);
4040
return value == null ? defaultValue : value;
4141
} catch (SecurityException e) {
4242
return defaultValue;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package datadog.environment;
2+
3+
import datadog.json.JsonMapper;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.util.HashMap;
7+
import java.util.HashSet;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Scanner;
11+
import java.util.Set;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
// TODO: Make this class run at buildtime and generate a static class with the data
16+
public class ParseSupportedConfigurations {
17+
public static Map<String, Object> fileData;
18+
public static Map<String, String> aliasMapping;
19+
public static Set<String> supportedConfigurations;
20+
public static Map<String, String> deprecatedConfigurations;
21+
public static Map<String, List<String>> aliases;
22+
private static final Logger log = LoggerFactory.getLogger(ParseSupportedConfigurations.class);
23+
24+
public static void loadSupportedConfigurations(String filename) {
25+
String jsonString;
26+
try {
27+
InputStream in =
28+
ParseSupportedConfigurations.class.getClassLoader().getResourceAsStream(filename);
29+
jsonString = new Scanner(in, "UTF-8").useDelimiter("\\A").next();
30+
fileData = JsonMapper.fromJsonToMap(jsonString);
31+
supportedConfigurations =
32+
new HashSet<>(
33+
((Map<String, List<String>>) fileData.get("supportedConfigurations")).keySet());
34+
aliasToConfig();
35+
deprecatedConfigurations = (Map<String, String>) fileData.get("deprecated");
36+
aliases = (Map<String, List<String>>) fileData.get("aliases");
37+
} catch (IOException e) {
38+
throw new RuntimeException("Failed to read " + filename, e);
39+
}
40+
}
41+
42+
private static void aliasToConfig() {
43+
aliasMapping = new HashMap<>();
44+
Map<String, List<String>> aliases = (Map<String, List<String>>) fileData.get("aliases");
45+
for (String env : aliases.keySet()) {
46+
for (String alias : aliases.get(env)) {
47+
if (aliasMapping.containsKey(alias)) {
48+
log.info(
49+
"{} is listed as an alias of {} when it already exists as an alias of {}",
50+
alias,
51+
env,
52+
aliasMapping.get(alias));
53+
} else {
54+
aliasMapping.put(alias, env);
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)