Skip to content

Commit 0446207

Browse files
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 <[email protected]>
1 parent d3986ef commit 0446207

File tree

4 files changed

+3097
-659
lines changed

4 files changed

+3097
-659
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- Attribute `KeyValue` creations functions to `go.opentelemetry.io/otel/semconv/v1.17.0` for all non-enum semantic conventions.
14+
These functions ensure semantic convention type correctness.
15+
1116
### Removed
1217

1318
- The deprecated `go.opentelemetry.io/otel/metric/instrument/asyncfloat64` package is removed. (#3631)

semconv/template.j2

+69-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
{%- macro to_go_attr_type(type, val) -%}
1+
{%- macro keyval_method(type) -%}
22
{%- if type == "string" -%}
3-
String("{{val}}")
3+
String
4+
{%- elif type == "string[]" -%}
5+
StringSlice
46
{%- elif type == "int" -%}
5-
Int({{val}})
7+
Int
8+
{%- elif type == "int[]" -%}
9+
IntSlice
10+
{%- elif type == "double" -%}
11+
Float64
12+
{%- elif type == "double[]" -%}
13+
Float64Slice
14+
{%- elif type == "boolean" -%}
15+
Bool
16+
{%- elif type == "boolean[]" -%}
17+
BoolSlice
618
{%- endif -%}
719
{%- endmacro -%}
20+
{%- macro to_go_attr_type(type, val) -%}
21+
{{keyval_method(type)}}({% if type == "string" %}"{{val}}"{% else %}{{val}}{% endif %})
22+
{%- endmacro -%}
823
{%- macro to_go_name(fqn) -%}
924
{{fqn | replace(".", " ") | replace("_", " ") | title | replace(" ", "")}}
1025
{%- endmacro -%}
11-
{%- macro godoc(attr) -%}
12-
{{ attr.brief }}
13-
//
26+
{%- macro it_reps(brief) -%}
27+
It represents {% if brief[:2] == "A " or brief[:3] == "An " or brief[:4] == "The " -%}
28+
{{ brief[0]|lower }}{{ brief[1:] }}
29+
{%- else -%}
30+
the {{ brief[0]|lower }}{{ brief[1:] }}
31+
{%- endif -%}
32+
{%- endmacro -%}
33+
{%- macro keydoc(attr) -%}
34+
{{ to_go_name(attr.fqn) }}Key is the attribute Key conforming to the "{{ attr.fqn }}" semantic conventions. {{ it_reps(attr.brief) }}
35+
{%- endmacro -%}
36+
{%- macro keydetails(attr) -%}
1437
{%- if attr.attr_type is string %}
1538
Type: {{ attr.attr_type }}
1639
{%- else %}
@@ -38,6 +61,33 @@ Examples: {{ attr.examples | pprint | trim("[]") }}
3861
Note: {{ attr.note }}
3962
{%- endif %}
4063
{%- endmacro -%}
64+
{%- macro fndoc(attr) -%}
65+
// {{ to_go_name(attr.fqn) }} returns an attribute KeyValue conforming to the "{{ attr.fqn }}" semantic conventions. {{ it_reps(attr.brief) }}
66+
{%- endmacro -%}
67+
{%- macro to_go_func(type, name) -%}
68+
{%- if type == "string" -%}
69+
func {{name}}(val string) attribute.KeyValue {
70+
{%- elif type == "string[]" -%}
71+
func {{name}}(val ...string) attribute.KeyValue {
72+
{%- elif type == "int" -%}
73+
func {{name}}(val int) attribute.KeyValue {
74+
{%- elif type == "int[]" -%}
75+
func {{name}}(val ...int) attribute.KeyValue {
76+
{%- elif type == "double" -%}
77+
func {{name}}(val float64) attribute.KeyValue {
78+
{%- elif type == "double[]" -%}
79+
func {{name}}(val ...float64) attribute.KeyValue {
80+
{%- elif type == "boolean" -%}
81+
func {{name}}(val bool) attribute.KeyValue {
82+
{%- elif type == "boolean[]" -%}
83+
func {{name}}(val ...bool) attribute.KeyValue {
84+
{%- endif -%}
85+
return {{name}}Key.{{keyval_method(type)}}(val)
86+
}
87+
{%- endmacro -%}
88+
{%- macro sentence_case(text) -%}
89+
{{ text[0]|upper}}{{text[1:] }}
90+
{%- endmacro -%}
4191
// Copyright The OpenTelemetry Authors
4292
//
4393
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -60,12 +110,13 @@ import "go.opentelemetry.io/otel/attribute"
60110

61111
{% for semconv in semconvs -%}
62112
{%- if semconvs[semconv].attributes | rejectattr("ref") | selectattr("is_local") | sort(attribute=fqn) | length > 0 -%}
63-
// {{ semconvs[semconv].brief }}
113+
// {{ sentence_case(semconvs[semconv].brief | replace("This document defines ", "")) | wordwrap(76, break_long_words=false, break_on_hyphens=false, wrapstring="\n// ") }}
64114
const (
65-
{% for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref -%}
66-
// {{ godoc(attr) | wordwrap | indent(3) | replace(" ", "\t// ") | replace("// //", "//") }}
67-
{{to_go_name(attr.fqn)}}Key = attribute.Key("{{attr.fqn}}")
68-
{% endfor %}
115+
{%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref %}
116+
// {{ keydoc(attr) | wordwrap(72, break_long_words=false, break_on_hyphens=false, wrapstring="\n\t// ") }}
117+
// {{ keydetails(attr) | wordwrap(72, break_long_words=false, break_on_hyphens=false, wrapstring="\n\t// ") }}
118+
{{to_go_name(attr.fqn)}}Key = attribute.Key("{{attr.fqn}}")
119+
{% endfor -%}
69120
)
70121
{%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref -%}
71122
{%- if attr.attr_type is not string %}
@@ -78,6 +129,13 @@ var (
78129
)
79130
{%- endif -%}
80131
{%- endfor %}
132+
{%- for attr in semconvs[semconv].attributes if attr.is_local and not attr.ref -%}
133+
{%- if attr.attr_type is string %}
134+
135+
{{ fndoc(attr) | wordwrap(76, break_long_words=false, break_on_hyphens=false, wrapstring="\n// ") }}
136+
{{to_go_func(attr.attr_type, to_go_name(attr.fqn))}}
137+
{%- endif -%}
138+
{%- endfor %}
81139

82140
{% endif %}
83141
{% endfor -%}

0 commit comments

Comments
 (0)