Skip to content

Commit

Permalink
Generate classes by root namespace
Browse files Browse the repository at this point in the history
Co-authored-by: Liudmila Molkova <[email protected]>
  • Loading branch information
jack-berg and lmolkova committed Feb 9, 2024
1 parent 0141bda commit 34ad318
Show file tree
Hide file tree
Showing 60 changed files with 4,961 additions and 3,958 deletions.
30 changes: 13 additions & 17 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"
// TODO: build locally from https://github.com/open-telemetry/build-tools/tree/feature/codegen-by-namespace branch
// Blocked on release of this: https://github.com/open-telemetry/build-tools/pull/243#issuecomment-1936446767
var generatorVersion = "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,18 +72,16 @@ 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) "is_experimental" 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"

setArgs(listOf(
Expand All @@ -90,21 +90,19 @@ fun generateTask(taskName: String, resource: Boolean, incubating: Boolean) {
"-v", "$buildDir/semantic-conventions/model:/source",
"-v", "$projectDir/buildscripts/templates:/templates",
"-v", "$projectDir/$outputDir:/output",
"otel/semconvgen:$generatorVersion",
"--only", onlyArg,
"semconvgen:$generatorVersion",
"--yaml-root", "/source", "code",
"--template", "/templates/SemanticAttributes.java.j2",
"--output", "/output/$className.java",
"-Dclass=$className",
"-Dstability=${stability}",
"--output", "/output/${classPrefix}Attributes.java",
"--file-per-group", "root_namespace",
"-Dfilter=${filter}",
"-DclassPrefix=${classPrefix}",
"-Dpkg=$packageNameArg"))
}
}

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 +121,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>
60 changes: 18 additions & 42 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,18 @@
{%- 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:] }}
{%- endmacro %}
{%- set filtered_attributes = attributes_and_templates | select(filter) | list %}
{%- if filtered_attributes | count > 0 %}
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package {{pkg | trim}};

Expand All @@ -61,9 +55,9 @@ 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 %}
/**
* {{attribute.brief | render_markdown(code="{{@code {0}}}", paragraph="{0}")}}
{%- if attribute.note %}
Expand All @@ -74,42 +68,23 @@ 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}}.
{%- endif %}
*/
{%- if (attribute.stability | string()) == "StabilityLevel.DEPRECATED" %}
{%- if attribute | is_deprecated %}
@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.fqn | to_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.fqn | to_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}} {
Expand All @@ -124,5 +99,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

0 comments on commit 34ad318

Please sign in to comment.