forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
219 lines (181 loc) · 8.57 KB
/
build.gradle.kts
File metadata and controls
219 lines (181 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import de.undercouch.gradle.tasks.download.Download
import java.io.FileFilter
plugins {
id("otel.java-conventions")
id("otel.publish-conventions")
id("otel.animalsniffer-conventions")
id("de.undercouch.download")
id("org.jsonschema2pojo")
}
// SDK modules that are still being developed.
description = "OpenTelemetry SDK Incubator"
otelJava.moduleName.set("io.opentelemetry.sdk.extension.incubator")
dependencies {
api(project(":sdk:all"))
annotationProcessor("com.google.auto.value:auto-value")
// io.opentelemetry.sdk.extension.incubator.metric.viewconfig
implementation(project(":sdk-extensions:autoconfigure-spi"))
implementation("org.snakeyaml:snakeyaml-engine")
// io.opentelemetry.sdk.extension.incubator.fileconfig
api(project(":api:incubator"))
implementation("com.fasterxml.jackson.core:jackson-databind")
api("com.fasterxml.jackson.core:jackson-annotations")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml")
implementation(project(":sdk-extensions:autoconfigure"))
testImplementation(project(":sdk:testing"))
testImplementation(project(":sdk-extensions:autoconfigure"))
testImplementation(project(":exporters:logging"))
testImplementation(project(":exporters:logging-otlp"))
testImplementation(project(":exporters:otlp:all"))
testImplementation(project(":exporters:prometheus"))
testImplementation(project(":sdk-extensions:jaeger-remote-sampler"))
testImplementation(project(":extensions:trace-propagators"))
testImplementation("edu.berkeley.cs.jqf:jqf-fuzz")
testImplementation("io.opentelemetry.contrib:opentelemetry-aws-xray-propagator")
testImplementation("com.linecorp.armeria:armeria-junit5")
testImplementation("com.google.guava:guava-testlib")
}
// The following tasks download the JSON Schema files from open-telemetry/opentelemetry-configuration and generate classes from the type definitions which are used with jackson-databind to parse JSON / YAML to the configuration schema.
// The sequence of tasks is:
// 1. downloadConfigurationSchema - download configuration schema from open-telemetry/opentelemetry-configuration
// 2. unzipConfigurationSchema - unzip the configuration schema archive contents to $buildDir/configuration/
// 3. generateJsonSchema2Pojo - generate java POJOs from the configuration schema
// 4. jsonSchema2PojoPostProcessing - perform various post processing on the generated POJOs, e.g. replace javax.annotation.processing.Generated with javax.annotation.Generated, add @SuppressWarning("rawtypes") annotation
// 5. overwriteJs2p - overwrite original generated classes with versions containing updated @Generated annotation
// 6. deleteJs2pTmp - delete tmp directory
// ... proceed with normal sourcesJar, compileJava, etc
val configurationTag = "1.0.0"
val configurationRef = "refs/tags/v$configurationTag" // Replace with commit SHA to point to experiment with a specific commit
val configurationRepoZip = "https://github.com/open-telemetry/opentelemetry-configuration/archive/$configurationRef.zip"
val buildDirectory = layout.buildDirectory.asFile.get()
val downloadConfigurationSchema by tasks.registering(Download::class) {
src(configurationRepoZip)
dest("$buildDirectory/configuration/opentelemetry-configuration.zip")
overwrite(false)
}
val unzipConfigurationSchema by tasks.registering(Copy::class) {
dependsOn(downloadConfigurationSchema)
from(zipTree(downloadConfigurationSchema.get().dest))
eachFile(closureOf<FileCopyDetails> {
// Remove the top level folder "/opentelemetry-configuration-$configurationRef"
val pathParts = path.split("/")
path = pathParts.subList(1, pathParts.size).joinToString("/")
})
into("$buildDirectory/configuration/")
}
jsonSchema2Pojo {
sourceFiles = setOf(file("$buildDirectory/configuration/opentelemetry_configuration.json"))
targetDirectory = file("$buildDirectory/generated/sources/js2p/java/main")
targetPackage = "io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model"
// Clear old source files to avoid contaminated source dir when updating
removeOldOutput = true
// Include @Nullable annotation. Note: jsonSchmea2Pojo will not add @Nullable annotations on getters
// so we perform some steps in jsonSchema2PojoPostProcessing to add these.
includeJsr305Annotations = true
// Prefer builders to setters
includeSetters = false
generateBuilders = true
// Use title field to generate class name, instead of default which is based on filename / propertynames
useTitleAsClassname = true
// Force java 9+ @Generated annotation, since java 8 @Generated annotation isn't detected by
// jsonSchema2Pojo and annotation is skipped altogether
targetVersion = "1.9"
// Append Model as suffix to the generated classes.
classNameSuffix = "Model"
}
val generateJsonSchema2Pojo = tasks.getByName("generateJsonSchema2Pojo")
generateJsonSchema2Pojo.dependsOn(unzipConfigurationSchema)
val jsonSchema2PojoPostProcessing by tasks.registering(Copy::class) {
dependsOn(generateJsonSchema2Pojo)
from("$buildDirectory/generated/sources/js2p")
into("$buildDirectory/generated/sources/js2p-tmp")
filter {
it
// Remove @Nullable annotation so it can be deterministically added later
.replace("import javax.annotation.Nullable;\n", "")
// Replace java 9+ @Generated annotation with java 8 version, add @Nullable annotation
.replace("import javax.annotation.processing.Generated;", "import javax.annotation.Nullable;\nimport javax.annotation.Generated;")
// Add @SuppressWarnings("rawtypes") annotation to address raw types used in jsonschema2pojo builders
.replace("@Generated(\"jsonschema2pojo\")", "@Generated(\"jsonschema2pojo\")\n@SuppressWarnings(\"rawtypes\")")
// Add @Nullable annotations to all getters
.replace("( *)public ([a-zA-Z]*) get([a-zA-Z]*)".toRegex(), "$1@Nullable\n$1public $2 get$3")
}
}
val overwriteJs2p by tasks.registering(Copy::class) {
dependsOn(jsonSchema2PojoPostProcessing)
from("$buildDirectory/generated/sources/js2p-tmp")
into("$buildDirectory/generated/sources/js2p")
}
val deleteJs2pTmp by tasks.registering(Delete::class) {
dependsOn(overwriteJs2p)
delete("$buildDirectory/generated/sources/js2p-tmp/")
}
val buildGraalVmReflectionJson = tasks.register("buildGraalVmReflectionJson") {
val buildDir = buildDirectory
val targetFile = File(
buildDir,
"resources/main/META-INF/native-image/io.opentelemetry/io.opentelemetry.sdk.extension.incubator/reflect-config.json"
)
val sourcePackage =
"io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model"
val sourcePackagePath = sourcePackage.replace(".", "/")
val classesDir =
File(
buildDir,
"classes/java/main/$sourcePackagePath"
)
inputs.dir(classesDir)
outputs.file(targetFile)
onlyIf { !targetFile.exists() }
dependsOn("compileJava")
doLast {
println("Generating GraalVM reflection config at: ${targetFile.absolutePath}")
val classes = mutableListOf<String>()
classesDir.walkTopDown().filter { it.isFile && it.extension == "class" }.forEach { file ->
val relativePath = file.toRelativeString(classesDir)
val className = relativePath
.removeSuffix(".class")
.replace(File.separatorChar, '.')
classes.add("$sourcePackage.$className")
}
classes.sort()
targetFile.parentFile.mkdirs()
targetFile.bufferedWriter().use { writer ->
writer.write("[\n")
classes.forEachIndexed { index, className ->
writer.write(" {\n")
writer.write(" \"name\": \"$className\",\n")
writer.write(" \"allDeclaredMethods\": true,\n")
writer.write(" \"allDeclaredFields\": true,\n")
writer.write(" \"allDeclaredConstructors\": true\n")
writer.write(" }")
if (index < classes.size - 1) {
writer.write(",\n")
} else {
writer.write("\n")
}
}
writer.write("]\n")
}
}
}
tasks.getByName("compileJava").dependsOn(deleteJs2pTmp)
tasks.getByName("sourcesJar").dependsOn(deleteJs2pTmp, buildGraalVmReflectionJson)
tasks.getByName("jar").dependsOn(deleteJs2pTmp, buildGraalVmReflectionJson)
tasks.getByName("javadoc").dependsOn(buildGraalVmReflectionJson)
tasks.getByName("compileTestJava").dependsOn(buildGraalVmReflectionJson)
// Exclude jsonschema2pojo generated sources from checkstyle
tasks.named<Checkstyle>("checkstyleMain") {
dependsOn(buildGraalVmReflectionJson)
exclude("**/fileconfig/internal/model/**")
}
tasks {
withType<Test>().configureEach {
environment(
mapOf(
// Expose the kitchen sink example file to tests
"CONFIG_REPO_ROOT" to "$buildDirectory/configuration"
)
)
}
}