|
| 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, |
| 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 | + o := addOptPool.Get().(*[]metric.AddOption) |
| 119 | + defer func() { |
| 120 | + *o = (*o)[:0] |
| 121 | + addOptPool.Put(o) |
| 122 | + }() |
| 123 | + |
| 124 | + *o = append( |
| 125 | + *o, |
| 126 | +{{ with_attributes_opt(metric.attributes, pkg=pkg, prefix="\t\t") }} |
| 127 | + ) |
| 128 | + |
| 129 | + m.{{ inst }}.Add(ctx, incr, *o...) |
| 130 | +} |
| 131 | +{%- endmacro -%} |
| 132 | + |
| 133 | +{%- macro add_method(metric, inst, pkg="") -%} |
| 134 | + |
| 135 | +// Add adds incr to the existing count. |
| 136 | +{%- if metric.attributes | length > 0 %} |
| 137 | +{{ params_docs(metric.attributes, pkg=pkg) }} |
| 138 | +{%- if metric.note is defined %} |
| 139 | +// |
| 140 | +{{ metric.note | comment }} |
| 141 | +{%- endif %} |
| 142 | +{{ add_method_with_optional(metric, inst, pkg) }} |
| 143 | +{%- else %} |
| 144 | +{%- if metric.note is defined %} |
| 145 | +// |
| 146 | +{{ metric.note | comment }} |
| 147 | +{%- endif %} |
| 148 | +func (m {{ h.to_go_name(metric.metric_name, pkg) }}) Add(ctx context.Context, incr {{ value_type(metric) | lower }}, attrs ...attribute.KeyValue) { |
| 149 | + if len(attrs) == 0 { |
| 150 | + m.{{ inst }}.Add(ctx, incr) |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + o := addOptPool.Get().(*[]metric.AddOption) |
| 155 | + defer func() { |
| 156 | + *o = (*o)[:0] |
| 157 | + addOptPool.Put(o) |
| 158 | + }() |
| 159 | + |
| 160 | + *o = append(*o, metric.WithAttributes(attrs...)) |
| 161 | + m.{{ inst }}.Add(ctx, incr, *o...) |
| 162 | +} |
| 163 | +{%- endif -%} |
| 164 | +{%- endmacro -%} |
| 165 | + |
| 166 | +{%- macro record_method_with_optional(metric, inst, pkg="") -%} |
| 167 | +{%- set name = h.to_go_name(metric.metric_name, pkg) -%} |
| 168 | +{%- set req_attr = metric.attributes | required | attribute_sort -%} |
| 169 | +func (m {{ name }}) Record( |
| 170 | + ctx context.Context, |
| 171 | + val {{ value_type(metric) | lower }}, |
| 172 | +{{ params(metric.attributes, pkg=pkg, prefix="\t") }} |
| 173 | +) { |
| 174 | + o := recOptPool.Get().(*[]metric.RecordOption) |
| 175 | + defer func() { |
| 176 | + *o = (*o)[:0] |
| 177 | + recOptPool.Put(o) |
| 178 | + }() |
| 179 | + |
| 180 | + *o = append( |
| 181 | + *o, |
| 182 | +{{ with_attributes_opt(metric.attributes, pkg=pkg, prefix="\t\t") }} |
| 183 | + ) |
| 184 | + |
| 185 | + m.{{ inst }}.Record(ctx, val, *o...) |
| 186 | +} |
| 187 | +{%- endmacro -%} |
| 188 | + |
| 189 | +{%- macro record_method(metric, inst, pkg="") -%} |
| 190 | + |
| 191 | +// Record records val to the current distribution. |
| 192 | +{%- if metric.attributes | length > 0 %} |
| 193 | +{{ params_docs(metric.attributes, pkg=pkg) }} |
| 194 | +{%- if metric.note is defined %} |
| 195 | +// |
| 196 | +{{ metric.note | comment }} |
| 197 | +{%- endif %} |
| 198 | +{{ record_method_with_optional(metric, inst, pkg) }} |
| 199 | +{%- else %} |
| 200 | +{%- set name = h.to_go_name(metric.metric_name, pkg) -%} |
| 201 | +{%- if metric.note is defined %} |
| 202 | +// |
| 203 | +{{ metric.note | comment }} |
| 204 | +{%- endif %} |
| 205 | +func (m {{ name }}) Record(ctx context.Context, val {{ value_type(metric) | lower }}, attrs ...attribute.KeyValue) { |
| 206 | + if len(attrs) == 0 { |
| 207 | + m.{{ inst }}.Record(ctx, val) |
| 208 | + } |
| 209 | + |
| 210 | + o := recOptPool.Get().(*[]metric.RecordOption) |
| 211 | + defer func() { |
| 212 | + *o = (*o)[:0] |
| 213 | + recOptPool.Put(o) |
| 214 | + }() |
| 215 | + |
| 216 | + *o = append(*o, metric.WithAttributes(attrs...)) |
| 217 | + m.{{ inst }}.Record(ctx, val, *o...) |
| 218 | +} |
| 219 | +{%- endif -%} |
| 220 | +{%- endmacro -%} |
0 commit comments