Skip to content

Commit 8b1484e

Browse files
jack-bergLiudmila Molkovajsuereth
authored
Generate classes by root namespace (#45)
* Generate classes by root namespace Co-authored-by: Liudmila Molkova <[email protected]> * Use image from dockerhub * Update codegen arguments to use new output templating. * Use latest --strict-validation false * Generate all attributes in incubating artifact * Generate all attributes in incubating artifact * Work with otel/semconvgen:feature-codegen-by-namespace, add missing deprecated annotation --------- Co-authored-by: Liudmila Molkova <[email protected]> Co-authored-by: Josh Suereth <[email protected]>
1 parent 0141bda commit 8b1484e

File tree

66 files changed

+5680
-3962
lines changed

Some content is hidden

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

66 files changed

+5680
-3962
lines changed

build.gradle.kts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ nexusPublishing {
4848
}
4949

5050
// start - define tasks to download, unzip, and generate from opentelemetry/semantic-conventions
51-
var generatorVersion = "0.23.0"
51+
// Using image built from feature branch: https://github.com/open-telemetry/build-tools/tree/feature/codegen-by-namespace
52+
// TODO: upgrade to official release when features are incorporated into main
53+
var generatorVersion = "feature-codegen-by-namespace"
5254
val semanticConventionsRepoZip = "https://github.com/open-telemetry/semantic-conventions/archive/v$semanticConventionsVersion.zip"
5355
val schemaUrl = "https://opentelemetry.io/schemas/$semanticConventionsVersion"
5456

@@ -70,19 +72,18 @@ val unzipConfigurationSchema by tasks.registering(Copy::class) {
7072
into("$buildDir/semantic-conventions/")
7173
}
7274

73-
fun generateTask(taskName: String, resource: Boolean, incubating: Boolean) {
75+
fun generateTask(taskName: String, incubating: Boolean) {
7476
tasks.register(taskName, Exec::class) {
7577
dependsOn(unzipConfigurationSchema)
7678

7779
standardOutput = System.out
7880
executable = "docker"
7981

80-
val onlyArg = if (resource) "resource" else "span,event,attribute_group,scope,metric"
81-
val classNamePrefix = if (incubating) "Incubating" else ""
82-
var className = if (resource) "${classNamePrefix}ResourceAttributes" else "${classNamePrefix}SemanticAttributes"
82+
var filter = if (incubating) "any" else "is_stable"
83+
var classPrefix = if (incubating) "Incubating" else ""
8384
val outputDir = if (incubating) "semconv-incubating/src/main/java/io/opentelemetry/semconv/incubating/" else "semconv/src/main/java/io/opentelemetry/semconv/"
84-
val stability = if (incubating) "StabilityLevel.EXPERIMENTAL" else "StabilityLevel.STABLE"
8585
val packageNameArg = if (incubating) "io.opentelemetry.semconv.incubating" else "io.opentelemetry.semconv"
86+
val stablePackageNameArg = if (incubating) "io.opentelemetry.semconv" else ""
8687

8788
setArgs(listOf(
8889
"run",
@@ -91,20 +92,21 @@ fun generateTask(taskName: String, resource: Boolean, incubating: Boolean) {
9192
"-v", "$projectDir/buildscripts/templates:/templates",
9293
"-v", "$projectDir/$outputDir:/output",
9394
"otel/semconvgen:$generatorVersion",
94-
"--only", onlyArg,
95-
"--yaml-root", "/source", "code",
95+
"--yaml-root", "/source",
96+
"--strict-validation", "false",
97+
"code",
9698
"--template", "/templates/SemanticAttributes.java.j2",
97-
"--output", "/output/$className.java",
98-
"-Dclass=$className",
99-
"-Dstability=${stability}",
100-
"-Dpkg=$packageNameArg"))
99+
"--output", "/output/{{pascal_prefix}}${classPrefix}Attributes.java",
100+
"--file-per-group", "root_namespace",
101+
"-Dfilter=${filter}",
102+
"-DclassPrefix=${classPrefix}",
103+
"-Dpkg=$packageNameArg",
104+
"-DstablePkg=$stablePackageNameArg"))
101105
}
102106
}
103107

104-
generateTask("generateStableSemanticAttributes", false, false)
105-
generateTask("generateIncubatingSemanticAttributes", false, true)
106-
generateTask("generateStableResourceAttributes", true, false)
107-
generateTask("generateIncubatingResourceAttributes", true, true)
108+
generateTask("generateStableSemanticAttributes", false)
109+
generateTask("generateIncubatingSemanticAttributes", true)
108110

109111
tasks.register("checkSchemaUrls") {
110112
val schemaUrlsClass = File("$projectDir/semconv/src/main/java/io/opentelemetry/semconv/SchemaUrls.java")
@@ -123,8 +125,6 @@ tasks.register("checkSchemaUrls") {
123125
val generateSemanticConventions by tasks.registering {
124126
dependsOn(tasks.getByName("generateStableSemanticAttributes"))
125127
dependsOn(tasks.getByName("generateIncubatingSemanticAttributes"))
126-
dependsOn(tasks.getByName("generateStableResourceAttributes"))
127-
dependsOn(tasks.getByName("generateIncubatingResourceAttributes"))
128128
dependsOn(tasks.getByName("checkSchemaUrls"))
129129
}
130130

buildscripts/checkstyle-suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
<suppressions>
77
<!-- Suppress Javadoc-related checks in non-public code. -->
88
<suppress checks="JavadocParagraph|JavadocMethod" files="[\\/](jmh|test)[\\/]" />
9-
<suppress checks="SummaryJavadoc" files="SemanticAttributes|ResourceAttributes"/>
9+
<suppress checks="SummaryJavadoc" files=".*Attributes"/>
1010
<suppress checks="RedundantImport" files="[\\/]build[\\/]"/>
1111
</suppressions>
Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
* Copyright The OpenTelemetry Authors
3-
* SPDX-License-Identifier: Apache-2.0
4-
*/
5-
6-
71
{%- macro to_java_return_type(type) -%}
82
{%- if type == "string" -%}
93
String
@@ -31,18 +25,22 @@
3125
{%- elif type == "double" -%}
3226
doubleKey
3327
{%- else -%}
34-
{{lowerFirst(type)}}Key
28+
{{type | to_camelcase(False)}}Key
3529
{%- endif -%}
3630
{%- endmacro %}
37-
{%- macro print_value(type, value) -%}
38-
{{ "\"" if type == "String"}}{{value}}{{ "\"" if type == "String"}}
39-
{%- endmacro %}
40-
{%- macro upFirst(text) -%}
41-
{{ text[0]|upper}}{{text[1:] }}
42-
{%- endmacro %}
43-
{%- macro lowerFirst(text) -%}
44-
{{ text[0]|lower}}{{text[1:] }}
31+
{%- macro stable_class_ref(const_name) -%}
32+
{{stablePkg}}.{{ root_namespace | to_camelcase(True) }}Attributes#{{const_name}}
4533
{%- endmacro %}
34+
{%- if filter != 'any' %}
35+
{%- set filtered_attributes = attributes_and_templates | select(filter) | list %}
36+
{%- else %}
37+
{%- set filtered_attributes = attributes_and_templates | list %}
38+
{%- endif %}
39+
{%- if filtered_attributes | count > 0 %}
40+
/*
41+
* Copyright The OpenTelemetry Authors
42+
* SPDX-License-Identifier: Apache-2.0
43+
*/
4644

4745
package {{pkg | trim}};
4846

@@ -61,9 +59,10 @@ import java.util.List;
6159

6260
// DO NOT EDIT, this is an Auto-generated file from buildscripts{{template}}
6361
@SuppressWarnings("unused")
64-
public final class {{class}} {
62+
public final class {{ root_namespace | to_camelcase(True) }}{{ classPrefix }}Attributes {
63+
{%- for attribute in filtered_attributes %}
6564

66-
{%- for attribute in attributes if attribute.is_local and not attribute.ref and (attribute.stability | string()) == stability %}
65+
{% set attribute_const_name = attribute.fqn | to_const_name -%}
6766
/**
6867
* {{attribute.brief | render_markdown(code="{{@code {0}}}", paragraph="{0}")}}
6968
{%- if attribute.note %}
@@ -74,48 +73,31 @@ public final class {{class}} {
7473
<ul> {{attribute.note | replace("> ", "") | render_markdown(code="{{@code {0}}}", paragraph="<li>{0}</li>", list="{0}")}} </ul>
7574

7675
{%- endif %}
77-
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
76+
{%- if attribute | is_deprecated %}
7877
*
7978
* @deprecated {{attribute.brief | to_doc_brief}}.
79+
{%- elif attribute | is_stable and stablePkg != "" %}
80+
* @deprecated deprecated in favor of stable {@link {{stable_class_ref(attribute_const_name)}}} attribute.
8081
{%- endif %}
8182
*/
82-
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
83+
{%- if attribute | is_deprecated or attribute | is_stable and stablePkg != "" %}
8384
@Deprecated
8485
{%- endif %}
85-
public static final AttributeKey<{{upFirst(to_java_return_type(attribute.attr_type | string))}}> {{attribute.fqn | to_const_name}} = {{to_java_key_type(attribute.attr_type | string)}}("{{attribute.fqn}}");
86-
{%- endfor %}
87-
88-
{%- for attribute_template in attribute_templates if attribute_template.is_local and not attribute_template.ref and (attribute_template.stability | string()) == stability %}
89-
90-
/**
91-
* {{attribute_template.brief | render_markdown(code="{{@code {0}}}", paragraph="{0}")}}
92-
{%- if attribute_template.note %}
93-
*
94-
* <p>Notes:
95-
{# NOTE: replace("> ", "") removes the following problematic characters which produce mangled javadoc: #}
96-
{# https://github.com/open-telemetry/semantic-conventions/blob/c83a10a9c33c18a769835e959200d0e24dc708fe/model/resource/k8s.yaml#L34-L38 #}
97-
<ul> {{attribute_template.note | replace("> ", "") | render_markdown(code="{{@code {0}}}", paragraph="<li>{0}</li>", list="{0}")}} </ul>
98-
99-
{%- endif %}
100-
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
101-
*
102-
* @deprecated {{attribute_template.brief | to_doc_brief}}.
103-
{%- endif %}
104-
*/
105-
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
106-
@Deprecated
86+
{%- if attribute | is_template %}
87+
public static final AttributeKeyTemplate<{{to_java_return_type(attribute.instantiated_type | string) | first_up}}> {{attribute_const_name}} = {{to_java_key_type(attribute.instantiated_type | string)}}Template("{{attribute.fqn}}");
88+
{%- else %}
89+
public static final AttributeKey<{{to_java_return_type(attribute.attr_type | string) | first_up}}> {{attribute_const_name}} = {{to_java_key_type(attribute.attr_type | string)}}("{{attribute.fqn}}");
10790
{%- endif %}
108-
public static final AttributeKeyTemplate<{{upFirst(to_java_return_type(attribute_template.instantiated_type | string))}}> {{attribute_template.fqn | to_const_name}} = {{to_java_key_type(attribute_template.instantiated_type | string)}}Template("{{attribute_template.fqn}}");
10991
{%- endfor %}
11092

11193
// Enum definitions
112-
{%- for attribute in attributes if attribute.is_local and not attribute.ref and attribute.is_enum and (attribute.stability | string()) == stability%}
94+
{%- for attribute in filtered_attributes if attribute.is_enum %}
11395
{%- set class_name = attribute.fqn | to_camelcase(True) ~ "Values" %}
11496
{%- set type = to_java_return_type(attribute.attr_type.enum_type) %}
11597
public static final class {{class_name}} {
11698
{%- for member in attribute.attr_type.members %}
11799
/** {% filter escape %}{{member.brief | to_doc_brief}}.{% endfilter %} */
118-
public static final {{ type }} {{ member.member_id | to_const_name }} = {{ print_value(type, member.value) }};
100+
public static final {{ type }} {{ member.member_id | to_const_name }} = {{ attribute | print_member_value(member) }};
119101

120102
{%- endfor %}
121103

@@ -124,5 +106,6 @@ public final class {{class}} {
124106

125107
{%- endfor %}
126108

127-
private {{class}}() {}
109+
private {{ root_namespace | to_camelcase(True) }}{{ classPrefix }}Attributes() {}
128110
}
111+
{%- endif %}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.semconv.incubating;
7+
8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
10+
import io.opentelemetry.api.common.AttributeKey;
11+
12+
// DO NOT EDIT, this is an Auto-generated file from
13+
// buildscripts/templates/SemanticAttributes.java.j2
14+
@SuppressWarnings("unused")
15+
public final class AndroidIncubatingAttributes {
16+
17+
/**
18+
* Uniquely identifies the framework API revision offered by a version ({@code os.version}) of the
19+
* android operating system. More information can be found <a
20+
* href="https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels">here</a>.
21+
*/
22+
public static final AttributeKey<String> ANDROID_OS_API_LEVEL = stringKey("android.os.api_level");
23+
24+
/**
25+
* This attribute represents the state the application has transitioned into at the occurrence of
26+
* the event.
27+
*
28+
* <p>Notes:
29+
*
30+
* <ul>
31+
* <li>The Android lifecycle states are defined in <a
32+
* href="https://developer.android.com/guide/components/activities/activity-lifecycle#lc">Activity
33+
* lifecycle callbacks</a>, and from which the {@code OS identifiers} are derived.
34+
* </ul>
35+
*/
36+
public static final AttributeKey<String> ANDROID_STATE = stringKey("android.state");
37+
38+
// Enum definitions
39+
public static final class AndroidStateValues {
40+
/**
41+
* Any time before Activity.onResume() or, if the app has no Activity, Context.startService()
42+
* has been called in the app for the first time.
43+
*/
44+
public static final String CREATED = "created";
45+
46+
/**
47+
* Any time after Activity.onPause() or, if the app has no Activity, Context.stopService() has
48+
* been called when the app was in the foreground state.
49+
*/
50+
public static final String BACKGROUND = "background";
51+
52+
/**
53+
* Any time after Activity.onResume() or, if the app has no Activity, Context.startService() has
54+
* been called when the app was in either the created or background states.
55+
*/
56+
public static final String FOREGROUND = "foreground";
57+
58+
private AndroidStateValues() {}
59+
}
60+
61+
private AndroidIncubatingAttributes() {}
62+
}

0 commit comments

Comments
 (0)