Skip to content

Commit

Permalink
Use weaver for semantic convention codegen (#70)
Browse files Browse the repository at this point in the history
* Initial build generation to weaver.

* Use weaver for codegen.

* Remove deprecated incubating attributes.

* Update generated code.

* Add workaround for javadoc issue.

* Update codegen.

* Regenerate code.

* Bump to latest weaver.

* Regenerate classes/code.

* Allow generated code to have different paragaph styles.

* Add dots back into javadoc.

* Codegen.

* Add back-links from incubating to stable when things go stable.

* Update generated incubating files.

* Fix formatting.

* Fix formatting.

* Fix handling of template keys.

* codegen

* Update build.gradle.kts

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>

* Fix missing deprecated + experimental attributes.

* Regenerate semconv.

* Fix enum values.

* regenerate code.

* Fix deprecating stable enum values.

* Regenerate code.

* Fixes from review.

* Fix bug where deprecated attributes were being added in future for-loops.

* fix up deprecated attributes.

* Add descriptions back to deprecated-for-stable incubating attributes.

* Codegen

* Add deprecation warnings back into enum value classes.

* Fix deprecation on enum value classes.

* Fix enum value links.

* regenerate

* Fix links from deprecated-for-stable enum classes.

* regenerate.

* Fix deprecated annotation.

* regneerate.

---------

Co-authored-by: jack-berg <34418638+jack-berg@users.noreply.github.com>
  • Loading branch information
jsuereth and jack-berg authored Sep 6, 2024
1 parent 0156478 commit 1165ef4
Show file tree
Hide file tree
Showing 83 changed files with 1,728 additions and 1,581 deletions.
50 changes: 26 additions & 24 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ nexusPublishing {
}

// start - define tasks to download, unzip, and generate from opentelemetry/semantic-conventions
var generatorVersion = "0.24.0"
var generatorVersion = "v0.9.1"
val semanticConventionsRepoZip = "https://github.com/open-telemetry/semantic-conventions/archive/v${semanticConventionsVersion}.zip"
val schemaUrl = "https://opentelemetry.io/schemas/$semanticConventionsVersion"

Expand Down Expand Up @@ -83,35 +83,37 @@ fun generateTask(taskName: String, incubating: Boolean) {
standardOutput = System.out
executable = "docker"

var filter = if (incubating) "any" else "is_stable"
var classPrefix = if (incubating) "Incubating" else ""
var target = if (incubating) "incubating_java" else "java"
val outputDir = if (incubating) "semconv-incubating/src/main/java/io/opentelemetry/semconv/incubating/" else "semconv/src/main/java/io/opentelemetry/semconv/"
val packageNameArg = if (incubating) "io.opentelemetry.semconv.incubating" else "io.opentelemetry.semconv"
val stablePackageNameArg = if (incubating) "io.opentelemetry.semconv" else ""

setArgs(listOf(
"run",
val file_args = if (org.gradle.internal.os.OperatingSystem.current().isWindows())
// Don't need to worry about file system permissions in docker.
listOf()
else {
// Make sure we run as local file user
val unix = com.sun.security.auth.module.UnixSystem()
val uid = unix.getUid() // $(id -u $USERNAME)
val gid = unix.getGid() // $(id -g $USERNAME)
listOf("-u", "$uid:$gid")
}
val weaver_args = listOf(
"--rm",
"-v", "$buildDir/semantic-conventions-${semanticConventionsVersion}/model:/source",
"-v", "$projectDir/buildscripts/templates:/templates",
"-v", "$projectDir/$outputDir:/output",
"otel/semconvgen:$generatorVersion",
"--yaml-root", "/source",
"--continue-on-validation-errors",
"code",
"--template", "/templates/SemanticAttributes.java.j2",
"--output", "/output/{{pascal_prefix}}${classPrefix}Attributes.java",
"--file-per-group", "root_namespace",
// Space delimited list of root namespaces to excluded (i.e. "foo bar")
"-Dexcluded_namespaces=ios aspnetcore signalr",
"-Dexcluded_attributes=messaging.client_id",
"-Dfilter=${filter}",
"-DclassPrefix=${classPrefix}",
"-Dpkg=$packageNameArg",
"-DstablePkg=$stablePackageNameArg"))
"--platform=linux/x86_64",
"--mount", "type=bind,source=$buildDir/semantic-conventions-${semanticConventionsVersion}/model,target=/home/weaver/source,readonly",
"--mount", "type=bind,source=$projectDir/buildscripts/templates,target=/home/weaver/templates,readonly",
"--mount", "type=bind,source=$projectDir/$outputDir,target=/home/weaver/target",
"otel/weaver:$generatorVersion",
"registry", "generate",
"--registry=/home/weaver/source",
"--templates=/home/weaver/templates",
"$target",
"/home/weaver/target/")

setArgs(listOf("run") + file_args + weaver_args)
}
}

