2019-05-22 21:16:55 +00:00
|
|
|
{% import (
|
2020-10-12 17:01:51 +00:00
|
|
|
"bytes"
|
2022-04-29 09:49:47 +00:00
|
|
|
"math"
|
2020-10-12 17:01:51 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/valyala/quicktemplate"
|
2022-05-31 23:29:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/querytracer"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
) %}
|
|
|
|
|
|
|
|
{% stripspace %}
|
|
|
|
|
2020-10-12 17:01:51 +00:00
|
|
|
{% func ExportCSVLine(xb *exportBlock, fieldNames []string) %}
|
|
|
|
{% if len(xb.timestamps) == 0 || len(fieldNames) == 0 %}{% return %}{% endif %}
|
|
|
|
{% for i, timestamp := range xb.timestamps %}
|
|
|
|
{% code value := xb.values[i] %}
|
|
|
|
{%= exportCSVField(xb.mn, fieldNames[0], timestamp, value) %}
|
|
|
|
{% for _, fieldName := range fieldNames[1:] %}
|
|
|
|
,
|
|
|
|
{%= exportCSVField(xb.mn, fieldName, timestamp, value) %}
|
|
|
|
{% endfor %}
|
|
|
|
{% newline %}
|
|
|
|
{% endfor %}
|
|
|
|
{% endfunc %}
|
|
|
|
|
|
|
|
{% func exportCSVField(mn *storage.MetricName, fieldName string, timestamp int64, value float64) %}
|
|
|
|
{% if fieldName == "__value__" %}
|
|
|
|
{%f= value %}
|
|
|
|
{% return %}
|
|
|
|
{% endif %}
|
|
|
|
{% if fieldName == "__timestamp__" %}
|
|
|
|
{%dl timestamp %}
|
|
|
|
{% return %}
|
|
|
|
{% endif %}
|
|
|
|
{% if strings.HasPrefix(fieldName, "__timestamp__:") %}
|
|
|
|
{% code timeFormat := fieldName[len("__timestamp__:"):] %}
|
|
|
|
{% switch timeFormat %}
|
|
|
|
{% case "unix_s" %}
|
|
|
|
{%dl= timestamp/1000 %}
|
|
|
|
{% case "unix_ms" %}
|
|
|
|
{%dl= timestamp %}
|
|
|
|
{% case "unix_ns" %}
|
|
|
|
{%dl= timestamp*1e6 %}
|
|
|
|
{% case "rfc3339" %}
|
|
|
|
{% code
|
|
|
|
bb := quicktemplate.AcquireByteBuffer()
|
|
|
|
bb.B = time.Unix(timestamp/1000, (timestamp%1000)*1e6).AppendFormat(bb.B[:0], time.RFC3339)
|
|
|
|
%}
|
|
|
|
{%z= bb.B %}
|
|
|
|
{% code
|
|
|
|
quicktemplate.ReleaseByteBuffer(bb)
|
|
|
|
%}
|
|
|
|
{% default %}
|
|
|
|
{% if strings.HasPrefix(timeFormat, "custom:") %}
|
|
|
|
{% code
|
|
|
|
layout := timeFormat[len("custom:"):]
|
|
|
|
bb := quicktemplate.AcquireByteBuffer()
|
|
|
|
bb.B = time.Unix(timestamp/1000, (timestamp%1000)*1e6).AppendFormat(bb.B[:0], layout)
|
|
|
|
%}
|
|
|
|
{% if bytes.ContainsAny(bb.B, `"`+",\n") %}
|
|
|
|
{%qz bb.B %}
|
|
|
|
{% else %}
|
|
|
|
{%z= bb.B %}
|
|
|
|
{% endif %}
|
|
|
|
{% code
|
|
|
|
quicktemplate.ReleaseByteBuffer(bb)
|
|
|
|
%}
|
|
|
|
{% else %}
|
|
|
|
Unsupported timeFormat={%s= timeFormat %}
|
|
|
|
{% endif %}
|
|
|
|
{% endswitch %}
|
|
|
|
{% return %}
|
|
|
|
{% endif %}
|
|
|
|
{% code v := mn.GetTagValue(fieldName) %}
|
|
|
|
{% if bytes.ContainsAny(v, `"`+",\n") %}
|
|
|
|
{%qz= v %}
|
|
|
|
{% else %}
|
|
|
|
{%z= v %}
|
|
|
|
{% endif %}
|
|
|
|
{% endfunc %}
|
|
|
|
|
2020-09-26 01:29:45 +00:00
|
|
|
{% func ExportPrometheusLine(xb *exportBlock) %}
|
|
|
|
{% if len(xb.timestamps) == 0 %}{% return %}{% endif %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% code bb := quicktemplate.AcquireByteBuffer() %}
|
2020-09-26 01:29:45 +00:00
|
|
|
{% code writeprometheusMetricName(bb, xb.mn) %}
|
|
|
|
{% for i, ts := range xb.timestamps %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{%z= bb.B %}{% space %}
|
2020-09-26 01:29:45 +00:00
|
|
|
{%f= xb.values[i] %}{% space %}
|
2019-10-17 15:22:56 +00:00
|
|
|
{%dl= ts %}{% newline %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% endfor %}
|
|
|
|
{% code quicktemplate.ReleaseByteBuffer(bb) %}
|
|
|
|
{% endfunc %}
|
|
|
|
|
2020-09-26 01:29:45 +00:00
|
|
|
{% func ExportJSONLine(xb *exportBlock) %}
|
|
|
|
{% if len(xb.timestamps) == 0 %}{% return %}{% endif %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{
|
2020-09-26 01:29:45 +00:00
|
|
|
"metric":{%= metricNameObject(xb.mn) %},
|
2019-05-22 21:16:55 +00:00
|
|
|
"values":[
|
2020-09-26 01:29:45 +00:00
|
|
|
{% if len(xb.values) > 0 %}
|
|
|
|
{% code values := xb.values %}
|
2022-10-06 10:54:20 +00:00
|
|
|
{%= convertValueToSpecialJSON(values[0]) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% code values = values[1:] %}
|
|
|
|
{% for _, v := range values %}
|
2022-10-06 10:54:20 +00:00
|
|
|
,{%= convertValueToSpecialJSON(v) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% endfor %}
|
|
|
|
{% endif %}
|
|
|
|
],
|
|
|
|
"timestamps":[
|
2020-09-26 01:29:45 +00:00
|
|
|
{% if len(xb.timestamps) > 0 %}
|
|
|
|
{% code timestamps := xb.timestamps %}
|
2019-10-17 15:22:56 +00:00
|
|
|
{%dl= timestamps[0] %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% code timestamps = timestamps[1:] %}
|
|
|
|
{% for _, ts := range timestamps %}
|
2019-10-17 15:22:56 +00:00
|
|
|
,{%dl= ts %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% endfor %}
|
|
|
|
{% endif %}
|
|
|
|
]
|
|
|
|
}{% newline %}
|
|
|
|
{% endfunc %}
|
|
|
|
|
2020-09-26 01:29:45 +00:00
|
|
|
{% func ExportPromAPILine(xb *exportBlock) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{
|
2020-09-26 01:29:45 +00:00
|
|
|
"metric": {%= metricNameObject(xb.mn) %},
|
|
|
|
"values": {%= valuesWithTimestamps(xb.values, xb.timestamps) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
{% endfunc %}
|
|
|
|
|
2022-10-01 19:05:43 +00:00
|
|
|
{% func ExportPromAPIHeader() %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{
|
|
|
|
"status":"success",
|
|
|
|
"data":{
|
|
|
|
"resultType":"matrix",
|
|
|
|
"result":[
|
2022-10-01 19:05:43 +00:00
|
|
|
{% endfunc %}
|
|
|
|
|
|
|
|
{% func ExportPromAPIFooter(qt *querytracer.Tracer) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
]
|
|
|
|
}
|
2022-05-31 23:29:19 +00:00
|
|
|
{% code
|
2022-10-01 19:05:43 +00:00
|
|
|
qt.Donef("export format=promapi")
|
2022-05-31 23:29:19 +00:00
|
|
|
%}
|
|
|
|
{%= dumpQueryTrace(qt) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
{% endfunc %}
|
|
|
|
|
|
|
|
{% func prometheusMetricName(mn *storage.MetricName) %}
|
|
|
|
{%z= mn.MetricGroup %}
|
|
|
|
{% if len(mn.Tags) > 0 %}
|
|
|
|
{
|
|
|
|
{% code tags := mn.Tags %}
|
2023-12-07 13:26:57 +00:00
|
|
|
{%z= tags[0].Key %}={%= escapePrometheusLabel(tags[0].Value) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% code tags = tags[1:] %}
|
|
|
|
{% for i := range tags %}
|
|
|
|
{% code tag := &tags[i] %}
|
2023-12-07 13:26:57 +00:00
|
|
|
,{%z= tag.Key %}={%= escapePrometheusLabel(tag.Value) %}
|
2019-05-22 21:16:55 +00:00
|
|
|
{% endfor %}
|
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
{% endfunc %}
|
2022-10-06 10:54:20 +00:00
|
|
|
|
|
|
|
{% func convertValueToSpecialJSON(v float64) %}
|
|
|
|
{% if math.IsNaN(v) %}
|
|
|
|
null
|
2022-10-06 11:50:07 +00:00
|
|
|
{% elseif math.IsInf(v, 0) %}
|
|
|
|
{% if v > 0 %}
|
|
|
|
"Infinity"
|
|
|
|
{% else %}
|
|
|
|
"-Infinity"
|
|
|
|
{% endif %}
|
2022-10-06 10:54:20 +00:00
|
|
|
{% else %}
|
|
|
|
{%f= v %}
|
|
|
|
{% endif %}
|
|
|
|
{% endfunc %}
|
|
|
|
|
2023-12-07 13:26:57 +00:00
|
|
|
{% func escapePrometheusLabel(b []byte) %}
|
|
|
|
"
|
|
|
|
{% for len(b) > 0 %}
|
|
|
|
{% code n := bytes.IndexAny(b, "\\\n\"") %}
|
|
|
|
{% if n < 0 %}
|
|
|
|
{%z= b %}
|
|
|
|
{% break %}
|
|
|
|
{% endif %}
|
|
|
|
{%z= b[:n] %}
|
|
|
|
{% switch b[n] %}
|
|
|
|
{% case '\\' %}
|
|
|
|
\\
|
|
|
|
{% case '\n' %}
|
|
|
|
\n
|
|
|
|
{% case '"' %}
|
|
|
|
\"
|
|
|
|
{% endswitch %}
|
|
|
|
{% code b = b[n+1:] %}
|
|
|
|
{% endfor %}
|
|
|
|
"
|
|
|
|
{% endfunc %}
|
|
|
|
|
2022-10-06 11:50:07 +00:00
|
|
|
{% endstripspace %}
|