Skip to content

Commit

Permalink
use weaver to generate semconv (#1398)
Browse files Browse the repository at this point in the history
update semconv generation to use the new tooling. This is fairly close to a 1:1 of what we have now,
although I noted a couple of TODOs which we might consider in future to align with what Java is
doing: split attribute up into file-per-type, and split stable/unstable.
  • Loading branch information
brettmc authored Oct 17, 2024
1 parent 01d0fd3 commit d2b1507
Show file tree
Hide file tree
Showing 18 changed files with 1,359 additions and 2,633 deletions.
2 changes: 1 addition & 1 deletion examples/traces/features/span_resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
ResourceAttributes::SERVICE_NAME => 'bar',
ResourceAttributes::SERVICE_INSTANCE_ID => 1,
ResourceAttributes::SERVICE_VERSION => '0.1',
ResourceAttributes::DEPLOYMENT_ENVIRONMENT => 'development',
ResourceAttributes::DEPLOYMENT_ENVIRONMENT_NAME => 'development',
])));

$tracerProvider = new TracerProvider(
Expand Down
90 changes: 19 additions & 71 deletions script/semantic-conventions/semconv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
# Source repositories:
# - https://github.com/open-telemetry/semantic-conventions/releases
# - https://github.com/open-telemetry/build-tools/releases
set -e
set -ex

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT_DIR="${SCRIPT_DIR}/../../"
ROOT_DIR="${SCRIPT_DIR}/../.."
SPEC_DIR="${ROOT_DIR}/var/semantic-conventions"
CODE_DIR="${ROOT_DIR}/src/SemConv"

# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible
SEMCONV_VERSION=${SEMCONV_VERSION:=1.27.0}
SEMCONV_VERSION=1.27.0
SPEC_VERSION=v$SEMCONV_VERSION
SCHEMA_URL=https://opentelemetry.io/schemas/$SEMCONV_VERSION
GENERATOR_VERSION=0.25.0

cd "${SCRIPT_DIR}"
OTEL_WEAVER_IMG_VERSION=v0.10.0

rm -rf "${SPEC_DIR}"
mkdir "${SPEC_DIR}"
Expand All @@ -38,68 +36,18 @@ cd "${SCRIPT_DIR}"
mkdir -p "${CODE_DIR}"
find "${CODE_DIR}" -name "*.php" ! -name "Version.php" -exec rm -f {} \;

# Trace
docker run --rm \
-v "${SPEC_DIR}/model:/source" \
-v "${SCRIPT_DIR}/templates:/templates" \
-v "${CODE_DIR}:/output" \
-u "${UID}" \
otel/semconvgen:$GENERATOR_VERSION \
--only span,event,attribute_group \
--yaml-root /source \
code \
--template /templates/Attributes.php.j2 \
--output "/output/TraceAttributes.php" \
-Dnamespace="OpenTelemetry\\SemConv" \
-Dclass="Trace" \
-DschemaUrl=$SCHEMA_URL

# Resource
docker run --rm \
-v "${SPEC_DIR}/model:/source" \
-v "${SCRIPT_DIR}/templates:/templates" \
-v "${CODE_DIR}:/output" \
-u "${UID}" \
otel/semconvgen:$GENERATOR_VERSION \
--only resource \
--yaml-root /source \
code \
--template /templates/Attributes.php.j2 \
--output "/output/ResourceAttributes.php" \
-Dnamespace="OpenTelemetry\\SemConv" \
-Dclass="Resource" \
-DschemaUrl=$SCHEMA_URL

# Trace attribute values
docker run --rm \
-v "${SPEC_DIR}/model:/source" \
-v "${SCRIPT_DIR}/templates:/templates" \
-v "${CODE_DIR}:/output" \
-u "${UID}" \
otel/semconvgen:$GENERATOR_VERSION \
--only span,event,attribute_group \
--yaml-root /source \
code \
--template /templates/AttributeValues.php.j2 \
--output "/output/TraceAttributeValues.php" \
-Dnamespace="OpenTelemetry\\SemConv" \
-Dclass="Trace" \
-DschemaUrl=$SCHEMA_URL

# Resource attribute values
docker run --rm \
-v "${SPEC_DIR}/model:/source" \
-v "${SCRIPT_DIR}/templates:/templates" \
-v "${CODE_DIR}:/output" \
-u "${UID}" \
otel/semconvgen:$GENERATOR_VERSION \
--only resource \
--yaml-root /source \
code \
--template /templates/AttributeValues.php.j2 \
--output "/output/ResourceAttributeValues.php" \
-Dnamespace="OpenTelemetry\\SemConv" \
-Dclass="Resource" \
-DschemaUrl=$SCHEMA_URL

rm -rf "${SPEC_DIR}"
echo "${SCHEMA_URL}" > ${SCRIPT_DIR}/templates/registry/php/version.txt

generate () {
docker run --rm \
-v "${SPEC_DIR}/model:/home/weaver/model" \
-v "${SCRIPT_DIR}/templates:/home/weaver/templates" \
-v "${CODE_DIR}:/home/weaver/output" \
-u "1000" \
otel/weaver:$OTEL_WEAVER_IMG_VERSION \
registry generate php
}

#TODO split stable from experimental
#TODO one file per group? (see java's implementation)
generate
42 changes: 0 additions & 42 deletions script/semantic-conventions/templates/AttributeValues.php.j2

This file was deleted.

39 changes: 0 additions & 39 deletions script/semantic-conventions/templates/Attributes.php.j2

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% import 'common.j2' as c %}
<?php