// TODO - With weaver we can generate both of this in one go, but let's refactor smaller pieces at a time.
generateTask("generateStableSemanticAttributes", false)
generateTask("generateIncubatingSemanticAttributes", true)

Expand Down
4 changes: 3 additions & 1 deletion buildscripts/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@
<property name="forbiddenSummaryFragments"
value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )"/>
</module>
<module name="JavadocParagraph"/>
<module name="JavadocParagraph">
<property name="allowNewlineParagraph" value="false"/>
</module>
<module name="AtclauseOrder">
<property name="tagOrder" value="@param, @return, @throws, @deprecated"/>
<property name="target"
Expand Down
128 changes: 0 additions & 128 deletions buildscripts/templates/SemanticAttributes.java.j2

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{%- macro to_java_key_type(attribute) -%}
{%- if attribute.type is template_type -%}
{{ attribute.type | instantiated_type | map_text("java_template_key_type") }}
{%- else -%}
{{ attribute.type | instantiated_type | map_text("java_key_type") }}
{%- endif %}
{%- endmacro %}
{%- macro to_java_key_factory(attribute) -%}
{%- if attribute.type is template_type -%}
{{ attribute.type | instantiated_type | map_text("java_template_key_factory") }}
{%- else -%}
{{ attribute.type | instantiated_type | map_text("java_key_factory") }}
{%- endif %}
{%- endmacro %}
{%- set my_class_name = ctx.root_namespace | pascal_case ~ "IncubatingAttributes" -%}
{%- set stable_class_name = ctx.root_namespace | pascal_case ~ "Attributes" -%}
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.semconv.incubating;

import static io.opentelemetry.api.common.AttributeKey.booleanKey;
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;

import static io.opentelemetry.semconv.AttributeKeyTemplate.stringArrayKeyTemplate;
import static io.opentelemetry.semconv.AttributeKeyTemplate.stringKeyTemplate;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.semconv.AttributeKeyTemplate;
import java.util.List;

