mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-26 20:30:10 +01:00
22762d7a69
* app/vmselect: adds milliseconds to the csv export response for rfc3339 * milliseconds is a standard prescion for VictoriaMetrics query request responses https://github.com/VictoriaMetrics/VictoriaMetrics/issues/5837 * app/victoria-metrics: adds tests for csv export/import follow-up after 3541a8d0cf96dd4f8563624c4aab6816615d0756 --------- Signed-off-by: hagen1778 <roman@victoriametrics.com> Co-authored-by: hagen1778 <roman@victoriametrics.com>
200 lines
4.7 KiB
Plaintext
200 lines
4.7 KiB
Plaintext
{% import (
|
|
"bytes"
|
|
"math"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/valyala/quicktemplate"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/querytracer"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
) %}
|
|
|
|
{% stripspace %}
|
|
|
|
{% 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 %}
|
|
|
|
{%code const rfc3339Milli = "2006-01-02T15:04:05.999Z07:00" %}
|
|
{% 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], rfc3339Milli)
|
|
%}
|
|
{%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 %}
|
|
|
|
{% func ExportPrometheusLine(xb *exportBlock) %}
|
|
{% if len(xb.timestamps) == 0 %}{% return %}{% endif %}
|
|
{% code bb := quicktemplate.AcquireByteBuffer() %}
|
|
{% code writeprometheusMetricName(bb, xb.mn) %}
|
|
{% for i, ts := range xb.timestamps %}
|
|
{%z= bb.B %}{% space %}
|
|
{%f= xb.values[i] %}{% space %}
|
|
{%dl= ts %}{% newline %}
|
|
{% endfor %}
|
|
{% code quicktemplate.ReleaseByteBuffer(bb) %}
|
|
{% endfunc %}
|
|
|
|
{% func ExportJSONLine(xb *exportBlock) %}
|
|
{% if len(xb.timestamps) == 0 %}{% return %}{% endif %}
|
|
{
|
|
"metric":{%= metricNameObject(xb.mn) %},
|
|
"values":[
|
|
{% if len(xb.values) > 0 %}
|
|
{% code values := xb.values %}
|
|
{%= convertValueToSpecialJSON(values[0]) %}
|
|
{% code values = values[1:] %}
|
|
{% for _, v := range values %}
|
|
,{%= convertValueToSpecialJSON(v) %}
|
|
{% endfor %}
|
|
{% endif %}
|
|
],
|
|
"timestamps":[
|
|
{% if len(xb.timestamps) > 0 %}
|
|
{% code timestamps := xb.timestamps %}
|
|
{%dl= timestamps[0] %}
|
|
{% code timestamps = timestamps[1:] %}
|
|
{% for _, ts := range timestamps %}
|
|
,{%dl= ts %}
|
|
{% endfor %}
|
|
{% endif %}
|
|
]
|
|
}{% newline %}
|
|
{% endfunc %}
|
|
|
|
{% func ExportPromAPILine(xb *exportBlock) %}
|
|
{
|
|
"metric": {%= metricNameObject(xb.mn) %},
|
|
"values": {%= valuesWithTimestamps(xb.values, xb.timestamps) %}
|
|
}
|
|
{% endfunc %}
|
|
|
|
{% func ExportPromAPIHeader() %}
|
|
{
|
|
"status":"success",
|
|
"data":{
|
|
"resultType":"matrix",
|
|
"result":[
|
|
{% endfunc %}
|
|
|
|
{% func ExportPromAPIFooter(qt *querytracer.Tracer) %}
|
|
]
|
|
}
|
|
{% code
|
|
qt.Donef("export format=promapi")
|
|
%}
|
|
{%= dumpQueryTrace(qt) %}
|
|
}
|
|
{% endfunc %}
|
|
|
|
{% func prometheusMetricName(mn *storage.MetricName) %}
|
|
{%z= mn.MetricGroup %}
|
|
{% if len(mn.Tags) > 0 %}
|
|
{
|
|
{% code tags := mn.Tags %}
|
|
{%z= tags[0].Key %}={%= escapePrometheusLabel(tags[0].Value) %}
|
|
{% code tags = tags[1:] %}
|
|
{% for i := range tags %}
|
|
{% code tag := &tags[i] %}
|
|
,{%z= tag.Key %}={%= escapePrometheusLabel(tag.Value) %}
|
|
{% endfor %}
|
|
}
|
|
{% endif %}
|
|
{% endfunc %}
|
|
|
|
{% func convertValueToSpecialJSON(v float64) %}
|
|
{% if math.IsNaN(v) %}
|
|
null
|
|
{% elseif math.IsInf(v, 0) %}
|
|
{% if v > 0 %}
|
|
"Infinity"
|
|
{% else %}
|
|
"-Infinity"
|
|
{% endif %}
|
|
{% else %}
|
|
{%f= v %}
|
|
{% endif %}
|
|
{% endfunc %}
|
|
|
|
{% 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 %}
|
|
|
|
{% endstripspace %}
|