Skip to content
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
37 changes: 13 additions & 24 deletions Scripts/semantic-convention/generate.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/bin/bash
set -e
set -e -x

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

# freeze the spec & generator tools versions to make SemanticAttributes generation reproducible

# repository: https://github.com/open-telemetry/semantic-conventions
SEMCONV_VERSION=1.21.0
SEMCONV_VERSION=1.37.0
SPEC_VERSION=v$SEMCONV_VERSION

# repository: https://github.com/open-telemetry/build-tools
GENERATOR_VERSION=0.21.0
GENERATOR_VERSION=0.17.1

cd ${SCRIPT_DIR}

Expand All @@ -28,26 +28,15 @@ cd ${SCRIPT_DIR}
docker run --rm \
-v ${SCRIPT_DIR}/semantic-conventions/model:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/Sources/OpenTelemetryApi/Trace/:/output \
otel/semconvgen:$GENERATOR_VERSION \
--only span,event,attribute_group,scope \
-f /source code \
--template /templates/SemanticAttributes.swift.j2 \
--output /output/SemanticAttributes.swift \
-Dsemconv=trace \
-Denum=SemanticAttributes

docker run --rm \
-v ${SCRIPT_DIR}/semantic-conventions/model:/source \
-v ${SCRIPT_DIR}/templates:/templates \
-v ${ROOT_DIR}/Sources/OpenTelemetrySdk/Resources/:/output \
otel/semconvgen:$GENERATOR_VERSION \
--only resource \
-f /source code \
--template /templates/SemanticAttributes.swift.j2 \
--output /output/ResourceAttributes.swift \
-Dsemconv=resource \
-Denum=ResourceAttributes
-v ${ROOT_DIR}/Sources/OpenTelemetryApi/Common/SemanticAttributes:/output \
otel/weaver:v$GENERATOR_VERSION \
registry \
generate \
--registry=/source \
--templates=/templates \
./ \
/output \
-Doutput=/output/ \

cd "$ROOT_DIR"

Expand Down
128 changes: 0 additions & 128 deletions Scripts/semantic-convention/templates/SemanticAttributes.swift.j2

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{%- macro reserved_words_filter(string) -%}
{%- if string in ["internal", "extension", "defer", "static", "default", "as", "in"] -%}
_{{string}}
{%- else -%}
{{string}}
{%- endif -%}
{%- endmacro -%}


{%- macro to_swift_return_type(type) -%}
{% if type == "string" %}
String
{%- elif type == "string[]" %}
[String]
{%- elif type == "boolean" %}
Bool
{%- elif type == "number" %}
Int
{%- elif type == "int" %}
Int
{%- elif type == "double" %}
Double
{%- else %}
{{type}}
{%- endif %}
{%- endmacro -%}

{%- macro print_value(type, value) -%}
{{ "\"" if type == "String"}}{{value}}{{ "\"" if type == "String"}}
{%- endmacro -%}

{%- set enum_name = ctx.root_namespace | pascal_case | replace('-', "") %}
{%- set filename = ctx.output + (ctx.root_namespace | pascal_case | replace('-', '')) ~ "_attributes.swift" -%}
{{ template.set_file_name(filename) }}

{%- macro print_example(attribute, example) -%}
{%- set attribute_name = "SemanticConventions." + ctx.root_namespace|pascal_case + "." + attribute.name | replace(ctx.root_namespace, "") | camel_case ~ ".rawValue" -%}
{% if attribute.type|lower == "string" or attribute.type|lower == "template[string]" -%}
attributes[{{attribute_name}}] = "{{example | replace("\"", "\\\"")}}"
{% elif attribute is enum %}
attributes[{{attribute_name}}] = .{{example}}
{% else %}
attributes[{{attribute_name}}] = {{example}}
{%- endif %}
{%- endmacro -%}

/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

// DO **NOT** EDIT. This file is automatically generated.

import Foundation

