forked from VictoriaMetrics/VictoriaMetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app/(vminsert|vmagent): add support for new relic infrastructure agent (
VictoriaMetrics#4712) Signed-off-by: hagen1778 <roman@victoriametrics.com> Co-authored-by: hagen1778 <roman@victoriametrics.com>
- Loading branch information
1 parent
cc7d5b7
commit f60c08a
Showing
13 changed files
with
1,086 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package newrelic | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/VictoriaMetrics/metrics" | ||
|
||
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/common" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/auth" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" | ||
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/newrelic" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/newrelic/stream" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/tenantmetrics" | ||
) | ||
|
||
var ( | ||
rowsInserted = metrics.NewCounter(`vmagent_rows_inserted_total{type="newrelic"}`) | ||
rowsTenantInserted = tenantmetrics.NewCounterMap(`vmagent_tenant_inserted_rows_total{type="newrelic"}`) | ||
rowsPerInsert = metrics.NewHistogram(`vmagent_rows_per_insert{type="newrelic"}`) | ||
) | ||
|
||
// InsertHandlerForHTTP processes remote write for NewRelic POST /infra/v2/metrics/events/bulk request. | ||
func InsertHandlerForHTTP(at *auth.Token, req *http.Request) error { | ||
extraLabels, err := parserCommon.GetExtraLabels(req) | ||
if err != nil { | ||
return err | ||
} | ||
ce := req.Header.Get("Content-Encoding") | ||
isGzip := ce == "gzip" | ||
return stream.Parse(req.Body, isGzip, func(series []newrelic.Metric) error { | ||
return insertRows(at, series, extraLabels) | ||
}) | ||
} | ||
|
||
func insertRows(at *auth.Token, rows []newrelic.Metric, extraLabels []prompbmarshal.Label) error { | ||
ctx := common.GetPushCtx() | ||
defer common.PutPushCtx(ctx) | ||
|
||
rowsTotal := 0 | ||
tssDst := ctx.WriteRequest.Timeseries[:0] | ||
labels := ctx.Labels[:0] | ||
samples := ctx.Samples[:0] | ||
for i := range rows { | ||
r := &rows[i] | ||
labelsLen := len(labels) | ||
labels = append(labels, prompbmarshal.Label{ | ||
Name: "__name__", | ||
Value: r.Metric, | ||
}) | ||
for j := range r.Tags { | ||
tag := &r.Tags[j] | ||
labels = append(labels, prompbmarshal.Label{ | ||
Name: tag.Key, | ||
Value: tag.Value, | ||
}) | ||
} | ||
samples = append(samples, prompbmarshal.Sample{ | ||
Value: r.Value, | ||
Timestamp: r.Timestamp, | ||
}) | ||
tssDst = append(tssDst, prompbmarshal.TimeSeries{ | ||
Labels: labels[labelsLen:], | ||
Samples: samples[len(samples)-1:], | ||
}) | ||
labels = append(labels, extraLabels...) | ||
} | ||
ctx.WriteRequest.Timeseries = tssDst | ||
ctx.Labels = labels | ||
ctx.Samples = samples | ||
remotewrite.Push(at, &ctx.WriteRequest) | ||
rowsInserted.Add(len(rows)) | ||
if at != nil { | ||
rowsTenantInserted.Get(at).Add(rowsTotal) | ||
} | ||
rowsPerInsert.Update(float64(len(rows))) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package newrelic | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/VictoriaMetrics/metrics" | ||
|
||
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/common" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/app/vminsert/relabel" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal" | ||
parserCommon "github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/newrelic" | ||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/newrelic/stream" | ||
) | ||
|
||
var ( | ||
rowsInserted = metrics.NewCounter(`vm_rows_inserted_total{type="newrelic"}`) | ||
rowsPerInsert = metrics.NewHistogram(`vm_rows_per_insert{type="newrelic"}`) | ||
) | ||
|
||
// InsertHandlerForHTTP processes remote write for NewRelic POST /infra/v2/metrics/events/bulk request. | ||
func InsertHandlerForHTTP(req *http.Request) error { | ||
extraLabels, err := parserCommon.GetExtraLabels(req) | ||
if err != nil { | ||
return err | ||
} | ||
ce := req.Header.Get("Content-Encoding") | ||
isGzip := ce == "gzip" | ||
return stream.Parse(req.Body, isGzip, func(series []newrelic.Metric) error { | ||
return insertRows(series, extraLabels) | ||
}) | ||
} | ||
|
||
func insertRows(rows []newrelic.Metric, extraLabels []prompbmarshal.Label) error { | ||
ctx := common.GetInsertCtx() | ||
defer common.PutInsertCtx(ctx) | ||
|
||
ctx.Reset(len(rows)) | ||
hasRelabeling := relabel.HasRelabeling() | ||
for i := range rows { | ||
r := &rows[i] | ||
ctx.Labels = ctx.Labels[:0] | ||
ctx.AddLabel("", r.Metric) | ||
for j := range r.Tags { | ||
tag := &r.Tags[j] | ||
ctx.AddLabel(tag.Key, tag.Value) | ||
} | ||
for j := range extraLabels { | ||
label := &extraLabels[j] | ||
ctx.AddLabel(label.Name, label.Value) | ||
} | ||
if hasRelabeling { | ||
ctx.ApplyRelabeling() | ||
} | ||
if len(ctx.Labels) == 0 { | ||
// Skip metric without labels. | ||
continue | ||
} | ||
ctx.SortLabelsIfNeeded() | ||
if err := ctx.WriteDataPoint(nil, ctx.Labels, r.Timestamp, r.Value); err != nil { | ||
return err | ||
} | ||
} | ||
rowsInserted.Add(len(rows)) | ||
rowsPerInsert.Update(float64(len(rows))) | ||
return ctx.FlushBufs() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.