Create OtelVersion class at build time. (#5365)

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>
This commit is contained in:
jason plumb 2023-04-15 06:31:55 -07:00 committed by GitHub
parent bce7d96b0c
commit 75b83db960
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 16 deletions

View File

@ -0,0 +1,70 @@
package io.opentelemetry.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.the
import java.io.File
/**
* This gradle plugin will define a new task called generateOtelVersionClass.
* This task generates a Java source file that contains the project version
* as a string constant. The "compileJava" task is updated to depend on
* generateOtelVersionClass, and the project source set is updated to
* include the new file.
*/
class OtelVersionClassPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.plugins.apply(JavaPlugin::class.java)
project.task("generateOtelVersionClass") {
doLast {
writeFile(project)
}
}
// Add dependency on this task
project.tasks.getByName("compileJava") {
dependsOn("generateOtelVersionClass")
}
// Add new source dir to the "main" source set
val outDir = buildOutDir(project)
val java = project.the<JavaPluginExtension>()
java.sourceSets.getByName("main").java {
srcDir(outDir)
}
}
private fun writeFile(project: Project) {
val group = "${project.group}".replace('.', '/')
val projectName = project.name.replace('-', '/')
val outDir = buildOutDir(project)
val filename = "$group/$projectName/internal/OtelVersion.java"
val outFile = File(outDir, filename)
val packageName = "${project.group}.${project.name.replace('-', '.')}.internal"
val classBody = getClassBody("${project.version}", packageName)
outFile.parentFile.mkdirs()
outFile.writeText(classBody)
}
private fun getClassBody(version: String, packageName: String): String {
return """
package $packageName;
import javax.annotation.Generated;
/** Autogenerated class do not edit. */
@Generated("io.opentelemetry.gradle.OtelVersionClassPlugin")
public final class OtelVersion {
public static final String VERSION = "$version";
private OtelVersion() {}
}
""".trimIndent()
}
private fun buildOutDir(project: Project): File {
return File(project.buildDir, "generated/sources/version/java/main")
}
}

View File

@ -155,7 +155,7 @@ class ProtoFieldsWireHandler : SchemaHandler() {
for (field in type.fieldsAndOneOfFields) {
builder.addField(
FieldSpec.builder(PROTO_FIELD_INFO, field.name.toUpperCase(), PUBLIC, STATIC, FINAL)
FieldSpec.builder(PROTO_FIELD_INFO, field.name.uppercase(), PUBLIC, STATIC, FINAL)
.initializer("\$T.create(\$L, \$L, \"\$L\")",
PROTO_FIELD_INFO,
field.tag,

View File

@ -1,9 +1,11 @@
import io.opentelemetry.gradle.OtelVersionClassPlugin
plugins {
id("otel.java-conventions")
id("otel.publish-conventions")
id("otel.animalsniffer-conventions")
}
apply<OtelVersionClassPlugin>()
description = "OpenTelemetry SDK Common"
otelJava.moduleName.set("io.opentelemetry.sdk.common")

View File

@ -16,8 +16,8 @@ import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.internal.Utils;
import io.opentelemetry.sdk.common.internal.OtelVersion;
import java.util.Objects;
import java.util.Properties;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@ -54,7 +54,7 @@ public abstract class Resource {
Attributes.builder()
.put(TELEMETRY_SDK_NAME, "opentelemetry")
.put(TELEMETRY_SDK_LANGUAGE, "java")
.put(TELEMETRY_SDK_VERSION, readVersion())
.put(TELEMETRY_SDK_VERSION, OtelVersion.VERSION)
.build());
}
@ -109,18 +109,6 @@ public abstract class Resource {
return new AutoValue_Resource(schemaUrl, attributes);
}
private static String readVersion() {
Properties properties = new Properties();
try {
properties.load(
Resource.class.getResourceAsStream("/io/opentelemetry/sdk/common/version.properties"));
} catch (Exception e) {
// we left the attribute empty
return "unknown";
}
return properties.getProperty("sdk.version", "unknown");
}
/**
* Returns the URL of the OpenTelemetry schema used by this resource. May be null.
*