// DO NOT EDIT, this is an Auto-generated file from buildscripts/templates/registry/incubating_java/IncubatingSemanticAttributes.java.j2
@SuppressWarnings("unused")
public final class {{ my_class_name }} {
{% for attribute in ctx.attributes | rejectattr("name", "in", ctx.excluded_attributes) %}{% if attribute is experimental %}
{%- if attribute is deprecated %}{%- set deprecated_javadoc = "@deprecated " ~ attribute.deprecated -%}
{%- else -%}{%- set deprecated_javadoc = "" -%}
{%- endif -%}
{{ [attribute.brief, concat_if("\n\nNotes:\n\n", attribute.note), deprecated_javadoc] | comment(indent=4) }}
{% if attribute is deprecated %}@Deprecated{% endif %} public static final {{ to_java_key_type(attribute) | trim }} {{ attribute.name | screaming_snake_case }} = {{ to_java_key_factory(attribute) | trim }}("{{ attribute.name }}");
{% elif attribute is stable %}
{%- set stable_class_link = "io.opentelemetry.semconv." ~ stable_class_name ~ "#" ~ (attribute.name | screaming_snake_case) -%}
{{ [attribute.brief, concat_if("\n\nNotes:\n\n", attribute.note), "@deprecated deprecated in favor of stable {@link " ~ stable_class_link ~ "} attribute."] | comment(indent=4) }}
@Deprecated public static final {{ to_java_key_type(attribute) | trim }} {{ attribute.name | screaming_snake_case }} = {{ to_java_key_factory(attribute) | trim }}("{{ attribute.name }}");
{% endif %}
{% endfor %}
// Enum definitions
{% for attribute in ctx.attributes | select("enum") | rejectattr("name", "in", ctx.excluded_attributes) %}
{%- if attribute is stable -%}
{%- set stable_class_link = "io.opentelemetry.semconv." ~ stable_class_name ~ "." ~ (attribute.name | pascal_case) ~ "Values" -%}
/**
* Values for {@link #{{ attribute.name | screaming_snake_case }}}.
*
* @deprecated deprecated in favor of stable {@link {{stable_class_link}}}.
*/
{%- elif attribute is deprecated -%}
{{ ["Values for {@link #" ~ attribute.name | screaming_snake_case ~ "}", "@deprecated " ~ attribute.deprecated ] | comment }}
{%- else -%}
/** Values for {@link #{{ attribute.name | screaming_snake_case }}}. */
{%- endif -%}
{% if attribute is stable or attribute is deprecated %}@Deprecated{% endif %}
public static final class {{ attribute.name | pascal_case }}Values {
{%- for member in attribute.type.members %}
{{ [member.brief or (member.id ~ '.')] | comment(indent=4) }}
public static final {{ attribute.type | instantiated_type | map_text("java_enum_type") }} {{ member.id | screaming_snake_case }} = {{ member.value | print_member_value }};
{%- endfor %}
private {{ attribute.name | pascal_case }}Values() {}
}
{% endfor %}

private {{ my_class_name }}() {}
}
77 changes: 77 additions & 0 deletions buildscripts/templates/registry/incubating_java/weaver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
params:
excluded_namespaces: ["ios", "aspnetcore", "signalr"]
excluded_attributes: ["messaging.client_id"]
comment_formats:
javadoc:
format: html
header: "/**"
prefix: " * "
footer: " */"
old_style_paragraph: true
omit_closing_li: true
inline_code_snippet: "{@code {{code}}}"
block_code_snippet: "<pre>{@code {{code}}}</pre>"
trim: true
remove_trailing_dots: false
default_comment_format: javadoc
templates:
- pattern: IncubatingSemanticAttributes.java.j2
filter: >
semconv_grouped_attributes({
"exclude_root_namespace": $excluded_namespaces,
"exclude_stability": [],
"exclude_deprecated": false
}) | map({
root_namespace: .root_namespace,
attributes: .attributes,
excluded_attributes: $excluded_attributes[]
})
application_mode: each
file_name: "{{ctx.root_namespace | pascal_case}}IncubatingAttributes.java"
text_maps:
java_enum_type:
int: long
double: double
boolean: boolean
string: String
string[]: String[]
int[]: long[]
double[]: double[]
boolean[]: boolean[]
java_key_type:
int: AttributeKey<Long>
double: AttributeKey<Double>
boolean: AttributeKey<Boolean>
string: AttributeKey<String>
string[]: AttributeKey<List<String>>
int[]: AttributeKey<List<Long>>
double[]: AttributeKey<List<Double>>
boolean[]: AttributeKey<List<Boolean>>
java_template_key_type:
int: AttributeKeyTemplate<Long>
double: AttributeKeyTemplate<Double>
boolean: AttributeKeyTemplate<Boolean>
string: AttributeKeyTemplate<String>
string[]: AttributeKeyTemplate<List<String>>
int[]: AttributeKeyTemplate<List<Long>>
double[]: AttributeKeyTemplate<List<Double>>
boolean[]: AttributeKeyTemplate<List<Boolean>>
java_key_factory:
int: longKey
double: doubleKey
boolean: booleanKey
string: stringKey
string[]: stringArrayKey
int[]: longArrayKey
double[]: doubleArrayKey
boolean[]: booleanArrayKey
java_template_key_factory:
int: longKeyTemplate
double: doubleKeyTemplate
boolean: booleanKeyTemplate
string: stringKeyTemplate
string[]: stringArrayKeyTemplate
int[]: longArrayKeyTemplate
double[]: doubleArrayKeyTemplate
boolean[]: booleanArrayKeyTemplate

Loading

0 comments on commit 1165ef4

Please sign in to comment.