| 1 |
{% import 'helpers.j2' as h -%} |
| 2 |
|
| 3 |
{%- macro instrument_default(metric) -%} |
| 4 |
{%- set ns = namespace(value="Int64", inst="unknown") -%} |
| 5 |
{%- set divisible = ["s", "ms"] -%} |
| 6 |
{%- if metric.unit is in divisible -%} |
| 7 |
{%- set ns.value="Float64" -%} |
| 8 |
{%- endif -%} |
| 9 |
{%- if metric.instrument == "counter" -%} |
| 10 |
{%- set ns.inst="Counter" -%} |
| 11 |
{%- elif metric.instrument == "updowncounter" -%} |
| 12 |
{%- set ns.inst="UpDownCounter" -%} |
| 13 |
{%- elif metric.instrument == "gauge" -%} |
| 14 |
{%- set ns.inst="Gauge" -%} |
| 15 |
{%- elif metric.instrument == "histogram" -%} |
| 16 |
{%- set ns.inst="Histogram" -%} |
| 17 |
{%- endif -%} |
| 18 |
{{ ns.value ~ ns.inst }} |
| 19 |
{%- endmacro -%} |
| 20 |
|
| 21 |
{%- macro instrument(metric) -%} |
| 22 |
{{ metric.metric_name | map_text("instrument", instrument_default(metric)) }} |
| 23 |
{%- endmacro -%} |
| 24 |
|
| 25 |
{%- macro value_type(metric) -%} |
| 26 |
{%- if instrument(metric)[:7] == "Float64" -%} |
| 27 |
Float64 |
| 28 |
{%- else -%} |
| 29 |
Int64 |
| 30 |
{%- endif -%} |
| 31 |
{%- endmacro -%} |
| 32 |
|
| 33 |
{%- macro param_name(raw="", pkg="") -%} |
| 34 |
{%- set reserved = [ |
| 35 |
"type", "break", "default", "func", "interface", "select", "case", "defer", |
| 36 |
"go", "map", "struct", "chan", "else", "goto", "const", "fallthrough", "if", |
| 37 |
"range", "type", "continue", "for", "import", "return", "var", |
| 38 |
]-%} |
| 39 |
{%- set name = raw -%} |
| 40 |
{%- if pkg != "" -%} |
| 41 |
{%- set n = pkg | length -%} |
| 42 |
{%- if pkg == name[:n] -%} |
| 43 |
{%- set name = name[n:] -%} |
| 44 |
{%- if name[0] == "." or name[0] == "." -%} |
| 45 |
{%- set name = name[1:] -%} |
| 46 |
{%- endif -%} |
| 47 |
{%- endif -%} |
| 48 |
{%- endif -%} |
| 49 |
{%- if name | lower is in reserved -%} |
| 50 |
{{ raw | camel_case }} |
| 51 |
{%- else -%} |
| 52 |
{{ name | camel_case }} |
| 53 |
{%- endif -%} |
| 54 |
{%- endmacro -%} |
| 55 |
|
| 56 |
{%- macro params_docs(attrs, pkg="") -%} |
| 57 |
{%- set ns = namespace(output='') -%} |
| 58 |
{%- for attr in attrs | required | attribute_sort -%} |
| 59 |
{%- set ns.output = ns.output ~ "\n\nThe " ~ param_name(attr.name, pkg) ~ " is the " ~ h.lower_first(attr.brief) -%} |
| 60 |
{%- endfor -%} |
| 61 |
{%- if attrs | not_required | length > 0 -%} |
| 62 |
{%- set ns.output = ns.output ~ "\n\nAll additional attrs passed are included in the recorded value." -%} |
| 63 |
{%- endif -%} |
| 64 |
{%- if ns.output != "" -%} |
| 65 |
// |
| 66 |
{{ ns.output | comment }} |
| 67 |
{%- endif -%} |
| 68 |
{%- endmacro -%} |
| 69 |
|
| 70 |
{%- macro param(attr, pkg="") -%} |
| 71 |
{%- if attr.type is mapping -%} |
| 72 |
{{ param_name(attr.name, pkg) }} {{ h.to_go_name(attr.name, pkg) }}Attr, |
| 73 |
{%- else -%} |
| 74 |
{{ param_name(attr.name, pkg) }} {{ attr.type | map_text("attribute_type_value")}}, |
| 75 |
{%- endif -%} |
| 76 |
{%- endmacro -%} |
| 77 |
|
| 78 |
{%- macro params(attrs, type="", pkg="", prefix="") -%} |
| 79 |
{%- for attr in attrs | required | attribute_sort -%} |
| 80 |
{{ prefix ~ param(attr, pkg) }} |
| 81 |
{% endfor -%} |
| 82 |
{{ prefix ~ "attrs ...attribute.KeyValue," }} |
| 83 |
{%- endmacro -%} |
| 84 |
|
| 85 |
{%- macro to_attribute(attr, pkg="") -%} |
| 86 |
{%- if attr.type is mapping -%} |
| 87 |
attribute.{{ h.attr_type(attr) | map_text("attribute_type_method")}}("{{ attr.name }}", {{ h.member_type(attr.type.members[0]) }}({{ param_name(attr.name, pkg) }})), |
| 88 |
{%- else -%} |
| 89 |
attribute.{{ attr.type | map_text("attribute_type_method")}}("{{ attr.name }}", {{ param_name(attr.name, pkg) }}), |
| 90 |
{%- endif -%} |
| 91 |
{%- endmacro -%} |
| 92 |
|
| 93 |
{%- macro with_attributes_opt(attrs, pkg="", prefix="") -%} |
| 94 |
{%- if attrs | length > 0 -%} |
| 95 |
{{ prefix }}metric.WithAttributes( |
| 96 |
{%- if attrs | required | length > 0 %} |
| 97 |
{{ prefix }} append( |
| 98 |
{{ prefix }} attrs[:len(attrs):len(attrs)], |
| 99 |
{%- for attr in attrs | required | attribute_sort %} |
| 100 |
{{ prefix }} {{ to_attribute(attr, pkg) }} |
| 101 |
{%- endfor %} |
| 102 |
{{ prefix }} )..., |
| 103 |
{%- else %} |
| 104 |
{{ prefix }} attrs..., |
| 105 |
{%- endif %} |
| 106 |
{{ prefix }}), |
| 107 |
{%- endif -%} |
| 108 |
{%- endmacro -%} |
| 109 |
|
| 110 |
{%- macro add_method_with_optional(metric, inst, pkg="") -%} |
| 111 |
{%- set name = h.to_go_name(metric.metric_name, pkg) -%} |
| 112 |
{%- set req_attr = metric.attributes | required | attribute_sort -%} |
| 113 |
func (m {{ name }}) Add( |
| 114 |
ctx context.Context, |
| 115 |
incr {{ value_type(metric) | lower }}, |
| 116 |
{{ params(metric.attributes, pkg=pkg, prefix="\t") }} |
| 117 |
) { |
| 118 |
if len(attrs) == 0 { |
| 119 |
{%- if req_attr | length > 0 %} |
| 120 |
m.{{ inst }}.Add(ctx, incr, metric.WithAttributes( |
| 121 |
{%- for attr in req_attr %} |
| 122 |
{{ to_attribute(attr, pkg) }} |
| 123 |
{%- endfor %} |
| 124 |
)) |
| 125 |
{%- else %} |
| 126 |
m.{{ inst }}.Add(ctx, incr) |
| 127 |
{%- endif %} |
| 128 |
return |
| 129 |
} |
| 130 |
|
| 131 |
o := addOptPool.Get().(*[]metric.AddOption) |
| 132 |
defer func() { |
| 133 |
*o = (*o)[:0] |
| 134 |
addOptPool.Put(o) |
| 135 |
}() |
| 136 |
|
| 137 |
*o = append( |
| 138 |
*o, |
| 139 |
{{ with_attributes_opt(metric.attributes, pkg=pkg, prefix="\t\t") }} |
| 140 |
) |
| 141 |
|
| 142 |
m.{{ inst }}.Add(ctx, incr, *o...) |
| 143 |
} |
| 144 |
{%- endmacro -%} |
| 145 |
|
| 146 |
{%- macro add_method(metric, inst, pkg="") -%} |
| 147 |
|
| 148 |
// Add adds incr to the existing count for attrs. |
| 149 |
{%- if metric.attributes is not none and metric.attributes | length > 0 %} |
| 150 |
{{ params_docs(metric.attributes, pkg=pkg) }} |
| 151 |
{%- if metric.note is defined %} |
| 152 |
// |
| 153 |
{{ metric.note | comment }} |
| 154 |
{%- endif %} |
| 155 |
{{ add_method_with_optional(metric, inst, pkg) }} |
| 156 |
{%- else %} |
| 157 |
{%- if metric.note is defined %} |
| 158 |
// |
| 159 |
{{ metric.note | comment }} |
| 160 |
{%- endif %} |
| 161 |
func (m {{ h.to_go_name(metric.metric_name, pkg) }}) Add(ctx context.Context, incr {{ value_type(metric) | lower }}, attrs ...attribute.KeyValue) { |
| 162 |
if len(attrs) == 0 { |
| 163 |
m.{{ inst }}.Add(ctx, incr) |
| 164 |
return |
| 165 |
} |
| 166 |
|
| 167 |
o := addOptPool.Get().(*[]metric.AddOption) |
| 168 |
defer func() { |
| 169 |
*o = (*o)[:0] |
| 170 |
addOptPool.Put(o) |
| 171 |
}() |
| 172 |
|
| 173 |
*o = append(*o, metric.WithAttributes(attrs...)) |
| 174 |
m.{{ inst }}.Add(ctx, incr, *o...) |
| 175 |
} |
| 176 |
{%- endif -%} |
| 177 |
{%- endmacro -%} |
| 178 |
|
| 179 |
{%- macro add_set_method(metric, inst, pkg="") -%} |
| 180 |
|
| 181 |
// AddSet adds incr to the existing count for set. |
| 182 |
{%- if metric.note is defined %} |
| 183 |
// |
| 184 |
{{ metric.note | comment }} |
| 185 |
{%- endif %} |
| 186 |
func (m {{ h.to_go_name(metric.metric_name, pkg) }}) AddSet(ctx context.Context, incr {{ value_type(metric) | lower }}, set attribute.Set) { |
| 187 |
if set.Len() == 0 { |
| 188 |
m.{{ inst }}.Add(ctx, incr) |
| 189 |
return |
| 190 |
} |
| 191 |
|
| 192 |
o := addOptPool.Get().(*[]metric.AddOption) |
| 193 |
defer func() { |
| 194 |
*o = (*o)[:0] |
| 195 |
addOptPool.Put(o) |
| 196 |
}() |
| 197 |
|
| 198 |
*o = append(*o, metric.WithAttributeSet(set)) |
| 199 |
m.{{ inst }}.Add(ctx, incr, *o...) |
| 200 |
} |
| 201 |
{%- endmacro -%} |
| 202 |
|
| 203 |
{%- macro record_method_with_optional(metric, inst, pkg="") -%} |
| 204 |
{%- set name = h.to_go_name(metric.metric_name, pkg) -%} |
| 205 |
{%- set req_attr = metric.attributes | required | attribute_sort -%} |
| 206 |
func (m {{ name }}) Record( |
| 207 |
ctx context.Context, |
| 208 |
val {{ value_type(metric) | lower }}, |
| 209 |
{{ params(metric.attributes, pkg=pkg, prefix="\t") }} |
| 210 |
) { |
| 211 |
if len(attrs) == 0 { |
| 212 |
{%- if req_attr | length > 0 %} |
| 213 |
m.{{ inst }}.Record(ctx, val, metric.WithAttributes( |
| 214 |
{%- for attr in req_attr %} |
| 215 |
{{ to_attribute(attr, pkg) }} |
| 216 |
{%- endfor %} |
| 217 |
)) |
| 218 |
{%- else %} |
| 219 |
m.{{ inst }}.Record(ctx, val) |
| 220 |
{%- endif %} |
| 221 |
return |
| 222 |
} |
| 223 |
|
| 224 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 225 |
defer func() { |
| 226 |
*o = (*o)[:0] |
| 227 |
recOptPool.Put(o) |
| 228 |
}() |
| 229 |
|
| 230 |
*o = append( |
| 231 |
*o, |
| 232 |
{{ with_attributes_opt(metric.attributes, pkg=pkg, prefix="\t\t") }} |
| 233 |
) |
| 234 |
|
| 235 |
m.{{ inst }}.Record(ctx, val, *o...) |
| 236 |
} |
| 237 |
{%- endmacro -%} |
| 238 |
|
| 239 |
{%- macro record_method(metric, inst, pkg="") -%} |
| 240 |
|
| 241 |
// Record records val to the current distribution for attrs. |
| 242 |
{%- if metric.attributes is not none and metric.attributes | length > 0 %} |
| 243 |
{{ params_docs(metric.attributes, pkg=pkg) }} |
| 244 |
{%- if metric.note is defined %} |
| 245 |
// |
| 246 |
{{ metric.note | comment }} |
| 247 |
{%- endif %} |
| 248 |
{{ record_method_with_optional(metric, inst, pkg) }} |
| 249 |
{%- else %} |
| 250 |
{%- set name = h.to_go_name(metric.metric_name, pkg) -%} |
| 251 |
{%- if metric.note is defined %} |
| 252 |
// |
| 253 |
{{ metric.note | comment }} |
| 254 |
{%- endif %} |
| 255 |
func (m {{ name }}) Record(ctx context.Context, val {{ value_type(metric) | lower }}, attrs ...attribute.KeyValue) { |
| 256 |
if len(attrs) == 0 { |
| 257 |
m.{{ inst }}.Record(ctx, val) |
| 258 |
return |
| 259 |
} |
| 260 |
|
| 261 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 262 |
defer func() { |
| 263 |
*o = (*o)[:0] |
| 264 |
recOptPool.Put(o) |
| 265 |
}() |
| 266 |
|
| 267 |
*o = append(*o, metric.WithAttributes(attrs...)) |
| 268 |
m.{{ inst }}.Record(ctx, val, *o...) |
| 269 |
} |
| 270 |
{%- endif -%} |
| 271 |
{%- endmacro -%} |
| 272 |
|
| 273 |
{%- macro record_set_method(metric, inst, pkg="") -%} |
| 274 |
{%- set name = h.to_go_name(metric.metric_name, pkg) -%} |
| 275 |
|
| 276 |
// RecordSet records val to the current distribution for set. |
| 277 |
{%- if metric.note is defined %} |
| 278 |
// |
| 279 |
{{ metric.note | comment }} |
| 280 |
{%- endif %} |
| 281 |
func (m {{ name }}) RecordSet(ctx context.Context, val {{ value_type(metric) | lower }}, set attribute.Set) { |
| 282 |
if set.Len() == 0 { |
| 283 |
m.{{ inst }}.Record(ctx, val) |
| 284 |
return |
| 285 |
} |
| 286 |
|
| 287 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 288 |
defer func() { |
| 289 |
*o = (*o)[:0] |
| 290 |
recOptPool.Put(o) |
| 291 |
}() |
| 292 |
|
| 293 |
*o = append(*o, metric.WithAttributeSet(set)) |
| 294 |
m.{{ inst }}.Record(ctx, val, *o...) |
| 295 |
} |
| 296 |
{%- endmacro -%} |
| 297 |
|
| 298 |
{%- macro desc(metric) -%} |
| 299 |
{{metric.brief | replace('\n', ' ') | trim}} |
| 300 |
{%- endmacro -%} |