Skip to content

Commit

Permalink
Use Weaver to generate semconv
Browse files Browse the repository at this point in the history
Use new workflow to generate v1.27.0.
  • Loading branch information
jsuereth authored and MrAlias committed Oct 21, 2024
1 parent bf6a7e1 commit dab26e9
Show file tree
Hide file tree
Showing 9 changed files with 12,941 additions and 9,823 deletions.
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ $(PYTOOLS)/%: $(PYTOOLS)
CODESPELL = $(PYTOOLS)/codespell
$(CODESPELL): PACKAGE=codespell

# Definitions for semconvgen
DOCKER_USER=$(shell id -u):$(shell id -g)
# TODO - Pull docker image versions from rennovate-friendly source, e.g.
# $(shell cat dependencies.Dockerfile | awk '$$4=="weaver" {print $$2}')
WEAVER_CONTAINER=otel/weaver:v0.10.0

# Generate

.PHONY: generate
Expand Down Expand Up @@ -257,11 +263,25 @@ check-clean-work-tree:

SEMCONVPKG ?= "semconv/"
.PHONY: semconv-generate
semconv-generate: $(SEMCONVGEN) $(SEMCONVKIT)
semconv-generate: $(SEMCONVKIT)
[ "$(TAG)" ] || ( echo "TAG unset: missing opentelemetry semantic-conventions tag"; exit 1 )
[ "$(OTEL_SEMCONV_REPO)" ] || ( echo "OTEL_SEMCONV_REPO unset: missing path to opentelemetry semantic-conventions repo"; exit 1 )
$(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=attribute_group -p conventionType=trace -f attribute_group.go -z "$(SEMCONVPKG)/capitalizations.txt" -t "$(SEMCONVPKG)/template.j2" -s "$(TAG)"
$(SEMCONVGEN) -i "$(OTEL_SEMCONV_REPO)/model/." --only=metric -f metric.go -t "$(SEMCONVPKG)/metric_template.j2" -s "$(TAG)"
# Ensure the target directory for source code is available.
mkdir -p $(PWD)/$(SEMCONVPKG)/${TAG}
# Note: We mount a home directory for downloading/storing the semconv repository.
# Weaver will automatically clean the cache when finished, but the directories will remain.
mkdir -p ~/.weaver
docker run --rm \
-u $(DOCKER_USER) \
--env HOME=/tmp/weaver \
--mount 'type=bind,source=$(PWD)/semconv,target=/home/weaver/templates/registry/go,readonly' \
--mount 'type=bind,source=$(PWD)/semconv/${TAG},target=/home/weaver/target' \
--mount 'type=bind,source=$(HOME)/.weaver,target=/tmp/weaver/.weaver' \
$(WEAVER_CONTAINER) registry generate \
--registry=https://github.com/open-telemetry/semantic-conventions.git@$(TAG)#model \
--templates=/home/weaver/templates \
--param tag=$(TAG) \
go \
/home/weaver/target
$(SEMCONVKIT) -output "$(SEMCONVPKG)/$(TAG)" -tag "$(TAG)"

.PHONY: gorelease
Expand Down
81 changes: 81 additions & 0 deletions semconv/attribute_group.go.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{% import 'helpers.j2' as h %}
{# TODO - REQUIREMNET LEVEL SHOULD NOT BE USED IN ATTRIBUTE REGISTRY #}
{%- macro requirement_level_doc(attr) -%}
{%- if attr.requirement_level == "required" -%}
Requirement Level: Required
{% elif attr.requirement_level.conditionally_required %}
Requirement Level: Conditionally Required - {{ attr.requirement_level.conditionally_required }}
{%- elif attr.requirement_level == "recommended" -%}
Requirement Level: Recommended
{% elif attr.requirement_level.recommended %}
Requirement Level: Recommended - {{ attr.requirement_level.recommended }}
{%- else -%}
Requirement Level: Optional
{%- endif %}
{%- endmacro -%}
{%- macro deprecated_doc(attr) -%}
{% if attr is deprecated %}Deprecated: {{ attr.deprecated }}{% endif %}
{%- endmacro -%}
{%- macro notes_doc(attr) -%}
{% if attr.note %}Note: {{ attr.note }}{% endif %}
{%- endmacro -%}
{%- macro examples_doc(attr) -%}
{%- if attr.examples is iterable %}
Examples: {{ attr.examples | pprint | trim("[]") }}
{%- endif -%}
{%- endmacro -%}
{%- macro keydoc(attr) -%}
{{ attr.brief }}
Stability: {{ attr.stability | title }}
{% if attr is enum %}Type: Enum{% else %}Type: {{ attr.type }}{% endif %}
{{ deprecated_doc(attr) }}
{{ examples_doc(attr) }}
{{ notes_doc(attr) }}
{%- endmacro -%}
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Code generated from semantic convention specification. DO NOT EDIT.

package semconv // import "go.opentelemetry.io/otel/semconv/{{params.tag}}"

import "go.opentelemetry.io/otel/attribute"

{% for group in ctx %}
{% if group.attributes | length > 0 %}
{# TODO - we're grouping by registry namespace, not attribute group, so we lose group docs #}
{{ ["Namespace: " ~ group.root_namespace] | comment(format="go") }}
const (
{% for attribute in group.attributes %}
{%- if not attribute.type is template_type %}{# TODO - Go does not handle template attributes yet!!! #}
{{ keydoc(attribute) | comment(format="go_1tab") }}
{{h.to_go_name(attribute.name)}}Key = attribute.Key("{{attribute.name}}")
{%- endif %}
{%- endfor %}
)
{# Render a construction function #}
{% for attribute in group.attributes %}
{# TODO - Go does not handle template attributes yet!!! #}
{%- if not attribute.type is template_type %}
{{ [h.to_go_name(attribute.name) ~ " returns an attribute KeyValue conforming to the \"" ~ attribute.name ~"\"semantic conventions."] | comment(format="go") }}
{{ ["It represents the " ~ attribute.brief] | comment(format="go") }}
func {{h.to_go_name(attribute.name)}}(val {{attribute.type | instantiated_type | map_text("attribute_type_value")}}) attribute.KeyValue {
return {{h.to_go_name(attribute.name)}}Key.{{attribute.type | instantiated_type | map_text("attribute_type_method")}}(val)
}
{%- endif %}
{% endfor %}
{# Render values for enums #}
{%- for attribute in group.attributes %}
{%- if attribute is enum %}
{{ ["Enum values for " ~ attribute.name] | comment(format="go") }}
var (
{% for value in attribute.type.members %}
{{ [value.brief or value.id, "Stability: " ~ value.stability] | comment(format="go_1tab") }}
{%- if value.deprecated %}{{ value.deprecated | comment(format="go_1tab") }}{% endif %}
{{h.to_go_name(attribute.name ~ "." ~ value.id)}} = {{ h.to_go_name(attribute.name) }}Key.{{attribute.type | instantiated_type | map_text("attribute_type_method")}}({{ value.value | print_member_value }})
{%- endfor %}
)
{%- endif %}
{% endfor %}
{%- endif %}
{% endfor %}
3 changes: 3 additions & 0 deletions semconv/helpers.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{%- macro to_go_name(fqn) -%}
{{ fqn | title_case | replace(" ", "") | acronym }}
{%- endmacro -%}
41 changes: 41 additions & 0 deletions semconv/metric.go.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% import 'helpers.j2' as h %}
{%- macro it_reps(brief) -%}
It represents {% if brief[:2] == "A " or brief[:3] == "An " or brief[:4] == "The " -%}
{{ brief[0]|lower }}{{ brief[1:] }}
{%- else -%}
the {{ brief[0]|lower }}{{ brief[1:] }}
{%- endif -%}
{%- endmacro -%}
{%- macro keydoc(metric) -%}
{%- if not metric.brief -%}
{{ h.to_go_name(metric.metric_name) }} is the metric conforming to the "{{ metric.metric_name}}" semantic conventions.
{%- else -%}
{{ h.to_go_name(metric.metric_name) }} is the metric conforming to the "{{ metric.metric_name}}" semantic conventions. {{ it_reps(metric.brief)|trim(".") }}.
{%- endif %}
{%- endmacro -%}
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Code generated from semantic convention specification. DO NOT EDIT.

package semconv // import "go.opentelemetry.io/otel/semconv/{{params.tag}}"

const (
{% for metric in ctx %}
{{ keydoc(metric) | comment(indent=2) }}
// Instrument: {{ metric.instrument }}
// Unit: {{ metric.unit }}
// Stability: {{ metric.stability }}
{%- if metric is deprecated %}
// Deprecated: {{ metric.deprecated }}
{%- endif %}
{%- if not metric.brief %}
// NOTE: The description (brief) for this metric is not defined in the semantic-conventions repository.
{%- endif %}
{{ h.to_go_name(metric.metric_name) }}Name = "{{ metric.metric_name }}"
{{ h.to_go_name(metric.metric_name) }}Unit = "{{ metric.unit }}"
{%- if metric.brief %}
{{ h.to_go_name(metric.metric_name) }}Description = "{{ metric.brief | trim }}"
{%- endif %}
{% endfor %}
)
49 changes: 0 additions & 49 deletions semconv/metric_template.j2

This file was deleted.

145 changes: 0 additions & 145 deletions semconv/template.j2

This file was deleted.

Loading

0 comments on commit dab26e9

Please sign in to comment.