Skip to content

Commit 1165ef4

Browse files
jsuerethjack-berg
andauthored
Use weaver for semantic convention codegen (#70)
* 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 <[email protected]> * 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 <[email protected]>
1 parent 0156478 commit 1165ef4

File tree

83 files changed

+1728
-1581
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1728
-1581
lines changed

build.gradle.kts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ nexusPublishing {
5454
}
5555

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

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

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

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

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

buildscripts/checkstyle.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@
144144
<property name="forbiddenSummaryFragments"
145145
value="^@return the *|^This method returns |^A [{]@code [a-zA-Z0-9]+[}]( is a )"/>
146146
</module>
147-
<module name="JavadocParagraph"/>
147+
<module name="JavadocParagraph">
148+
<property name="allowNewlineParagraph" value="false"/>
149+
</module>
148150
<module name="AtclauseOrder">
149151
<property name="tagOrder" value="@param, @return, @throws, @deprecated"/>
150152
<property name="target"

buildscripts/templates/SemanticAttributes.java.j2

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{%- macro to_java_key_type(attribute) -%}
2+
{%- if attribute.type is template_type -%}
3+
{{ attribute.type | instantiated_type | map_text("java_template_key_type") }}
4+
{%- else -%}
5+
{{ attribute.type | instantiated_type | map_text("java_key_type") }}
6+
{%- endif %}
7+
{%- endmacro %}
8+
{%- macro to_java_key_factory(attribute) -%}
9+
{%- if attribute.type is template_type -%}
10+
{{ attribute.type | instantiated_type | map_text("java_template_key_factory") }}
11+
{%- else -%}
12+
{{ attribute.type | instantiated_type | map_text("java_key_factory") }}
13+
{%- endif %}
14+
{%- endmacro %}
15+
{%- set my_class_name = ctx.root_namespace | pascal_case ~ "IncubatingAttributes" -%}
16+
{%- set stable_class_name = ctx.root_namespace | pascal_case ~ "Attributes" -%}
17+
/*
18+
* Copyright The OpenTelemetry Authors
19+
* SPDX-License-Identifier: Apache-2.0
20+
*/
21+
package io.opentelemetry.semconv.incubating;
22+
23+
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
24+
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
25+
import static io.opentelemetry.api.common.AttributeKey.longKey;
26+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
27+
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
28+
29+
import static io.opentelemetry.semconv.AttributeKeyTemplate.stringArrayKeyTemplate;
30+
import static io.opentelemetry.semconv.AttributeKeyTemplate.stringKeyTemplate;
31+
32+
import io.opentelemetry.api.common.AttributeKey;
33+
import io.opentelemetry.semconv.AttributeKeyTemplate;
34+
import java.util.List;
35+
36+
// DO NOT EDIT, this is an Auto-generated file from buildscripts/templates/registry/incubating_java/IncubatingSemanticAttributes.java.j2
37+
@SuppressWarnings("unused")
38+
public final class {{ my_class_name }} {
39+
{% for attribute in ctx.attributes | rejectattr("name", "in", ctx.excluded_attributes) %}{% if attribute is experimental %}
40+
{%- if attribute is deprecated %}{%- set deprecated_javadoc = "@deprecated " ~ attribute.deprecated -%}
41+
{%- else -%}{%- set deprecated_javadoc = "" -%}
42+
{%- endif -%}
43+
{{ [attribute.brief, concat_if("\n\nNotes:\n\n", attribute.note), deprecated_javadoc] | comment(indent=4) }}
44+
{% 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 }}");
45+
{% elif attribute is stable %}
46+
{%- set stable_class_link = "io.opentelemetry.semconv." ~ stable_class_name ~ "#" ~ (attribute.name | screaming_snake_case) -%}
47+
{{ [attribute.brief, concat_if("\n\nNotes:\n\n", attribute.note), "@deprecated deprecated in favor of stable {@link " ~ stable_class_link ~ "} attribute."] | comment(indent=4) }}
48+
@Deprecated public static final {{ to_java_key_type(attribute) | trim }} {{ attribute.name | screaming_snake_case }} = {{ to_java_key_factory(attribute) | trim }}("{{ attribute.name }}");
49+
{% endif %}
50+
{% endfor %}
51+
// Enum definitions
52+
{% for attribute in ctx.attributes | select("enum") | rejectattr("name", "in", ctx.excluded_attributes) %}
53+
{%- if attribute is stable -%}
54+
{%- set stable_class_link = "io.opentelemetry.semconv." ~ stable_class_name ~ "." ~ (attribute.name | pascal_case) ~ "Values" -%}
55+
/**
56+
* Values for {@link #{{ attribute.name | screaming_snake_case }}}.
57+
*
58+
* @deprecated deprecated in favor of stable {@link {{stable_class_link}}}.
59+
*/
60+
{%- elif attribute is deprecated -%}
61+
{{ ["Values for {@link #" ~ attribute.name | screaming_snake_case ~ "}", "@deprecated " ~ attribute.deprecated ] | comment }}
62+
{%- else -%}
63+
/** Values for {@link #{{ attribute.name | screaming_snake_case }}}. */
64+
{%- endif -%}
65+
{% if attribute is stable or attribute is deprecated %}@Deprecated{% endif %}
66+
public static final class {{ attribute.name | pascal_case }}Values {
67+
{%- for member in attribute.type.members %}
68+
{{ [member.brief or (member.id ~ '.')] | comment(indent=4) }}
69+
public static final {{ attribute.type | instantiated_type | map_text("java_enum_type") }} {{ member.id | screaming_snake_case }} = {{ member.value | print_member_value }};
70+
{%- endfor %}
71+
private {{ attribute.name | pascal_case }}Values() {}
72+
}
73+
{% endfor %}
74+
75+
private {{ my_class_name }}() {}
76+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
params:
2+
excluded_namespaces: ["ios", "aspnetcore", "signalr"]
3+
excluded_attributes: ["messaging.client_id"]
4+
comment_formats:
5+
javadoc:
6+
format: html
7+
header: "/**"
8+
prefix: " * "
9+
footer: " */"
10+
old_style_paragraph: true
11+
omit_closing_li: true
12+
inline_code_snippet: "{@code {{code}}}"
13+
block_code_snippet: "<pre>{@code {{code}}}</pre>"
14+
trim: true
15+
remove_trailing_dots: false
16+
default_comment_format: javadoc
17+
templates:
18+
- pattern: IncubatingSemanticAttributes.java.j2
19+
filter: >
20+
semconv_grouped_attributes({
21+
"exclude_root_namespace": $excluded_namespaces,
22+
"exclude_stability": [],
23+
"exclude_deprecated": false
24+
}) | map({
25+
root_namespace: .root_namespace,
26+
attributes: .attributes,
27+
excluded_attributes: $excluded_attributes[]
28+
})
29+
application_mode: each
30+
file_name: "{{ctx.root_namespace | pascal_case}}IncubatingAttributes.java"
31+
text_maps:
32+
java_enum_type:
33+
int: long
34+
double: double
35+
boolean: boolean
36+
string: String
37+
string[]: String[]
38+
int[]: long[]
39+
double[]: double[]
40+
boolean[]: boolean[]
41+
java_key_type:
42+
int: AttributeKey<Long>
43+
double: AttributeKey<Double>
44+
boolean: AttributeKey<Boolean>
45+
string: AttributeKey<String>
46+
string[]: AttributeKey<List<String>>
47+
int[]: AttributeKey<List<Long>>
48+
double[]: AttributeKey<List<Double>>
49+
boolean[]: AttributeKey<List<Boolean>>
50+
java_template_key_type:
51+
int: AttributeKeyTemplate<Long>
52+
double: AttributeKeyTemplate<Double>
53+
boolean: AttributeKeyTemplate<Boolean>
54+
string: AttributeKeyTemplate<String>
55+
string[]: AttributeKeyTemplate<List<String>>
56+
int[]: AttributeKeyTemplate<List<Long>>
57+
double[]: AttributeKeyTemplate<List<Double>>
58+
boolean[]: AttributeKeyTemplate<List<Boolean>>
59+
java_key_factory:
60+
int: longKey
61+
double: doubleKey
62+
boolean: booleanKey
63+
string: stringKey
64+
string[]: stringArrayKey
65+
int[]: longArrayKey
66+
double[]: doubleArrayKey
67+
boolean[]: booleanArrayKey
68+
java_template_key_factory:
69+
int: longKeyTemplate
70+
double: doubleKeyTemplate
71+
boolean: booleanKeyTemplate
72+
string: stringKeyTemplate
73+
string[]: stringArrayKeyTemplate
74+
int[]: longArrayKeyTemplate
75+
double[]: doubleArrayKeyTemplate
76+
boolean[]: booleanArrayKeyTemplate
77+

0 commit comments

Comments
 (0)