Skip to content

Commit b15550d

Browse files
committed
Copy over upstream files
1 parent 1f6a6fe commit b15550d

4 files changed

+414
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/** Common setup for manual instrumentation of libraries and javaagent instrumentation. */
2+
3+
plugins {
4+
`java-library`
5+
}
6+
7+
/**
8+
* We define three dependency configurations to use when adding dependencies to libraries being
9+
* instrumented.
10+
*
11+
* - library: A dependency on the instrumented library. Results in the dependency being added to
12+
* compileOnly and testImplementation. If the build is run with -PtestLatestDeps=true, the
13+
* version when added to testImplementation will be overridden by `+`, the latest version
14+
* possible. For simple libraries without different behavior between versions, it is possible
15+
* to have a single dependency on library only.
16+
*
17+
* - testLibrary: A dependency on a library for testing. This will usually be used to either
18+
* a) use a different version of the library for compilation and testing and b) to add a helper
19+
* that is only required for tests (e.g., library-testing artifact). The dependency will be
20+
* added to testImplementation and will have a version of `+` when testing latest deps as
21+
* described above.
22+
*
23+
* - latestDepTestLibrary: A dependency on a library for testing when testing of latest dependency
24+
* version is enabled. This dependency will be added as-is to testImplementation, but only if
25+
* -PtestLatestDeps=true. The version will not be modified but it will be given highest
26+
* precedence. Use this to restrict the latest version dependency from the default `+`, for
27+
* example to restrict to just a major version by specifying `2.+`.
28+
*/
29+
30+
val testLatestDeps = gradle.startParameter.projectProperties["testLatestDeps"] == "true"
31+
extra["testLatestDeps"] = testLatestDeps
32+
33+
@CacheableRule
34+
abstract class TestLatestDepsRule : ComponentMetadataRule {
35+
override fun execute(context: ComponentMetadataContext) {
36+
val version = context.details.id.version
37+
if (version.contains("-alpha", true)
38+
|| version.contains("-beta", true)
39+
|| version.contains("-rc", true)
40+
|| version.contains(".rc", true)
41+
|| version.contains("-m", true) // e.g. spring milestones are published to grails repo
42+
|| version.contains(".m", true) // e.g. lettuce
43+
|| version.contains(".alpha", true) // e.g. netty
44+
|| version.contains(".beta", true) // e.g. hibernate
45+
|| version.contains(".cr", true) // e.g. hibernate
46+
|| version.endsWith("-nf-execution") // graphql
47+
|| GIT_SHA_PATTERN.matches(version) // graphql
48+
|| DATETIME_PATTERN.matches(version) // graphql
49+
) {
50+
context.details.status = "milestone"
51+
}
52+
}
53+
54+
companion object {
55+
private val GIT_SHA_PATTERN = Regex("^.*-[0-9a-f]{7,}$")
56+
private val DATETIME_PATTERN = Regex("^\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}.*$")
57+
}
58+
}
59+
60+
configurations {
61+
val library by creating {
62+
isCanBeResolved = false
63+
isCanBeConsumed = false
64+
}
65+
val testLibrary by creating {
66+
isCanBeResolved = false
67+
isCanBeConsumed = false
68+
}
69+
val latestDepTestLibrary by creating {
70+
isCanBeResolved = false
71+
isCanBeConsumed = false
72+
}
73+
74+
val testImplementation by getting
75+
76+
listOf(library, testLibrary).forEach { configuration ->
77+
// We use whenObjectAdded and copy into the real configurations instead of extension to allow
78+
// mutating the version for latest dep tests.
79+
configuration.dependencies.whenObjectAdded {
80+
val dep = copy()
81+
if (testLatestDeps) {
82+
(dep as ExternalDependency).version {
83+
require("latest.release")
84+
}
85+
}
86+
testImplementation.dependencies.add(dep)
87+
}
88+
}
89+
if (testLatestDeps) {
90+
dependencies {
91+
components {
92+
all<TestLatestDepsRule>()
93+
}
94+
}
95+
96+
latestDepTestLibrary.dependencies.whenObjectAdded {
97+
val dep = copy()
98+
val declaredVersion = dep.version
99+
if (declaredVersion != null) {
100+
(dep as ExternalDependency).version {
101+
strictly(declaredVersion)
102+
}
103+
}
104+
testImplementation.dependencies.add(dep)
105+
}
106+
}
107+
named("compileOnly") {
108+
extendsFrom(library)
109+
}
110+
}
111+
112+
if (testLatestDeps) {
113+
afterEvaluate {
114+
tasks {
115+
withType<JavaCompile>().configureEach {
116+
with(options) {
117+
// We may use methods that are deprecated in future versions, we check lint on the normal
118+
// build and don't need this for testLatestDeps.
119+
compilerArgs.add("-Xlint:-deprecation")
120+
}
121+
}
122+
}
123+
124+
if (tasks.names.contains("latestDepTest")) {
125+
val latestDepTest by tasks.existing
126+
tasks.named("test").configure {
127+
dependsOn(latestDepTest)
128+
}
129+
}
130+
}
131+
} else {
132+
afterEvaluate {
133+
// Disable compiling latest dep tests for non latest dep builds in CI. This is needed to avoid
134+
// breaking build because of a new library version which could force backporting latest dep
135+
// fixes to release branches.
136+
// This is only needed for modules where base version and latest dep tests use a different
137+
// source directory.
138+
var latestDepCompileTaskNames = arrayOf("compileLatestDepTestJava", "compileLatestDepTestGroovy", "compileLatestDepTestScala")
139+
for (compileTaskName in latestDepCompileTaskNames) {
140+
if (tasks.names.contains(compileTaskName)) {
141+
tasks.named(compileTaskName).configure {
142+
enabled = false
143+
}
144+
}
145+
}
146+
}
147+
}
148+
149+
tasks {
150+
val generateInstrumentationVersionFile by registering {
151+
val name = computeInstrumentationName()
152+
val version = project.version as String
153+
inputs.property("instrumentation.name", name)
154+
inputs.property("instrumentation.version", version)
155+
156+
val propertiesDir = layout.buildDirectory.dir("generated/instrumentationVersion/META-INF/io/opentelemetry/instrumentation/")
157+
outputs.dir(propertiesDir)
158+
159+
doLast {
160+
File(propertiesDir.get().asFile, "$name.properties").writeText("version=$version")
161+
}
162+
}
163+
}
164+
165+
fun computeInstrumentationName(): String {
166+
val name = when (projectDir.name) {
167+
"javaagent", "library", "library-autoconfigure" -> projectDir.parentFile.name
168+
else -> project.name
169+
}
170+
return "io.opentelemetry.$name"
171+
}
172+
173+
sourceSets {
174+
main {
175+
output.dir("build/generated/instrumentationVersion", "builtBy" to "generateInstrumentationVersionFile")
176+
}
177+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
id("io.opentelemetry.instrumentation.javaagent-testing")
3+
id("io.opentelemetry.instrumentation.muzzle-check")
4+
id("io.opentelemetry.instrumentation.muzzle-generation")
5+
}
6+
7+
dependencies {
8+
add("muzzleBootstrap", "io.opentelemetry.instrumentation:opentelemetry-instrumentation-api")
9+
add("muzzleBootstrap", "io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-incubator")
10+
add("muzzleBootstrap", "io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations-support")
11+
add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api")
12+
add("muzzleTooling", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling")
13+
14+
/*
15+
Dependencies added to this configuration will be found by the muzzle gradle plugin during code
16+
generation phase. These classes become part of the code that plugin inspects and traverses during
17+
references collection phase.
18+
*/
19+
add("codegen", "io.opentelemetry.javaagent:opentelemetry-javaagent-tooling")
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
3+
plugins {
4+
id("com.gradleup.shadow")
5+
}
6+
7+
// NOTE: any modifications below should also be made in
8+
// io.opentelemetry.instrumentation.muzzle-check.gradle.kts
9+
tasks.withType<ShadowJar>().configureEach {
10+
mergeServiceFiles()
11+
// Merge any AWS SDK service files that may be present (too bad they didn't just use normal
12+
// service loader...)
13+
mergeServiceFiles("software/amazon/awssdk/global/handlers")
14+
15+
exclude("**/module-info.class")
16+
17+
// rewrite dependencies calling Logger.getLogger
18+
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")
19+
20+
if (project.findProperty("disableShadowRelocate") != "true") {
21+
// prevents conflict with library instrumentation, since these classes live in the bootstrap class loader
22+
relocate("io.opentelemetry.instrumentation", "io.opentelemetry.javaagent.shaded.instrumentation") {
23+
// Exclude resource providers since they live in the agent class loader
24+
exclude("io.opentelemetry.instrumentation.resources.*")
25+
exclude("io.opentelemetry.instrumentation.spring.resources.*")
26+
}
27+
28+
// relocate(OpenTelemetry API) since these classes live in the bootstrap class loader
29+
relocate("io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
30+
relocate("io.opentelemetry.semconv", "io.opentelemetry.javaagent.shaded.io.opentelemetry.semconv")
31+
relocate("io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context")
32+
relocate("io.opentelemetry.common", "io.opentelemetry.javaagent.shaded.io.opentelemetry.common")
33+
}
34+
35+
// relocate(the OpenTelemetry extensions that are used by instrumentation modules)
36+
// these extensions live in the AgentClassLoader, and are injected into the user's class loader
37+
// by the instrumentation modules that use them
38+
relocate("io.opentelemetry.contrib.awsxray", "io.opentelemetry.javaagent.shaded.io.opentelemetry.contrib.awsxray")
39+
relocate("io.opentelemetry.extension.kotlin", "io.opentelemetry.javaagent.shaded.io.opentelemetry.extension.kotlin")
40+
41+
// this is for instrumentation of opentelemetry-api and opentelemetry-instrumentation-api
42+
relocate("application.io.opentelemetry", "io.opentelemetry")
43+
relocate("application.io.opentelemetry.instrumentation.api", "io.opentelemetry.instrumentation.api")
44+
45+
// this is for instrumentation on java.util.logging (since java.util.logging itself is shaded above)
46+
relocate("application.java.util.logging", "java.util.logging")
47+
}

0 commit comments

Comments
 (0)