Skip to content

Commit

Permalink
Auto-generated Semantic Conventions (#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopgrassi authored Oct 19, 2022
1 parent 6d6222f commit 2377d00
Show file tree
Hide file tree
Showing 14 changed files with 4,229 additions and 0 deletions.
7 changes: 7 additions & 0 deletions OpenTelemetry.proj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

<PackProjects Include="src\**\*.csproj" />

<!-- Windows specific projects -->
<PackProjects Remove="src\OpenTelemetry.Instrumentation.AspNet\OpenTelemetry.Instrumentation.AspNet.csproj" Condition="'$(OS)' != 'Windows_NT'" />

<!-- Not pack SemanticConventions project for now -->
<SolutionProjects Remove="src\OpenTelemetry.SemanticConventions\OpenTelemetry.SemanticConventions.csproj" />
<PackProjects Remove="src\OpenTelemetry.SemanticConventions\OpenTelemetry.SemanticConventions.csproj" Condition="'$(OS)' != 'Windows_NT'" />

</ItemGroup>

<Target Name="Build">
Expand Down
6 changes: 6 additions & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "source-generation", "docs\l
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "getting-started-prometheus-grafana", "docs\metrics\getting-started-prometheus-grafana\getting-started-prometheus-grafana.csproj", "{41B784AA-3301-4126-AF9F-1D59BD04B0BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetry.SemanticConventions", "src\OpenTelemetry.SemanticConventions\OpenTelemetry.SemanticConventions.csproj", "{D4519DF6-CC72-4AC4-A851-E21383098D11}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "customizing-the-sdk", "docs\logs\customizing-the-sdk\customizing-the-sdk.csproj", "{6C7A1595-36D6-4229-BBB5-5A6B5791791D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.Extensions.Propagators", "src\OpenTelemetry.Extensions.Propagators\OpenTelemetry.Extensions.Propagators.csproj", "{E91B2E40-E428-43B3-8A43-09709F0E69E4}"
Expand Down Expand Up @@ -434,6 +436,10 @@ Global
{41B784AA-3301-4126-AF9F-1D59BD04B0BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41B784AA-3301-4126-AF9F-1D59BD04B0BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41B784AA-3301-4126-AF9F-1D59BD04B0BF}.Release|Any CPU.Build.0 = Release|Any CPU
{D4519DF6-CC72-4AC4-A851-E21383098D11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4519DF6-CC72-4AC4-A851-E21383098D11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4519DF6-CC72-4AC4-A851-E21383098D11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4519DF6-CC72-4AC4-A851-E21383098D11}.Release|Any CPU.Build.0 = Release|Any CPU
{6C7A1595-36D6-4229-BBB5-5A6B5791791D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C7A1595-36D6-4229-BBB5-5A6B5791791D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C7A1595-36D6-4229-BBB5-5A6B5791791D}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Empty file.

Large diffs are not rendered by default.

Empty file.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
<Description>OpenTelemetry Semantic Conventions</Description>
<PackageTags>$(PackageTags);semantic-conventions</PackageTags>
</PropertyGroup>

</Project>
34 changes: 34 additions & 0 deletions src/OpenTelemetry.SemanticConventions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Semantic Conventions for OpenTelemetry .NET

This project contains the generated code for the Semantic Conventions
defined by the OpenTelemetry specification.

## Installation

```shell
dotnet add package OpenTelemetry.SemanticConventions
```

## Generating the files

This project uses the
[Semantic Convention Generator](https://github.com/open-telemetry/build-tools/blob/main/semantic-conventions/README.md).
The folder `scripts` at the top level of the project contains
the templates and the script file used in the process.

To generate the code files, run:

```shell
./scripts/semantic-conventions/generate.sh
```

Or, with PowerShell:

```shell
./scripts/semantic-conventions/generate.ps1
```

## References

* [OpenTelemetry Project](https://opentelemetry.io/)
* [Build tools](https://github.com/open-telemetry/build-tools)
1,028 changes: 1,028 additions & 0 deletions src/OpenTelemetry.SemanticConventions/Resource/ResourceSemanticConventions.cs

Large diffs are not rendered by default.

1,878 changes: 1,878 additions & 0 deletions src/OpenTelemetry.SemanticConventions/Trace/TraceSemanticConventions.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
opentelemetry-specification/
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

$SCRIPT_DIR=$PSScriptRoot
$ROOT_DIR="${SCRIPT_DIR}/../../"

# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible
$SPEC_VERSION="v1.13.0"
$SCHEMA_URL="https://opentelemetry.io/schemas/$SPEC_VERSION"
$GENERATOR_VERSION="0.14.0"

Set-Location $SCRIPT_DIR

Remove-Item -r -fo opentelemetry-specification
mkdir opentelemetry-specification
Set-Location opentelemetry-specification

git init
git remote add origin https://github.com/open-telemetry/opentelemetry-specification.git
git fetch origin $SPEC_VERSION
git reset --hard FETCH_HEAD
Set-Location ${SCRIPT_DIR}

docker run --rm `
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions/trace:/source `
-v ${SCRIPT_DIR}/templates:/templates `
-v ${ROOT_DIR}/Trace:/output `
otel/semconvgen:$GENERATOR_VERSION `
-f /source code `
--template /templates/SemanticConventions.cs.j2 `
--output /output/TraceSemanticConventions.cs `
--trim-whitespace `
-D class=TraceSemanticConventions `
-D schemaUrl=$SCHEMA_URL `
-D pkg=OpenTelemetry.Trace

docker run --rm `
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions/resource:/source `
-v ${SCRIPT_DIR}/templates:/templates `
-v ${ROOT_DIR}/Resource:/output `
otel/semconvgen:$GENERATOR_VERSION `
-f /source code `
--template /templates/SemanticConventions.cs.j2 `
--output /output/ResourceSemanticConventions.cs `
--trim-whitespace `
-D class=ResourceSemanticConventions `
-D schemaUrl=$SCHEMA_URL `
-D pkg=OpenTelemetry.Resources

Set-Location ${ROOT_DIR}

# Write-Host "Running dotnet-format on the generated files"
# dotnet format --severity warn
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT_DIR="${SCRIPT_DIR}/../../"

# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible
SPEC_VERSION=v1.13.0
SCHEMA_URL=https://opentelemetry.io/schemas/$SPEC_VERSION
GENERATOR_VERSION=0.14.0

cd ${SCRIPT_DIR}

rm -rf opentelemetry-specification || true
mkdir opentelemetry-specification
cd opentelemetry-specification

git init
git remote add origin https://github.com/open-telemetry/opentelemetry-specification.git
git fetch origin "$SPEC_VERSION"
git reset --hard FETCH_HEAD
cd ${SCRIPT_DIR}

docker run --rm \
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions/trace:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/Trace:/output \
otel/semconvgen:$GENERATOR_VERSION \
-f /source code \
--template /templates/SemanticConventions.cs.j2 \
--output /output/TraceSemanticConventions.cs \
--trim-whitespace \
-Dclass=TraceSemanticConventions \
-DschemaUrl=$SCHEMA_URL \
-Dpkg=OpenTelemetry.Trace

docker run --rm \
-v ${SCRIPT_DIR}/opentelemetry-specification/semantic_conventions/resource:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/Resource:/output \
otel/semconvgen:$GENERATOR_VERSION \
-f /source code \
--template /templates/SemanticConventions.cs.j2 \
--output /output/ResourceSemanticConventions.cs \
--trim-whitespace \
-Dclass=ResourceSemanticConventions \
-DschemaUrl=$SCHEMA_URL \
-Dpkg=OpenTelemetry.Resources
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{%- macro print_value(type, value) -%}
{{ "\"" if type == "string"}}{{value}}{{ "\"" if type == "string"}}
{%- endmacro %}

{%- macro format_remarks(text) -%}
{%- set notes = '\n /// '.join(text.splitlines()).encode('ascii', 'xmlcharrefreplace').decode() -%}
{{notes}}
{%- endmacro -%}

{%- macro format_xml_doc(text) -%}
{%- set escaped = text.encode('ascii', 'xmlcharrefreplace').decode() -%}
{%- if not escaped.endswith('.')-%}
{{escaped + '.'}}
{%- else -%}
{{escaped}}
{%- endif -%}
{%- endmacro -%}

// <copyright file="{{class}}.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

// <auto-generated> This file has been auto generated from buildscripts/semantic-conventions{{template}}</auto-generated>

using System;

namespace {{pkg | trim}}
{
/// <summary>
/// Constants for semantic attribute names outlined by the OpenTelemetry specifications.
{% if class == "TraceSemanticConventions" %}
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/README.md"/>.
{% elif class == "ResourceSemanticConventions" %}
/// <see href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md"/>.
{% endif %}
/// </summary>
/// <remarks>
/// Schema and specification version: {{schemaUrl}}.
/// </remarks>
public static class {{class}}
{
{% if class == "TraceSemanticConventions" %}
/// <summary>
/// Attribute for db.instance.
/// </summary>
public const string AttributeDbInstance = "db.instance";

{% endif %}
{% for attribute in attributes if attribute.is_local and not attribute.ref %}
{% if not loop.first %}{{"\n"}}{% endif %}
/// <summary>
/// {{format_xml_doc(attribute.brief | render_markdown(code="<c>{0}</c>", paragraph="{0}"))}}
/// </summary>
{% if attribute.note %}
/// <remarks>
/// {{format_remarks(attribute.note | to_doc_brief | escape | render_markdown(code="<c>{0}</c>", paragraph="{0}"))}}.
/// </remarks>
{% endif %}
{% if attribute.deprecated %}
[Obsolete("{{attribute.deprecated | to_doc_brief}}")]
{% endif %}
public const string Attribute{{attribute.fqn | replace("-","_") | to_camelcase(True)}} = "{{attribute.fqn}}";
{% endfor %}
{% for semconv in semconvs.values() | unique(attribute="prefix") %}
{% if semconv.prefix %}

/// <summary>
/// Prefix for '{{semconv.semconv_id}}'.
/// </summary>
public static readonly string Prefix{{semconv.semconv_id | replace("-","_") | to_camelcase(True)}} = "{{semconv.prefix}}";
{% endif %}
{% endfor %}
{% for semconv in semconvs.values() %}
{% if semconv.GROUP_TYPE_NAME == 'event' %}

/// <summary>
/// Event name for '{{semconv.semconv_id}}'.
/// </summary>
public static readonly string Event{{semconv.semconv_id | replace("-","_") | to_camelcase(True)}} = "{{semconv.name}}";
{% endif %}
{% endfor %}
{% for attribute in attributes if attribute.is_local and not attribute.ref %}
{% if attribute.is_enum %}
{% set class_name = attribute.fqn | to_camelcase(True) ~ "Values" %}
{% set type = attribute.attr_type.enum_type %}

/// <summary>
/// {{format_xml_doc(attribute.brief | render_markdown(code="<c>{0}</c>", paragraph="{0}"))}}
/// </summary>
public static class {{class_name}}
{
{% for member in attribute.attr_type.members %}
/// <summary>
/// {{format_xml_doc(member.brief | render_markdown(code="<c>{0}</c>", paragraph="{0}"))}}
/// </summary>
public const {{ type }} {{ member.member_id | to_camelcase(True) }} = {{ print_value(type, member.value) }};
{% if not loop.last %}{{"\n"}}{% endif %}
{% endfor %}
}
{% endif %}
{% endfor %}
}
}

0 comments on commit 2377d00

Please sign in to comment.