Skip to content

Commit 9d68afb

Browse files
authored
refactor semantic conventions (#1659)
Align with how the spec advises SemConv should be organised, and break unstable semconvs out into their own directory. I played around with generating values as enums, but I could only get this to work if the output was in the same file as the attributes (which would not work well with composer autoloading), so I've used <attribute>_VALUE_<name>, similar to what we have now except all in the same file. * fix linting, broken values containing '.' * fix incubating namespace * update semconv readme * restore ResourceAttributeValues * adding DNS semconv * adding db metrics
1 parent 4afe0aa commit 9d68afb

File tree

55 files changed

+4385
-294
lines changed

Some content is hidden

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

55 files changed

+4385
-294
lines changed

script/semantic-conventions/semconv.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ SPEC_DIR="${ROOT_DIR}/var/semantic-conventions"
1717
CODE_DIR="${ROOT_DIR}/src/SemConv"
1818

1919
# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible
20-
SEMCONV_VERSION=1.32.0
20+
SEMCONV_VERSION=1.36.0
2121
SPEC_VERSION=v$SEMCONV_VERSION
2222
SCHEMA_URL=https://opentelemetry.io/schemas/$SEMCONV_VERSION
23-
OTEL_WEAVER_IMG_VERSION=v0.15.2
23+
OTEL_WEAVER_IMG_VERSION=v0.16.1
2424

2525
rm -rf "${SPEC_DIR}"
2626
mkdir "${SPEC_DIR}"
@@ -34,7 +34,13 @@ git reset --hard FETCH_HEAD
3434
cd "${SCRIPT_DIR}"
3535

3636
mkdir -p "${CODE_DIR}"
37-
find "${CODE_DIR}" -name "*.php" ! -name "Version.php" -exec rm -f {} \;
37+
find "${CODE_DIR}" -name "*.php" \
38+
! -name "Version.php" \
39+
! -name "ResourceAttributes.php" \
40+
! -name "ResourceAttributeValues.php" \
41+
! -name "TraceAttributes.php" \
42+
! -name "TraceAttributeValues.php" \
43+
-exec rm -f {} \;
3844

3945
echo "${SCHEMA_URL}" > ${SCRIPT_DIR}/templates/registry/php/version.txt
4046

script/semantic-conventions/templates/registry/php/AttributeValues.php.j2

Lines changed: 0 additions & 30 deletions
This file was deleted.

script/semantic-conventions/templates/registry/php/Attributes.php.j2

Lines changed: 0 additions & 33 deletions
This file was deleted.

script/semantic-conventions/templates/registry/php/Metrics.php.j2

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1+
{%- set my_class_name = ctx.root_namespace | pascal_case ~ ("Incubating" if ctx.incubating ) ~ "Metrics" -%}
12
<?php
23

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

56
declare(strict_types=1);
67

7-
namespace OpenTelemetry\SemConv\Metrics;
8+
namespace {{ ctx.namespace }};
89

9-
interface {{ ctx.root_namespace | pascal_case }}Metrics
10+
/**
11+
* Metrics for {{ ctx.root_namespace }}.
12+
{% if ctx.incubating %}
13+
* May contain @experimental Semantic Conventions which may change or be removed in the future.
14+
{% endif %}
15+
*/
16+
interface {{ my_class_name }}
1017
{
1118
{# blank line #}
1219
{%- for metric in ctx.metrics %}
1320
{% set const_name = metric.metric_name | screaming_snake_case %}
14-
{%- if metric is deprecated %}
15-
{%- set deprecated_phpdoc = "@deprecated" -%}
16-
{% else %}
17-
{% set deprecated_phpdoc = "" %}
18-
{%- endif -%}
21+
{%- set instrument_phpdoc = "Instrument: " ~ metric.instrument -%}
22+
{%- set unit_phpdoc = "Unit: " ~ metric.unit -%}
1923
{%- if metric is stable -%}
2024
{%- set stability_phpdoc = "@stable" -%}
2125
{%- else -%}
2226
{%- set stability_phpdoc = "@experimental" -%}
2327
{%- endif -%}
24-
{%- set instrument_phpdoc = "Instrument: " ~ metric.instrument -%}
25-
{%- set unit_phpdoc = "Unit: " ~ metric.unit -%}
26-
{{ [metric.brief, concat_if(metric.note), instrument_phpdoc, unit_phpdoc, deprecated_phpdoc, stability_phpdoc] | comment(indent=4) | replace(' \n', '\n') }}
28+
{{ [metric.brief, concat_if(metric.note), instrument_phpdoc, unit_phpdoc, stability_phpdoc] | comment(indent=4) | replace(' \n', '\n') }}
2729
public const {{ const_name }} = '{{ metric.metric_name }}';
2830

2931
{% endfor %}

script/semantic-conventions/templates/registry/php/ResourceAttributeValues.php.j2

Lines changed: 0 additions & 34 deletions
This file was deleted.

script/semantic-conventions/templates/registry/php/ResourceAttributes.php.j2

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% import 'common.j2' as c %}
2+
{%- set my_class_name = ctx.root_namespace | pascal_case ~ ("Incubating" if ctx.incubating ) ~ "Attributes" -%}
3+
<?php
4+
5+
// DO NOT EDIT, this is an Auto-generated file from script/semantic-conventions
6+
7+
declare(strict_types=1);
8+
9+
namespace {{ ctx.namespace }};
10+
11+
/**
12+
* Semantic attributes and corresponding values for {{ ctx.root_namespace }}.
13+
* @see https://opentelemetry.io/docs/specs/semconv/registry/attributes/{{ ctx.root_namespace }}/
14+
{% if ctx.incubating %}
15+
* May contain @experimental Semantic Conventions which may change or be removed in the future.
16+
{% endif %}
17+
*/
18+
interface {{ my_class_name }}
19+
{
20+
{% for attribute in ctx.attributes | rejectattr("name", "in", ctx.excluded_attributes) if not (attribute is deprecated) %}
21+
{%- set stability_phpdoc = c.stability(attribute) -%}
22+
{{ [attribute.brief, concat_if(attribute.note), stability_phpdoc] | comment(indent=4) | replace(' \n', '\n') }}
23+
public const {{ c.attribute_name(attribute) }} = '{{ attribute.name }}';
24+
25+
{% if attribute is enum %}{% for member in attribute.type.members if not (member is deprecated) %}
26+
{%- set stability_phpdoc = c.stability(attribute) -%}
27+
{{ [member.brief, stability_phpdoc] | comment(indent=4)}}
28+
public const {{ c.attribute_value_name(attribute, member) }} = {{ member.value | print_member_value | replace('"', "'") }};
29+
30+
{% endfor %}{% endif %}
31+
{% endfor %}
32+
}
33+
{# blank line #}

script/semantic-conventions/templates/registry/php/common.j2

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
{{to_const_name(attribute.name)}}
2828
{%- endmacro -%}
2929

30-
{%- macro attribute_value_name(member) -%}
31-
{{to_const_name(member.id)}}
30+
{%- macro attribute_value_name(attribute, member) -%}
31+
{{ to_const_name(attribute.name) ~ "_VALUE_" ~ to_const_name(member.id) }}
3232
{%- endmacro -%}
3333

34-
{%- macro attribute_stability(attribute) -%}
35-
{% if attribute.deprecated %}deprecated{% else %}{{attribute.stability}}{%endif%}
34+
{%- macro stability(member) -%}
35+
{%- if member is stable -%}
36+
{{ "@stable" }}
37+
{%- else -%}
38+
{{ "@experimental" }}
39+
{%- endif -%}
3640
{%- endmacro -%}

script/semantic-conventions/templates/registry/php/weaver.yaml

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
params:
22
# excluded namespaces will not be generated
33
# this behavior is fully controlled by jinja templates
4-
# TODO exclude more namespaces, eg: ios, aspnetcore, signalr, android, dotnet, jvm, kestrel, browser, device, ...
5-
excluded_namespaces: [cloudfoundry, dotnet, hw, nodejs, profile]
6-
excluded_metrics_namespaces: [aspnetcore, azure, cicd, container, cpu, cpython, db, dns, dotnet, faas, gen_ai, go, hw, jvm, k8s, kestrel, messaging, nodejs, process, rpc, signalr, system, v8js, vcs]
4+
excluded_namespaces: [
5+
android, app, artifact, aws, azure, browser, cassandra, device, disk,
6+
elasticsearch, enduser, event, faas, feature_flag, file, gen_ai, gcp,
7+
geo, go, graphql, heroku, ios, k8s, linux, mainframe, message, network,
8+
oci, openai, opentracing, other, pool, rpc, security_rule, source, system, test,
9+
tls, useragent, v8js, vcs, webengine, zos, az, session, thread, destination,
10+
cicd, cloud, cloudevents, cpu, cpython, deployment, aspnetcore,
11+
cloudfoundry, dotnet, hw, jvm, nodejs, profile, signalr
12+
]
13+
excluded_metrics_namespaces: [
14+
aspnetcore, azure, cicd, container, cpu, cpython, dotnet,
15+
faas, gen_ai, go, hw, jvm, k8s, kestrel, messaging, nodejs, process,
16+
rpc, signalr, system, v8js, vcs
17+
]
718

8-
# excluded attributes will be commented out in the generated code
9-
# this behavior is fully controlled by jinja templates
10-
excluded_attributes: ["messaging.client_id"]
19+
# excluded attributes will be skipped (eg "messaging.client.id" vs "messaging.client_id" breakage)
20+
excluded_attributes: []
1121

1222
text_maps:
1323
php_types:
@@ -18,55 +28,60 @@ text_maps:
1828
string[]: string[]
1929
template[string]: string
2030
template[string[]]: string[]
21-
2231
templates:
23-
- template: Attributes.php.j2
32+
- pattern: SemanticAttributes.php.j2
2433
filter: >
2534
semconv_grouped_attributes({
2635
"exclude_root_namespace": $excluded_namespaces,
27-
"exclude_stability": [],
36+
"exclude_stability": [ "experimental", "deprecated" ]
2837
}) | map({
2938
root_namespace: .root_namespace,
3039
attributes: .attributes,
31-
excluded_attributes: $excluded_attributes
40+
excluded_attributes: $excluded_attributes,
41+
namespace: "OpenTelemetry\\SemConv\\Attributes",
42+
experimental: false,
3243
})
33-
application_mode: single
34-
file_name: "TraceAttributes.php"
35-
- template: AttributeValues.php.j2
44+
application_mode: each
45+
file_name: "Attributes/{{ctx.root_namespace | pascal_case}}Attributes.php"
46+
- pattern: SemanticAttributes.php.j2
3647
filter: >
3748
semconv_grouped_attributes({
3849
"exclude_root_namespace": $excluded_namespaces,
39-
"exclude_stability": [],
50+
"exclude_stability": [ "deprecated" ]
51+
}) | map({
52+
root_namespace: .root_namespace,
53+
attributes: .attributes,
54+
excluded_attributes: $excluded_attributes,
55+
namespace: "OpenTelemetry\\SemConv\\Incubating\\Attributes",
56+
incubating: true,
4057
})
41-
application_mode: single
42-
file_name: "TraceAttributeValues.php"
43-
- template: ResourceAttributes.php.j2
44-
filter: >
45-
semconv_signal("resource"; {})
46-
| map(.attributes[])
47-
| group_by(.name)
48-
| map(.[-1])
49-
application_mode: single
50-
file_name: "ResourceAttributes.php"
51-
- template: ResourceAttributeValues.php.j2
52-
filter: >
53-
semconv_signal("resource"; {})
54-
| map(.attributes[])
55-
| group_by(.name)
56-
| map(.[-1])
57-
application_mode: single
58-
file_name: "ResourceAttributeValues.php"
58+
application_mode: each
59+
file_name: "Incubating/Attributes/{{ctx.root_namespace | pascal_case}}IncubatingAttributes.php"
5960
- template: Metrics.php.j2
6061
filter: >
6162
semconv_grouped_metrics({
6263
"exclude_root_namespace": $excluded_metrics_namespaces,
63-
"exclude_stability": [],
64+
"exclude_stability": [ "experimental", "deprecated" ]
6465
}) | map({
6566
root_namespace: .root_namespace,
6667
metrics: .metrics,
68+
namespace: "OpenTelemetry\\SemConv\\Metrics",
6769
})
6870
application_mode: each
6971
file_name: "Metrics/{{ctx.root_namespace | pascal_case}}Metrics.php"
72+
- template: Metrics.php.j2
73+
filter: >
74+
semconv_grouped_metrics({
75+
"exclude_root_namespace": $excluded_metrics_namespaces,
76+
"exclude_stability": [ "deprecated" ]
77+
}) | map({
78+
root_namespace: .root_namespace,
79+
metrics: .metrics,
80+
namespace: "OpenTelemetry\\SemConv\\Unstable\\Metrics",
81+
incubating: true,
82+
})
83+
application_mode: each
84+
file_name: "Incubating/Metrics/{{ctx.root_namespace | pascal_case}}IncubatingMetrics.php"
7085
whitespace_control:
7186
trim_blocks: true
7287
lstrip_blocks: true

0 commit comments

Comments
 (0)