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

Generate classes by root namespace #45

Merged
merged 8 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 18 additions & 18 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ nexusPublishing {
}

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

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

fun generateTask(taskName: String, resource: Boolean, incubating: Boolean) {
fun generateTask(taskName: String, incubating: Boolean) {
tasks.register(taskName, Exec::class) {
dependsOn(unzipConfigurationSchema)

standardOutput = System.out
executable = "docker"

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

setArgs(listOf(
"run",
Expand All @@ -91,20 +92,21 @@ fun generateTask(taskName: String, resource: Boolean, incubating: Boolean) {
"-v", "$projectDir/buildscripts/templates:/templates",
"-v", "$projectDir/$outputDir:/output",
"otel/semconvgen:$generatorVersion",
"--only", onlyArg,
"--yaml-root", "/source", "code",
"--yaml-root", "/source",
"--strict-validation", "false",
"code",
"--template", "/templates/SemanticAttributes.java.j2",
"--output", "/output/$className.java",
"-Dclass=$className",
"-Dstability=${stability}",
"-Dpkg=$packageNameArg"))
"--output", "/output/{{pascal_prefix}}${classPrefix}Attributes.java",
"--file-per-group", "root_namespace",
"-Dfilter=${filter}",
"-DclassPrefix=${classPrefix}",
"-Dpkg=$packageNameArg",
"-DstablePkg=$stablePackageNameArg"))
}
}

generateTask("generateStableSemanticAttributes", false, false)
generateTask("generateIncubatingSemanticAttributes", false, true)
generateTask("generateStableResourceAttributes", true, false)
generateTask("generateIncubatingResourceAttributes", true, true)
generateTask("generateStableSemanticAttributes", false)
generateTask("generateIncubatingSemanticAttributes", true)

tasks.register("checkSchemaUrls") {
val schemaUrlsClass = File("$projectDir/semconv/src/main/java/io/opentelemetry/semconv/SchemaUrls.java")
Expand All @@ -123,8 +125,6 @@ tasks.register("checkSchemaUrls") {
val generateSemanticConventions by tasks.registering {
dependsOn(tasks.getByName("generateStableSemanticAttributes"))
dependsOn(tasks.getByName("generateIncubatingSemanticAttributes"))
dependsOn(tasks.getByName("generateStableResourceAttributes"))
dependsOn(tasks.getByName("generateIncubatingResourceAttributes"))
dependsOn(tasks.getByName("checkSchemaUrls"))
}

Expand Down
2 changes: 1 addition & 1 deletion buildscripts/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<suppressions>
<!-- Suppress Javadoc-related checks in non-public code. -->
<suppress checks="JavadocParagraph|JavadocMethod" files="[\\/](jmh|test)[\\/]" />
<suppress checks="SummaryJavadoc" files="SemanticAttributes|ResourceAttributes"/>
<suppress checks="SummaryJavadoc" files=".*Attributes"/>
<suppress checks="RedundantImport" files="[\\/]build[\\/]"/>
</suppressions>
73 changes: 28 additions & 45 deletions buildscripts/templates/SemanticAttributes.java.j2
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/


{%- macro to_java_return_type(type) -%}
{%- if type == "string" -%}
String
Expand Down Expand Up @@ -31,18 +25,22 @@
{%- elif type == "double" -%}
doubleKey
{%- else -%}
{{lowerFirst(type)}}Key
{{type | to_camelcase(False)}}Key
{%- endif -%}
{%- endmacro %}
{%- macro print_value(type, value) -%}
{{ "\"" if type == "String"}}{{value}}{{ "\"" if type == "String"}}
{%- endmacro %}
{%- macro upFirst(text) -%}
{{ text[0]|upper}}{{text[1:] }}
{%- endmacro %}
{%- macro lowerFirst(text) -%}
{{ text[0]|lower}}{{text[1:] }}
{%- macro stable_class_ref(const_name) -%}
{{stablePkg}}.{{ root_namespace | to_camelcase(True) }}Attributes#{{const_name}}
{%- endmacro %}
{%- if filter != 'any' %}
{%- set filtered_attributes = attributes_and_templates | select(filter) | list %}
{%- else %}
jack-berg marked this conversation as resolved.
Show resolved Hide resolved
{%- set filtered_attributes = attributes_and_templates | list %}
{%- endif %}
{%- if filtered_attributes | count > 0 %}
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package {{pkg | trim}};

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

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

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

{%- endif %}
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
{%- if attribute | is_deprecated %}
*
* @deprecated {{attribute.brief | to_doc_brief}}.
{%- elif attribute | is_stable and stablePkg != "" %}
* @deprecated deprecated in favor of stable {@link {{stable_class_ref(attribute_const_name)}}} attribute.
{%- endif %}
*/
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
{%- if attribute | is_deprecated or attribute | is_stable and stablePkg != "" %}
@Deprecated
{%- endif %}
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}}");
{%- endfor %}