// DO NOT EDIT, this is an Auto-generated file from script/semantic-conventions/

declare(strict_types=1);

namespace OpenTelemetry\SemConv;

interface TraceAttributeValues
{
/**
* The URL of the OpenTelemetry schema for these values.
*/
public const SCHEMA_URL = '{% include "version.txt" without context %}';
{% for section in ctx %}
{% for attribute in section.attributes %}
{% if "members" in attribute.type and attribute.type.members | length > 0 %}
{% for member in attribute.type.members %}
{%- set see = "@see TraceAttributes::" ~ c.attribute_name(attribute) -%}
{%- set deprecated = "@deprecated " ~ member.deprecated if member.deprecated != none else "" -%}
{{ [member.brief or member.id, '\n', see, deprecated] | comment(indent=4) | replace(' \n', '\n') }}
public const {{ c.attribute_name(attribute) + "_" + c.attribute_value_name(member) }} = '{{ member.value }}';

{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
}
{# blank line #}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% import 'common.j2' as c %}
<?php

// DO NOT EDIT, this is an Auto-generated file from script/semantic-conventions

declare(strict_types=1);

namespace OpenTelemetry\SemConv;

interface TraceAttributes
{
/**
* The URL of the OpenTelemetry schema for these keys and values.
*/
public const SCHEMA_URL = '{% include "version.txt" without context %}';

{% for section in ctx %}
{#{ debug(section) }#}
{% for attribute in section.attributes %}
{% if attribute.name not in section.excluded_attributes %}
{%- if attribute is deprecated %}
{%- set deprecated_phpdoc = "@deprecated " ~ attribute.deprecated -%}
{% else %}
{% set deprecated_phpdoc = "" %}
{%- endif -%}
{{ [attribute.brief, concat_if(attribute.note), deprecated_phpdoc] | comment(indent=4) | replace(' \n', '\n') }}
public const {{ c.attribute_name(attribute) }} = '{{ attribute.name }}';

{% endif %}
{% endfor %}
{% endfor %}
}
{# blank line #}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% import 'common.j2' as c %}
<?php

// DO NOT EDIT, this is an Auto-generated file from script/semantic-conventions

declare(strict_types=1);

namespace OpenTelemetry\SemConv;

interface ResourceAttributeValues
{
/**
* The URL of the OpenTelemetry schema for these values.
*/
public const SCHEMA_URL = '{% include "version.txt" without context %}';

{% for attribute in ctx %}
{% if "members" in attribute.type and attribute.type.members | length > 0 %}
{% for member in attribute.type.members %}
/**
* {{ member.brief or member.id }}
*
* @see ResourceAttributes::{{ c.attribute_name(attribute) }}
{% if attribute.deprecated %}
* @deprecated {{ attribute.deprecated }}
{% endif %}
*/
public const {{ c.attribute_name(attribute) + "_" + c.attribute_value_name(member) }} = '{{ member.value }}';

{% endfor %}
{% endif %}
{% endfor %}
}
{# blank line #}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% import 'common.j2' as c %}
<?php

// DO NOT EDIT, this is an Auto-generated file from script/semantic-conventions

declare(strict_types=1);

namespace OpenTelemetry\SemConv;

interface ResourceAttributes
{
/**
* The URL of the OpenTelemetry schema for these keys and values.
*/
public const SCHEMA_URL = '{% include "version.txt" without context %}';

{% for attribute in ctx %}
{%- if attribute is deprecated %}
{%- set deprecated_phpdoc = "@deprecated " ~ attribute.deprecated -%}
{%- endif -%}
{{ [attribute.brief, concat_if(attribute.note), deprecated_phpdoc] | comment(indent=4) | replace(' \n', '\n') }}
public const {{ c.attribute_name(attribute) }} = '{{ attribute.name }}';

{% endfor %}
}
{# blank line #}
36 changes: 36 additions & 0 deletions script/semantic-conventions/templates/registry/php/common.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{%- macro file_header() -%}
# 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.

{% endmacro -%}

{%- macro to_const_name(attr_name) -%}
{{attr_name | upper | replace('.', '_')}}
{%- endmacro %}

{%- macro print_value(type, value) -%}
{%- if type == "string" -%}"{{value}}"{%-else-%}{{value}}{%-endif-%}
{%- endmacro -%}

{%- macro attribute_name(attribute) -%}
{{to_const_name(attribute.name)}}
{%- endmacro -%}

{%- macro attribute_value_name(member) -%}
{{to_const_name(member.id)}}
{%- endmacro -%}

{%- macro attribute_stability(attribute) -%}
{% if attribute.deprecated %}deprecated{% else %}{{attribute.stability}}{%endif%}
{%- endmacro -%}
Loading

0 comments on commit d2b1507

Please sign in to comment.