extension SemanticConventions {
public enum {{ enum_name }}: String {
{% set attributes = ctx.attributes %}
{% if ctx.root_namespace == "exception" %}

// MARK: - Manual Definitions
// Some definitions have not yet been added to the YAML which generates this script.
// As such as we have some manually defined cases.
/**
An exception event **MUST** be called "exception" as per the [specification](https://github.com/open-telemetry/semantic-conventions/blob/main/docs/exceptions/exceptions-spans.md#exception-event).
*/
case exception = "exception"

{% endif %}
{% for attribute in attributes | unique(attribute="name") %}
{% set class_name = attribute.name | replace(ctx.root_namespace, '') | pascal_case ~ "Values" %}

/**
{{attribute.brief | comment }}
{% if attribute.examples %}

- Examples:
```
{% if attribute.examples is sequence %}
{% for example in attribute.examples -%}
{{ print_example(attribute, example) | indent(6, true) }}
{% endfor %}
{% else -%}
{{ print_example(attribute, attribute.examples) | indent(6, true) }}
{% endif %}
```
{% endif %}
{% if attribute.note %}

- Note: {{attribute.note | comment(indent=7) }}
{% endif %}
{% if attribute.deprecated %}

- Warning: Deprecated • {{attribute.deprecated | comment}}
{% endif %}
{% if attribute is enum %}

- Requires: Value should be one of ``{{class_name}}`` (of type `{{to_swift_return_type(attribute.type | enum_type |lower)}}`)
{% else %}

- Requires: Value type should be `{{to_swift_return_type(attribute.type|lower)}}`
{% endif %}
*/
{% if attribute.deprecated %}
@available(*, deprecated)
{% endif %}
case {{reserved_words_filter(attribute.name | replace(ctx.root_namespace, "") | camel_case)}} = "{{attribute.name}}"
{% endfor %}
{% for attribute in attributes | unique(attribute="name") %}
{% set class_name = attribute.name | replace(ctx.root_namespace, "") | pascal_case ~ "Values" %}
{% set type = to_swift_return_type(attribute.type) %}
{% if attribute is enum %}

/**
{{attribute.brief | comment}}
*/
{% if attribute is enum %}
public struct {{class_name}}: CustomStringConvertible {
{% for member in attribute.type.members %}
{% if member.brief is defined %}

/// {{member.brief | comment}}
{% endif %}
public static let {{ reserved_words_filter(member.id | camel_case) }} = {{class_name}}({{ print_value(to_swift_return_type(attribute.type | enum_type), member.value) }})
{% endfor %}

internal let value: {{ to_swift_return_type(attribute.type | enum_type) }}

public init(_ customValue: {{to_swift_return_type(attribute.type | enum_type)}}) {
self.value = customValue
}

public var description: String {
{%- if attribute.type | enum_type == "string" %}
return value
{% else %}
return "\(value)"
{% endif %}
}
}
{% else %}
public enum {{class_name}}: {{ type }} {
{% for member in attribute.type.members -%}
{% if member.brief is defined -%}
/*
{{member.brief | comment}}
*/
{% endif %}
case {{ member.member_id | replace(".", "_") | replace("internal", "`internal`") }} = {{ print_value(type, member.value) }}
{% endfor %}
}
{% endif %}
{% endif %}
{% endfor %}
{% if filename == "ResourceAttributes" %}

public func ==(left: ResourceAttributes, right: String) -> Bool {
return left.rawValue == right
}

public func ==(left: String, right: ResourceAttributes) -> Bool {
return left == right.rawValue
}
{% endif %}
}
}
24 changes: 24 additions & 0 deletions Scripts/semantic-convention/templates/registry/weaver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
params:
excluded_namespaces: []
stable_package_name: opentelemetry.semconv
comment_formats:
default:
format: markdown
whitespace_control:
trim_blocks: true
lstrip_blocks: true
templates:
- pattern: SemanticAttributes.swift.j2
filter: >
semconv_grouped_attributes({
"exclude_deprecated": true,
"exclude_experimental": true,
"exclude_root_namespace": $excluded_namespaces,
})
| map({
root_namespace: .root_namespace,
attributes: .attributes,
output: $output + "Attributes/",
})
file_name: "{{ params.enum }}.swift"
application_mode: each
Loading
Loading