influx fix

This commit is contained in:
2026-10-11 14:52:11 +02:00
parent a41d4e7af1
commit 963138efcf

View File

@@ -13,10 +13,10 @@ import (
)
type InfluxDBExporter struct {
url string // full write URL, e.g. "http://localhost:8086/write?db=telegraf"
token string // for v2/v3
user string // for v1 basic auth
pass string // for v1 basic auth
url string
token string
user string
pass string
client *http.Client
}
@@ -26,7 +26,7 @@ type InfluxDBExporter struct {
//
// v1: http://localhost:8086/write?db=nvllama&rp=autogen
// v2: http://localhost:8086/api/v2/write?org=myorg&bucket=nvllama
// v3: http://localhost:8086/api/v2/write?bucket=nvllama
// v3: http://localhost:8181/api/v3/write_lp?db=nvllama
func NewInfluxDBExporter(url, token, user, pass string) *InfluxDBExporter {
return &InfluxDBExporter{
url: url,
@@ -51,9 +51,9 @@ func (e *InfluxDBExporter) Write(ctx context.Context, metrics []core.Metric) err
}
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
// Auth: token (v2/v3) or basic (v1)
// Auth: Bearer token (v2/v3) or basic auth (v1)
if e.token != "" {
req.Header.Set("Authorization", "Token "+e.token)
req.Header.Set("Authorization", "Bearer "+e.token)
} else if e.user != "" {
req.SetBasicAuth(e.user, e.pass)
}
@@ -80,16 +80,11 @@ func (e *InfluxDBExporter) Write(ctx context.Context, metrics []core.Metric) err
// Each metric becomes one line:
//
// measurement,tag1=val1,tag2=val2 value=<float> <timestamp_ns>
//
// The metric Name is the measurement, Labels are tags, and Value is
// written as a single "value" field.
func EncodeLineProtocol(metrics []core.Metric) string {
if len(metrics) == 0 {
return ""
}
// Group by (measurement + tags) so we can batch fields if needed.
// For our use case each metric is one value, so one line per metric.
var b strings.Builder
b.Grow(len(metrics) * 128)
@@ -98,7 +93,7 @@ func EncodeLineProtocol(metrics []core.Metric) string {
b.WriteByte('\n')
}
// Measurement (escape commas and spaces)
// Measurement
b.WriteString(escapeMeasurement(m.Name))
// Tags (sorted for determinism)
@@ -132,7 +127,6 @@ func EncodeLineProtocol(metrics []core.Metric) string {
}
func escapeMeasurement(s string) string {
// Escape commas and spaces in measurement names
if strings.ContainsAny(s, ", ") {
var b strings.Builder
for _, c := range s {
@@ -165,8 +159,6 @@ func escapeTagValue(s string) string {
}
func formatFloat(f float64) string {
// Write as integer if it's a whole number (InfluxDB treats unquoted
// integers as i64, which is more storage-efficient)
if f == float64(int64(f)) && f < 9.223372036854775807e18 && f > -9.223372036854775808e18 {
return fmt.Sprintf("%di", int64(f))
}