-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Generate class with instrumentation version #18600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e06777
Generate class with instrumetnation version
laurit ca30c0d
fix version separator
laurit 35aaac0
fix
laurit 0af2d5b
disable checkstyle
laurit 3b49453
address review comments
laurit 27e788f
Merge branch 'main' into instrumentation-version-class
laurit a79c71f
add test
laurit 8245088
address review comments
laurit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
conventions/src/main/kotlin/otel.instrumentation-version.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| plugins { | ||
| `java-library` | ||
| } | ||
|
|
||
| dependencies { | ||
| compileOnly("javax.annotation:javax.annotation-api:1.3.2") | ||
| } | ||
|
|
||
| tasks { | ||
| val generateInstrumentationVersionFile by registering { | ||
| val name = computeInstrumentationName() | ||
| val version = project.version as String | ||
| inputs.property("instrumentation.name", name) | ||
| inputs.property("instrumentation.version", version) | ||
|
|
||
| val propertiesDir = layout.buildDirectory.dir("generated/instrumentationVersion/META-INF/io/opentelemetry/instrumentation/") | ||
| outputs.dir(propertiesDir) | ||
|
|
||
| doLast { | ||
| val outputDir = propertiesDir.get().asFile | ||
| outputDir.mkdirs() | ||
| File(outputDir, "$name.properties").writeText("version=$version") | ||
| } | ||
| } | ||
|
|
||
| val generateInstrumentationVersionClass by registering { | ||
| val name = computeInstrumentationName() | ||
| val version = project.version as String | ||
| inputs.property("instrumentation.name", name) | ||
| inputs.property("instrumentation.version", version) | ||
|
|
||
| // The same logic is duplicated in EmbeddedInstrumentationProperties | ||
| // Strip trailing version suffix and remove dashes. | ||
| // If the module name contains a non-trailing version number e.g. jaxrs-3.0-jersey-3.0 replace | ||
| // the dot with underscore. This is needed to turn the module name into valid package name, java | ||
| // package name segments cannot start with a number. | ||
| val moduleName = name.replace("(-[0-9.]*)$".toRegex(), "") | ||
| .replace("-", "") | ||
| .replace("([0-9]+)\\.([0-9]+)".toRegex(), "$1_$2") | ||
| // Extract trailing version number and replace dots with underscores so it could be used as a | ||
| // package name segment. | ||
| val baseVersion = name.replace(".*?([0-9.]*)$".toRegex(), "$1") | ||
| .replace(".", "_") | ||
| val packageName = moduleName + if (baseVersion.isNotEmpty()) ".v$baseVersion" else "" | ||
|
|
||
| val classDir = layout.buildDirectory.dir("generated/instrumentationVersionClass/${packageName.replace('.', '/')}/internal") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that the package name will be something like |
||
| outputs.dir(classDir) | ||
|
|
||
| doLast { | ||
| val outputDir = classDir.get().asFile | ||
| outputDir.mkdirs() | ||
| File(outputDir, "InstrumentationVersion.java").writeText(""" | ||
| package $packageName.internal; | ||
|
|
||
| import javax.annotation.Generated; | ||
|
|
||
| /** Autogenerated class do not edit. */ | ||
| @Generated("otel.instrumentation-version") | ||
| public final class InstrumentationVersion { | ||
| public static final String VERSION = "$version"; | ||
|
|
||
| private InstrumentationVersion() {} | ||
| } | ||
| """.trimIndent()) | ||
| } | ||
| } | ||
|
|
||
| project.tasks.matching { it.name == "compileJava" || it.name == "sourcesJar" | ||
| || it.name == "compileKotlin" || it.name == "kotlinSourcesJar" }.configureEach { | ||
| dependsOn(generateInstrumentationVersionFile, generateInstrumentationVersionClass) | ||
| } | ||
|
|
||
| named<Checkstyle>("checkstyleMain") { | ||
| exclude("**/InstrumentationVersion.java") | ||
| } | ||
| } | ||
|
|
||
| fun computeInstrumentationName(): String { | ||
| val name = when (projectDir.name) { | ||
| "javaagent", "library", "library-autoconfigure" -> projectDir.parentFile.name | ||
| else -> project.name | ||
| } | ||
| return "io.opentelemetry.$name" | ||
| } | ||
|
|
||
| sourceSets { | ||
| main { | ||
| output.dir("build/generated/instrumentationVersion", "builtBy" to "generateInstrumentationVersionFile") | ||
| java { | ||
| srcDir(layout.buildDirectory.dir("generated/instrumentationVersionClass")) | ||
| } | ||
| } | ||
| } | ||
5 changes: 3 additions & 2 deletions
5
conventions/src/main/kotlin/otel.javaagent-instrumentation.gradle.kts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
.../io/opentelemetry/instrumentation/api/internal/EmbeddedInstrumentationPropertiesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.instrumentation.api.internal; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class EmbeddedInstrumentationPropertiesTest { | ||
|
|
||
| @Test | ||
| void loadVersionFromClass() { | ||
| assertThat( | ||
| EmbeddedInstrumentationProperties.loadVersionFromClass("io.opentelemetry.okhttp-3.0")) | ||
| .isNotEmpty(); | ||
| assertThat(EmbeddedInstrumentationProperties.loadVersionFromClass("does-not-exist")).isNull(); | ||
| } | ||
|
|
||
| @Test | ||
| void loadVersionFromProperties() { | ||
| assertThat( | ||
| EmbeddedInstrumentationProperties.loadVersionFromProperties( | ||
| "io.opentelemetry.okhttp-3.0")) | ||
| .isNotEmpty(); | ||
| assertThat(EmbeddedInstrumentationProperties.loadVersionFromProperties("does-not-exist")) | ||
| .isNull(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.