1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00
opentelemetry-go/semconv/template.j2
Tyler Yahn 0446207a38
Add funcs to semconv to create semantic convention KeyValues (#3675)
* Add semconv KeyValue funcs

* Add changes to changelog

* Variadic slice func parameters

---------

Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
2023-02-06 09:41:41 -08:00

143 lines
5.2 KiB
Django/Jinja

{%- macro keyval_method(type) -%}
{%- if type == "string" -%}
String
{%- elif type == "string[]" -%}
StringSlice
{%- elif type == "int" -%}
Int
{%- elif type == "int[]" -%}
IntSlice
{%- elif type == "double" -%}
Float64
{%- elif type == "double[]" -%}
Float64Slice
{%- elif type == "boolean" -%}
Bool
{%- elif type == "boolean[]" -%}
BoolSlice
{%- endif -%}
{%- endmacro -%}
{%- macro to_go_attr_type(type, val) -%}
{{keyval_method(type)}}({% if type == "string" %}"{{val}}"{% else %}{{val}}{% endif %})
{%- endmacro -%}
{%- macro to_go_name(fqn) -%}
{{fqn | replace(".", " ") | replace("_", " ") | title | replace(" ", "")}}
{%- endmacro -%}
{%- 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(attr) -%}
{{ to_go_name(attr.fqn) }}Key is the attribute Key conforming to the "{{ attr.fqn }}" semantic conventions. {{ it_reps(attr.brief) }}
{%- endmacro -%}
{%- macro keydetails(attr) -%}
{%- if attr.attr_type is string %}
Type: {{ attr.attr_type }}
{%- else %}
Type: Enum
{%- endif %}
{%- if attr.requirement_level == RequirementLevel.REQUIRED %}
RequirementLevel: Required
{%- elif attr.requirement_level == RequirementLevel.CONDITIONALLY_REQUIRED %}
RequirementLevel: ConditionallyRequired
{%- if attr.requirement_level_msg != "" %} ({{ attr.requirement_level_msg }}){%- endif %}
{%- elif attr.requirement_level == RequirementLevel.RECOMMENDED %}
RequirementLevel: Recommended
{%- if attr.requirement_level_msg != "" %} ({{ attr.requirement_level_msg }}){%- endif %}
{%- else %}
RequirementLevel: Optional
{%- endif %}
{{ attr.stability | replace("Level.", ": ") | capitalize }}
{%- if attr.deprecated != None %}
Deprecated: {{ attr.deprecated }}
{%- endif %}
{%- if attr.examples is iterable %}
Examples: {{ attr.examples | pprint | trim("[]") }}
{%- endif %}
{%- if attr.note %}
Note: {{ attr.note }}
{%- endif %}
{%- endmacro -%}
{%- macro fndoc(attr) -%}
// {{ to_go_name(attr.fqn) }} returns an attribute KeyValue conforming to the "{{ attr.fqn }}" semantic conventions. {{ it_reps(attr.brief) }}
{%- endmacro -%}
{%- macro to_go_func(type, name) -%}
{%- if type == "string" -%}
func {{name}}(val string) attribute.KeyValue {
{%- elif type == "string[]" -%}
func {{name}}(val ...string) attribute.KeyValue {
{%- elif type == "int" -%}
func {{name}}(val int) attribute.KeyValue {
{%- elif type == "int[]" -%}
func {{name}}(val ...int) attribute.KeyValue {
{%- elif type == "double" -%}
func {{name}}(val float64) attribute.KeyValue {
{%- elif type == "double[]" -%}
func {{name}}(val ...float64) attribute.KeyValue {
{%- elif type == "boolean" -%}
func {{name}}(val bool) attribute.KeyValue {
{%- elif type == "boolean[]" -%}
func {{name}}(val ...bool) attribute.KeyValue {
{%- endif -%}
return {{name}}Key.{{keyval_method(type)}}(val)
}
{%- endmacro -%}
{%- macro sentence_case(text) -%}
{{ text[0]|upper}}{{text[1:] }}
{%- endmacro -%}
// 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.
// Code generated from semantic convention specification. DO NOT EDIT.
package semconv // import [[IMPORTPATH]]
import "go.opentelemetry.io/otel/attribute"
{% for semconv in semconvs -%}
{%- if semconvs[semconv].attributes | rejectattr("ref") | selectattr("is_local") | sort(attribute=fqn) | length > 0 -%}
// {{ sentence_case(semconvs[semconv].brief | replace("This document defines ", "")) | wordwrap(76, break_long_words=false, break_on_hyphens=false, wrapstring="\n// ") }}
const (
{%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref %}
// {{ keydoc(attr) | wordwrap(72, break_long_words=false, break_on_hyphens=false, wrapstring="\n\t// ") }}
// {{ keydetails(attr) | wordwrap(72, break_long_words=false, break_on_hyphens=false, wrapstring="\n\t// ") }}
{{to_go_name(attr.fqn)}}Key = attribute.Key("{{attr.fqn}}")
{% endfor -%}
)
{%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref -%}
{%- if attr.attr_type is not string %}
var (
{%- for val in attr.attr_type.members %}
// {{ val.brief | to_doc_brief }}
{{to_go_name("{}.{}".format(attr.fqn, val.member_id))}} = {{to_go_name(attr.fqn)}}Key.{{to_go_attr_type(attr.attr_type.enum_type, val.value)}}
{%- endfor %}
)
{%- endif -%}
{%- endfor %}
{%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref -%}
{%- if attr.attr_type is string %}
{{ fndoc(attr) | wordwrap(76, break_long_words=false, break_on_hyphens=false, wrapstring="\n// ") }}
{{to_go_func(attr.attr_type, to_go_name(attr.fqn))}}
{%- endif -%}
{%- endfor %}
{% endif %}
{% endfor -%}