diff --git a/README.md b/README.md index a8f5f2b..06767aa 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,16 @@ # OpenTelemetry Semantic Conventions for Java Java code-generation for the [OpenTelemetry Semantic Conventions](https://github.com/open-telemetry/semantic-conventions). + +## Generating semantic conventions + +Prerequisites: +- Docker +- JDK 17+ + +In a shell, execute the following gradle tasks: + +```shell +./gradlew generateSemanticConventions --console=plain +./gradlew spotlessApply +``` diff --git a/build.gradle.kts b/build.gradle.kts index 8e903e0..043d499 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,7 +1,11 @@ +import de.undercouch.gradle.tasks.download.Download + plugins { id("otel.java-conventions") id("otel.animalsniffer-conventions") + + id("de.undercouch.download") } dependencies { @@ -15,3 +19,75 @@ dependencies { testImplementation(platform("org.assertj:assertj-bom:3.24.2")) testImplementation("org.assertj:assertj-core") } + +val specificationVersion = "1.20.0" +var generatorVersion = "0.18.0" + +val specificationRepoZip = "https://github.com/open-telemetry/opentelemetry-specification/archive/v$specificationVersion.zip" +val schemaUrl = "https://opentelemetry.io/schemas/$specificationVersion" + +val downloadSpecification by tasks.registering(Download::class) { + src(specificationRepoZip) + dest("$buildDir/opentelemetry-specification/opentelemetry-specification.zip") + overwrite(false) +} + +val unzipConfigurationSchema by tasks.registering(Copy::class) { + dependsOn(downloadSpecification) + + from(zipTree(downloadSpecification.get().dest)) + eachFile(closureOf { + // Remove the top level folder "/opentelemetry-specification-$semanticConventionsVersion" + val pathParts = path.split("/") + path = pathParts.subList(1, pathParts.size).joinToString("/") + }) + into("$buildDir/opentelemetry-specification/") +} + +val generateSemanticAttributes by tasks.registering(Exec::class) { + dependsOn(unzipConfigurationSchema) + + standardOutput = System.out + executable = "docker" + setArgs(listOf( + "run", + "--rm", + "-v", "$buildDir/opentelemetry-specification/semantic_conventions:/source", + "-v", "$projectDir/buildscripts/templates:/templates", + "-v", "$projectDir/src/main/java/io/opentelemetry/semconv/trace/attributes/:/output", + "otel/semconvgen:$generatorVersion", + "--only", "span,event,attribute_group,scope", + "-f", "/source", "code", + "--template", "/templates/SemanticAttributes.java.j2", + "--output", "/output/SemanticAttributes.java", + "-Dsemconv=trace", + "-Dclass=SemanticAttributes", + "-DschemaUrl=$schemaUrl", + "-Dpkg=io.opentelemetry.semconv.trace.attributes")) +} + +val generateResourceAttributes by tasks.registering(Exec::class) { + dependsOn(unzipConfigurationSchema) + + standardOutput = System.out + executable = "docker" + setArgs(listOf( + "run", + "--rm", + "-v", "$buildDir/opentelemetry-specification/semantic_conventions:/source", + "-v", "$projectDir/buildscripts/templates:/templates", + "-v", "$projectDir/src/main/java/io/opentelemetry/semconv/resource/attributes/:/output", + "otel/semconvgen:$generatorVersion", + "--only", "resource", + "-f", "/source", "code", + "--template", "/templates/SemanticAttributes.java.j2", + "--output", "/output/ResourceAttributes.java", + "-Dclass=ResourceAttributes", + "-DschemaUrl=$schemaUrl", + "-Dpkg=io.opentelemetry.semconv.resource.attributes")) +} + +val generateSemanticConventions by tasks.registering { + dependsOn(generateSemanticAttributes) + dependsOn(generateResourceAttributes) +} diff --git a/buildscripts/semantic-convention/.gitignore b/buildscripts/semantic-convention/.gitignore deleted file mode 100644 index a93b221..0000000 --- a/buildscripts/semantic-convention/.gitignore +++ /dev/null @@ -1 +0,0 @@ -opentelemetry-specification/ diff --git a/buildscripts/semantic-convention/generate.sh b/buildscripts/semantic-convention/generate.sh deleted file mode 100755 index f9e4cd1..0000000 --- a/buildscripts/semantic-convention/generate.sh +++ /dev/null @@ -1,52 +0,0 @@ -#!/bin/bash - -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -ROOT_DIR="${SCRIPT_DIR}/../../" - -# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible -SEMCONV_VERSION=1.20.0 -SPEC_VERSION=v$SEMCONV_VERSION -SCHEMA_URL=https://opentelemetry.io/schemas/$SEMCONV_VERSION -GENERATOR_VERSION=0.18.0 - -cd ${SCRIPT_DIR} - -rm -rf opentelemetry-specification || true -mkdir opentelemetry-specification -cd opentelemetry-specification - -git init -git remote add origin https://github.com/open-telemetry/opentelemetry-specification.git -git fetch origin "$SPEC_VERSION" -git reset --hard FETCH_HEAD -cd ${SCRIPT_DIR} - -docker run --rm \ - -v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions:/source \ - -v ${SCRIPT_DIR}/templates:/templates \ - -v ${ROOT_DIR}/src/main/java/io/opentelemetry/semconv/trace/attributes/:/output \ - otel/semconvgen:$GENERATOR_VERSION \ - --only span,event,attribute_group,scope \ - -f /source code \ - --template /templates/SemanticAttributes.java.j2 \ - --output /output/SemanticAttributes.java \ - -Dsemconv=trace \ - -Dclass=SemanticAttributes \ - -DschemaUrl=$SCHEMA_URL \ - -Dpkg=io.opentelemetry.semconv.trace.attributes - -docker run --rm \ - -v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions:/source \ - -v ${SCRIPT_DIR}/templates:/templates \ - -v ${ROOT_DIR}/src/main/java/io/opentelemetry/semconv/resource/attributes/:/output \ - otel/semconvgen:$GENERATOR_VERSION \ - --only resource \ - -f /source code \ - --template /templates/SemanticAttributes.java.j2 \ - --output /output/ResourceAttributes.java \ - -Dclass=ResourceAttributes \ - -DschemaUrl=$SCHEMA_URL \ - -Dpkg=io.opentelemetry.semconv.resource.attributes - -cd "$ROOT_DIR" -./gradlew spotlessApply diff --git a/buildscripts/semantic-convention/templates/SemanticAttributes.java.j2 b/buildscripts/templates/SemanticAttributes.java.j2 similarity index 100% rename from buildscripts/semantic-convention/templates/SemanticAttributes.java.j2 rename to buildscripts/templates/SemanticAttributes.java.j2 diff --git a/settings.gradle.kts b/settings.gradle.kts index 3f3bf56..f4c0589 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,6 +1,7 @@ pluginManagement { plugins { id("com.gradle.enterprise") version "3.14.1" + id("de.undercouch.download") version "5.4.0" } }