{%- for attribute_template in attribute_templates if attribute_template.is_local and not attribute_template.ref and (attribute_template.stability | string()) == stability %}

/**
* {{attribute_template.brief | render_markdown(code="{{@code {0}}}", paragraph="{0}")}}
{%- if attribute_template.note %}
*
* <p>Notes:
{# NOTE: replace("> ", "") removes the following problematic characters which produce mangled javadoc: #}
{# https://github.com/open-telemetry/semantic-conventions/blob/c83a10a9c33c18a769835e959200d0e24dc708fe/model/resource/k8s.yaml#L34-L38 #}
<ul> {{attribute_template.note | replace("> ", "") | render_markdown(code="{{@code {0}}}", paragraph="<li>{0}</li>", list="{0}")}} </ul>

{%- endif %}
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
*
* @deprecated {{attribute_template.brief | to_doc_brief}}.
{%- endif %}
*/
{%- if (attribute_template.stability | string()) == "StabilityLevel.DEPRECATED" %}
@Deprecated
{%- if attribute | is_template %}
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}}");
{%- else %}
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}}");
{%- endif %}
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}}");
{%- endfor %}

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

{%- endfor %}

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

{%- endfor %}

private {{class}}() {}
private {{ root_namespace | to_camelcase(True) }}{{ classPrefix }}Attributes() {}
}
{%- endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.semconv.incubating;

import static io.opentelemetry.api.common.AttributeKey.stringKey;

import io.opentelemetry.api.common.AttributeKey;

// DO NOT EDIT, this is an Auto-generated file from
// buildscripts/templates/SemanticAttributes.java.j2
@SuppressWarnings("unused")
public final class AndroidIncubatingAttributes {

/**
* Uniquely identifies the framework API revision offered by a version ({@code os.version}) of the
* android operating system. More information can be found <a
* href="https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels">here</a>.
*/
public static final AttributeKey<String> ANDROID_OS_API_LEVEL = stringKey("android.os.api_level");

/**
* This attribute represents the state the application has transitioned into at the occurrence of
* the event.
*
* <p>Notes:
*
* <ul>
* <li>The Android lifecycle states are defined in <a
* href="https://developer.android.com/guide/components/activities/activity-lifecycle#lc">Activity
* lifecycle callbacks</a>, and from which the {@code OS identifiers} are derived.
* </ul>
*/
public static final AttributeKey<String> ANDROID_STATE = stringKey("android.state");

// Enum definitions
public static final class AndroidStateValues {
/**
* Any time before Activity.onResume() or, if the app has no Activity, Context.startService()
* has been called in the app for the first time.
*/
public static final String CREATED = "created";

/**
* Any time after Activity.onPause() or, if the app has no Activity, Context.stopService() has
* been called when the app was in the foreground state.
*/
public static final String BACKGROUND = "background";

/**
* Any time after Activity.onResume() or, if the app has no Activity, Context.startService() has
* been called when the app was in either the created or background states.
*/
public static final String FOREGROUND = "foreground";

private AndroidStateValues() {}
}

private AndroidIncubatingAttributes() {}
}
Loading
Loading