2020-02-23 11:35:47 +00:00
package remotewrite
import (
"flag"
2020-05-30 11:36:40 +00:00
"fmt"
2023-12-04 23:20:44 +00:00
"strconv"
2020-02-23 11:35:47 +00:00
"strings"
"sync"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/prompbmarshal"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promrelabel"
)
var (
2022-10-01 15:26:05 +00:00
unparsedLabelsGlobal = flagutil . NewArrayString ( "remoteWrite.label" , "Optional label in the form 'name=value' to add to all the metrics before sending them to -remoteWrite.url. " +
2021-03-25 15:54:55 +00:00
"Pass multiple -remoteWrite.label flags in order to add multiple labels to metrics before sending them to remote storage" )
2023-01-04 06:04:46 +00:00
relabelConfigPathGlobal = flag . String ( "remoteWrite.relabelConfig" , "" , "Optional path to file with relabeling configs, which are applied " +
"to all the metrics before sending them to -remoteWrite.url. See also -remoteWrite.urlRelabelConfig. " +
"The path can point either to local file or to http url. " +
2024-04-17 23:31:37 +00:00
"See https://docs.victoriametrics.com/vmagent/#relabeling" )
2023-01-04 06:04:46 +00:00
relabelConfigPaths = flagutil . NewArrayString ( "remoteWrite.urlRelabelConfig" , "Optional path to relabel configs for the corresponding -remoteWrite.url. " +
"See also -remoteWrite.relabelConfig. The path can point either to local file or to http url. " +
2024-04-17 23:31:37 +00:00
"See https://docs.victoriametrics.com/vmagent/#relabeling" )
2022-09-26 10:11:37 +00:00
usePromCompatibleNaming = flag . Bool ( "usePromCompatibleNaming" , false , "Whether to replace characters unsupported by Prometheus with underscores " +
"in the ingested metric names and label names. For example, foo.bar{a.b='c'} is transformed into foo_bar{a_b='c'} during data ingestion if this flag is set. " +
"See https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels" )
2020-02-23 11:35:47 +00:00
)
2020-03-03 11:08:17 +00:00
var labelsGlobal [ ] prompbmarshal . Label
2020-02-23 11:35:47 +00:00
2020-05-30 11:36:40 +00:00
// CheckRelabelConfigs checks -remoteWrite.relabelConfig and -remoteWrite.urlRelabelConfig.
func CheckRelabelConfigs ( ) error {
_ , err := loadRelabelConfigs ( )
return err
}
func loadRelabelConfigs ( ) ( * relabelConfigs , error ) {
var rcs relabelConfigs
if * relabelConfigPathGlobal != "" {
2022-12-10 10:09:21 +00:00
global , err := promrelabel . LoadRelabelConfigs ( * relabelConfigPathGlobal )
2020-05-30 11:36:40 +00:00
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot load -remoteWrite.relabelConfig=%q: %w" , * relabelConfigPathGlobal , err )
2020-05-30 11:36:40 +00:00
}
rcs . global = global
}
2024-05-13 13:22:37 +00:00
if len ( * relabelConfigPaths ) > len ( * remoteWriteURLs ) {
return nil , fmt . Errorf ( "too many -remoteWrite.urlRelabelConfig args: %d; it mustn't exceed the number of -remoteWrite.url args: %d" ,
len ( * relabelConfigPaths ) , ( len ( * remoteWriteURLs ) ) )
2020-05-30 11:36:40 +00:00
}
2024-05-13 13:22:37 +00:00
rcs . perURL = make ( [ ] * promrelabel . ParsedConfigs , len ( * remoteWriteURLs ) )
2020-05-30 11:36:40 +00:00
for i , path := range * relabelConfigPaths {
2020-07-20 12:49:05 +00:00
if len ( path ) == 0 {
// Skip empty relabel config.
continue
}
2022-12-10 10:09:21 +00:00
prc , err := promrelabel . LoadRelabelConfigs ( path )
2020-05-30 11:36:40 +00:00
if err != nil {
2020-06-30 19:58:18 +00:00
return nil , fmt . Errorf ( "cannot load relabel configs from -remoteWrite.urlRelabelConfig=%q: %w" , path , err )
2020-05-30 11:36:40 +00:00
}
rcs . perURL [ i ] = prc
}
return & rcs , nil
}
type relabelConfigs struct {
2021-02-22 14:33:55 +00:00
global * promrelabel . ParsedConfigs
perURL [ ] * promrelabel . ParsedConfigs
2020-05-30 11:36:40 +00:00
}
// initLabelsGlobal must be called after parsing command-line flags.
func initLabelsGlobal ( ) {
2020-03-03 11:08:17 +00:00
labelsGlobal = nil
for _ , s := range * unparsedLabelsGlobal {
2020-12-08 12:53:41 +00:00
if len ( s ) == 0 {
continue
}
2020-02-23 11:35:47 +00:00
n := strings . IndexByte ( s , '=' )
if n < 0 {
2020-05-30 11:36:40 +00:00
logger . Fatalf ( "missing '=' in `-remoteWrite.label`. It must contain label in the form `name=value`; got %q" , s )
2020-02-23 11:35:47 +00:00
}
2020-03-03 11:08:17 +00:00
labelsGlobal = append ( labelsGlobal , prompbmarshal . Label {
2020-02-23 11:35:47 +00:00
Name : s [ : n ] ,
Value : s [ n + 1 : ] ,
} )
}
}
2023-08-15 11:47:48 +00:00
func ( rctx * relabelCtx ) applyRelabeling ( tss [ ] prompbmarshal . TimeSeries , pcs * promrelabel . ParsedConfigs ) [ ] prompbmarshal . TimeSeries {
if pcs . Len ( ) == 0 && ! * usePromCompatibleNaming {
2020-02-23 11:35:47 +00:00
// Nothing to change.
2020-02-28 16:57:45 +00:00
return tss
2020-02-23 11:35:47 +00:00
}
2023-12-04 23:20:44 +00:00
rctx . reset ( )
2020-02-23 11:35:47 +00:00
tssDst := tss [ : 0 ]
labels := rctx . labels [ : 0 ]
for i := range tss {
ts := & tss [ i ]
labelsLen := len ( labels )
labels = append ( labels , ts . Labels ... )
2022-10-09 11:51:14 +00:00
labels = pcs . Apply ( labels , labelsLen )
2022-10-13 09:04:10 +00:00
labels = promrelabel . FinalizeLabels ( labels [ : labelsLen ] , labels [ labelsLen : ] )
2020-02-23 11:35:47 +00:00
if len ( labels ) == labelsLen {
// Drop the current time series, since relabeling removed all the labels.
continue
}
2023-08-17 12:35:26 +00:00
if * usePromCompatibleNaming {
fixPromCompatibleNaming ( labels [ labelsLen : ] )
}
2020-02-23 11:35:47 +00:00
tssDst = append ( tssDst , prompbmarshal . TimeSeries {
Labels : labels [ labelsLen : ] ,
Samples : ts . Samples ,
} )
}
rctx . labels = labels
2020-02-28 16:57:45 +00:00
return tssDst
2020-02-23 11:35:47 +00:00
}
2023-08-17 12:35:26 +00:00
func ( rctx * relabelCtx ) appendExtraLabels ( tss [ ] prompbmarshal . TimeSeries , extraLabels [ ] prompbmarshal . Label ) {
2023-08-15 11:47:48 +00:00
if len ( extraLabels ) == 0 {
2023-08-17 12:35:26 +00:00
return
2023-08-15 11:47:48 +00:00
}
2023-12-04 23:20:44 +00:00
rctx . reset ( )
2023-08-15 11:47:48 +00:00
labels := rctx . labels [ : 0 ]
for i := range tss {
ts := & tss [ i ]
labelsLen := len ( labels )
labels = append ( labels , ts . Labels ... )
for j := range extraLabels {
extraLabel := extraLabels [ j ]
tmp := promrelabel . GetLabelByName ( labels [ labelsLen : ] , extraLabel . Name )
if tmp != nil {
tmp . Value = extraLabel . Value
} else {
labels = append ( labels , extraLabel )
}
}
2023-08-17 12:35:26 +00:00
ts . Labels = labels [ labelsLen : ]
2023-08-15 11:47:48 +00:00
}
rctx . labels = labels
}
2023-12-04 23:20:44 +00:00
func ( rctx * relabelCtx ) tenantToLabels ( tss [ ] prompbmarshal . TimeSeries , accountID , projectID uint32 ) {
rctx . reset ( )
accountIDStr := strconv . FormatUint ( uint64 ( accountID ) , 10 )
projectIDStr := strconv . FormatUint ( uint64 ( projectID ) , 10 )
labels := rctx . labels [ : 0 ]
for i := range tss {
ts := & tss [ i ]
labelsLen := len ( labels )
for _ , label := range ts . Labels {
labelName := label . Name
if labelName == "vm_account_id" || labelName == "vm_project_id" {
continue
}
labels = append ( labels , label )
}
labels = append ( labels , prompbmarshal . Label {
Name : "vm_account_id" ,
Value : accountIDStr ,
} )
labels = append ( labels , prompbmarshal . Label {
Name : "vm_project_id" ,
Value : projectIDStr ,
} )
ts . Labels = labels [ labelsLen : ]
}
rctx . labels = labels
}
2020-02-23 11:35:47 +00:00
type relabelCtx struct {
// pool for labels, which are used during the relabeling.
labels [ ] prompbmarshal . Label
}
func ( rctx * relabelCtx ) reset ( ) {
2020-11-07 14:16:56 +00:00
promrelabel . CleanLabels ( rctx . labels )
2020-02-23 11:35:47 +00:00
rctx . labels = rctx . labels [ : 0 ]
}
var relabelCtxPool = & sync . Pool {
2024-07-09 22:14:15 +00:00
New : func ( ) any {
2020-02-23 11:35:47 +00:00
return & relabelCtx { }
} ,
}
2020-03-03 11:08:17 +00:00
func getRelabelCtx ( ) * relabelCtx {
return relabelCtxPool . Get ( ) . ( * relabelCtx )
}
func putRelabelCtx ( rctx * relabelCtx ) {
2023-12-04 23:20:44 +00:00
rctx . reset ( )
2020-03-03 11:08:17 +00:00
relabelCtxPool . Put ( rctx )
}
2023-08-17 12:35:26 +00:00
func fixPromCompatibleNaming ( labels [ ] prompbmarshal . Label ) {
// Replace unsupported Prometheus chars in label names and metric names with underscores.
for i := range labels {
label := & labels [ i ]
if label . Name == "__name__" {
label . Value = promrelabel . SanitizeMetricName ( label . Value )
} else {
label . Name = promrelabel . SanitizeLabelName ( label . Name )
}
}
}