| 1 |
{% import 'helpers.j2' as h -%} |
| 2 |
{% import 'instrument.j2' as i -%} |
| 3 |
// Code generated from semantic convention specification. DO NOT EDIT. |
| 4 |
|
| 5 |
// Copyright The OpenTelemetry Authors |
| 6 |
// SPDX-License-Identifier: Apache-2.0 |
| 7 |
|
| 8 |
{% set pkg = ctx.root_namespace | camel_case | lower ~ "conv" -%} |
| 9 |
// Package {{ pkg }} provides types and functionality for OpenTelemetry semantic |
| 10 |
// conventions in the "{{ ctx.root_namespace }}" namespace. |
| 11 |
package {{ pkg }} |
| 12 |
|
| 13 |
import ( |
| 14 |
"context" |
| 15 |
"sync" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
"go.opentelemetry.io/otel/metric" |
| 19 |
"go.opentelemetry.io/otel/metric/noop" |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
addOptPool = &sync.Pool{New: func() any { return &[]metric.AddOption{} }} |
| 24 |
recOptPool = &sync.Pool{New: func() any { return &[]metric.RecordOption{} }} |
| 25 |
) |
| 26 |
{%- for attr in ctx.metrics | map(attribute="attributes") | flatten | selectattr("type", "mapping") | unique(attribute="name") | sort(attribute="name") %} |
| 27 |
{%- set name = h.to_go_name(attr.name, ctx.root_namespace) %} |
| 28 |
|
| 29 |
{{ [ name ~ "Attr is an attribute conforming to the " ~ attr.name ~ " semantic conventions. " ~ h.it_reps(attr.brief) ] | comment }} |
| 30 |
type {{ name }}Attr {{ h.member_type(attr.type.members[0]) }} |
| 31 |
|
| 32 |
var ( |
| 33 |
{%- for m in attr.type.members %} |
| 34 |
{%- set m_name = name ~ h.to_go_name(m.id, ctx.root_namespace) -%} |
| 35 |
|
| 36 |
{% if attr.type.members[0].value is string -%} |
| 37 |
{%- set m_value = '"' + m.value + '"' -%} |
| 38 |
{%- else -%} |
| 39 |
{%- set m_value = m.value -%} |
| 40 |
{%- endif -%} |
| 41 |
|
| 42 |
{%- if m.brief is defined %} |
| 43 |
{%- set m_brief = m.brief -%} |
| 44 |
{%- else %} |
| 45 |
{%- set m_brief = "standardized value " + m_value + ' of ' + name + 'Attr.' -%} |
| 46 |
{%- endif %} |
| 47 |
{{ h.prefix_brief(m_brief, m_name ~ " is ") | comment(format="go_1tab") }} |
| 48 |
{{ m_name }} {{ name }}Attr = {{ m_value }} |
| 49 |
{%- endfor %} |
| 50 |
) |
| 51 |
{%- endfor %} |
| 52 |
|
| 53 |
{%- for metric in ctx.metrics %} |
| 54 |
{%- set metric_name = h.to_go_name(metric.metric_name, ctx.root_namespace) %} |
| 55 |
{%- set metric_inst = metric.metric_name | map_text("instrument", i.instrument_default(metric)) %} |
| 56 |
|
| 57 |
{{ h.metric_typedoc(metric, ctx.root_namespace) | comment | trim }} |
| 58 |
type {{ metric_name }} struct { |
| 59 |
metric.{{ metric_inst }} |
| 60 |
} |
| 61 |
|
| 62 |
var new{{ metric_name }}Opts = []metric.{{ metric_inst }}Option{ |
| 63 |
metric.WithDescription("{{ i.desc(metric) }}"), |
| 64 |
metric.WithUnit("{{metric.unit}}"), |
| 65 |
} |
| 66 |
|
| 67 |
{{ ["New" ~ metric_name ~ " returns a new " ~ metric_name ~ " instrument."] | comment }} |
| 68 |
func New{{ metric_name }}( |
| 69 |
m metric.Meter, |
| 70 |
opt ...metric.{{ metric_inst}}Option, |
| 71 |
) ({{ metric_name }}, error) { |
| 72 |
// Check if the meter is nil. |
| 73 |
if m == nil { |
| 74 |
return {{metric_name}}{noop.{{ metric_inst }}{}}, nil |
| 75 |
} |
| 76 |
|
| 77 |
if len(opt) == 0 { |
| 78 |
opt = new{{ metric_name }}Opts |
| 79 |
} else { |
| 80 |
opt = append(opt, new{{ metric_name }}Opts...) |
| 81 |
} |
| 82 |
|
| 83 |
i, err := m.{{ metric_inst }}( |
| 84 |
"{{metric.metric_name}}", |
| 85 |
opt..., |
| 86 |
) |
| 87 |
if err != nil { |
| 88 |
return {{metric_name}}{noop.{{ metric_inst }}{}}, err |
| 89 |
} |
| 90 |
return {{ metric_name }}{i}, nil |
| 91 |
} |
| 92 |
|
| 93 |
// Inst returns the underlying metric instrument. |
| 94 |
func (m {{ metric_name }}) Inst() metric.{{ metric_inst }} { |
| 95 |
return m.{{ metric_inst }} |
| 96 |
} |
| 97 |
|
| 98 |
// Name returns the semantic convention name of the instrument. |
| 99 |
func ({{ metric_name }}) Name() string { |
| 100 |
return "{{ metric.metric_name }}" |
| 101 |
} |
| 102 |
|
| 103 |
// Unit returns the semantic convention unit of the instrument |
| 104 |
func ({{ metric_name }}) Unit() string { |
| 105 |
return "{{ metric.unit }}" |
| 106 |
} |
| 107 |
{%- if metric.brief %} |
| 108 |
|
| 109 |
// Description returns the semantic convention description of the instrument |
| 110 |
func ({{ metric_name }}) Description() string { |
| 111 |
return "{{ i.desc(metric) }}" |
| 112 |
} |
| 113 |
{%- endif %} |
| 114 |
{%- if "Observable" is in metric_inst %} |
| 115 |
{%- elif metric.instrument == "counter" or metric.instrument == "updowncounter" %} |
| 116 |
|
| 117 |
{{ i.add_method(metric, metric_inst, ctx.root_namespace) }} |
| 118 |
|
| 119 |
{{ i.add_set_method(metric, metric_inst, ctx.root_namespace) }} |
| 120 |
{%- elif metric.instrument == "histogram" or metric.instrument == "gauge" %} |
| 121 |
|
| 122 |
{{ i.record_method(metric, metric_inst, ctx.root_namespace) }} |
| 123 |
|
| 124 |
{{ i.record_set_method(metric, metric_inst, ctx.root_namespace) }} |
| 125 |
{%- endif %} |
| 126 |
{%- for attr in metric.attributes | not_required | attribute_sort %} |
| 127 |
{%- set name = h.to_go_name(attr.name, ctx.root_namespace) %} |
| 128 |
|
| 129 |
{{ [ "Attr" ~ name ~ " returns an optional attribute for the \"" ~ attr.name ~ "\" semantic convention. " ~ h.it_reps(attr.brief) ] | comment }} |
| 130 |
{%- if attr.type is mapping %} |
| 131 |
func ({{ metric_name}}) Attr{{name}}(val {{ name }}Attr) attribute.KeyValue { |
| 132 |
return attribute.{{ h.attr_type(attr) | map_text("attribute_type_method")}}("{{ attr.name }}", {{ h.member_type(attr.type.members[0]) }}(val)) |
| 133 |
} |
| 134 |
{%- else %} |
| 135 |
func ({{ metric_name}}) Attr{{name}}(val {{ attr.type | map_text("attribute_type_value")}}) attribute.KeyValue { |
| 136 |
return attribute.{{ h.attr_type(attr) | map_text("attribute_type_method")}}("{{ attr.name }}", val) |
| 137 |
} |
| 138 |
{%- endif %} |
| 139 |
{%- endfor %} |
| 140 |
{%- endfor %} |
| 141 |
|