Skip to content
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

Use weaver for semantic convention codegen #70

Merged
merged 38 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e3c0c9d
Initial build generation to weaver.
jsuereth Aug 5, 2024
d5b2f7e
Use weaver for codegen.
jsuereth Aug 5, 2024
24c17e2
Remove deprecated incubating attributes.
jsuereth Aug 5, 2024
71d4714
Update generated code.
jsuereth Aug 5, 2024
e58b57b
Add workaround for javadoc issue.
jsuereth Aug 7, 2024
0f7cf02
Update codegen.
jsuereth Aug 7, 2024
580363b
Regenerate code.
jsuereth Aug 21, 2024
4bbb3bc
Bump to latest weaver.
jsuereth Aug 23, 2024
24ff612
Regenerate classes/code.
jsuereth Aug 23, 2024
5f2c973
Allow generated code to have different paragaph styles.
jsuereth Aug 23, 2024
b46f248
Add dots back into javadoc.
jsuereth Aug 23, 2024
783a02b
Codegen.
jsuereth Aug 23, 2024
316bbe7
Add back-links from incubating to stable when things go stable.
jsuereth Aug 23, 2024
d091b00
Update generated incubating files.
jsuereth Aug 23, 2024
bce1947
Fix formatting.
jsuereth Aug 23, 2024
5ed3405
Fix formatting.
jsuereth Aug 23, 2024
17f7d37
Fix handling of template keys.
jsuereth Aug 26, 2024
28ee6e9
codegen
jsuereth Aug 26, 2024
2f66d64
Update build.gradle.kts
jsuereth Aug 26, 2024
9a4b07b
Fix missing deprecated + experimental attributes.
jsuereth Aug 29, 2024
e2f0c11
Regenerate semconv.
jsuereth Aug 29, 2024
afa8d8c
Fix enum values.
jsuereth Aug 29, 2024
9ed27c4
regenerate code.
jsuereth Aug 29, 2024
9695ec9
Fix deprecating stable enum values.
jsuereth Aug 29, 2024
8756a13
Regenerate code.
jsuereth Aug 29, 2024
5153d55
Fixes from review.
jsuereth Aug 29, 2024
bd6d24f
Fix bug where deprecated attributes were being added in future for-lo…
jsuereth Sep 4, 2024
09e7ddf
fix up deprecated attributes.
jsuereth Sep 4, 2024
75c8c58
Add descriptions back to deprecated-for-stable incubating attributes.
jsuereth Sep 4, 2024
c15f9ec
Codegen
jsuereth Sep 4, 2024
e3ba4b8
Add deprecation warnings back into enum value classes.
jsuereth Sep 4, 2024
4c03a4e
Fix deprecation on enum value classes.
jsuereth Sep 4, 2024
e3d06f0
Fix enum value links.
jsuereth Sep 4, 2024
f661cc4
regenerate
jsuereth Sep 4, 2024
4b66903
Fix links from deprecated-for-stable enum classes.
jsuereth Sep 4, 2024
5cfefa3
regenerate.
jsuereth Sep 4, 2024
d06cb00
Fix deprecated annotation.
jsuereth Sep 5, 2024
475a041
regneerate.
jsuereth Sep 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
jsuereth marked this conversation as resolved.
Show resolved Hide resolved
"-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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh.. we already have a generateSemanticConventions task which merged both into one. Also, it can be nice to have the ability to isolate to only generate stable or incubating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just means you have to duplicate the weaver config yaml in both places for now

Copy link
Member

@jack-berg jack-berg Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah would be good to DRY that up (in a followup)

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"/>
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
</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 %}@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
Loading