mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-06 14:17:12 +02:00
Implement json encoder/decoder for regexp (#15383)
* implement json encoder/decoder for regexp --------- Signed-off-by: Ben Ye <benye@amazon.com>
This commit is contained in:
parent
dd1d707e1c
commit
872e2db2a9
@ -16,6 +16,7 @@ package relabel
|
|||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -84,20 +85,20 @@ func (a *Action) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
// A list of labels from which values are taken and concatenated
|
// A list of labels from which values are taken and concatenated
|
||||||
// with the configured separator in order.
|
// with the configured separator in order.
|
||||||
SourceLabels model.LabelNames `yaml:"source_labels,flow,omitempty"`
|
SourceLabels model.LabelNames `yaml:"source_labels,flow,omitempty" json:"sourceLabels,omitempty"`
|
||||||
// Separator is the string between concatenated values from the source labels.
|
// Separator is the string between concatenated values from the source labels.
|
||||||
Separator string `yaml:"separator,omitempty"`
|
Separator string `yaml:"separator,omitempty" json:"separator,omitempty"`
|
||||||
// Regex against which the concatenation is matched.
|
// Regex against which the concatenation is matched.
|
||||||
Regex Regexp `yaml:"regex,omitempty"`
|
Regex Regexp `yaml:"regex,omitempty" json:"regex,omitempty"`
|
||||||
// Modulus to take of the hash of concatenated values from the source labels.
|
// Modulus to take of the hash of concatenated values from the source labels.
|
||||||
Modulus uint64 `yaml:"modulus,omitempty"`
|
Modulus uint64 `yaml:"modulus,omitempty" json:"modulus,omitempty"`
|
||||||
// TargetLabel is the label to which the resulting string is written in a replacement.
|
// TargetLabel is the label to which the resulting string is written in a replacement.
|
||||||
// Regexp interpolation is allowed for the replace action.
|
// Regexp interpolation is allowed for the replace action.
|
||||||
TargetLabel string `yaml:"target_label,omitempty"`
|
TargetLabel string `yaml:"target_label,omitempty" json:"targetLabel,omitempty"`
|
||||||
// Replacement is the regex replacement pattern to be used.
|
// Replacement is the regex replacement pattern to be used.
|
||||||
Replacement string `yaml:"replacement,omitempty"`
|
Replacement string `yaml:"replacement,omitempty" json:"replacement,omitempty"`
|
||||||
// Action is the action to be performed for the relabeling.
|
// Action is the action to be performed for the relabeling.
|
||||||
Action Action `yaml:"action,omitempty"`
|
Action Action `yaml:"action,omitempty" json:"action,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
||||||
@ -207,6 +208,25 @@ func (re Regexp) MarshalYAML() (interface{}, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||||
|
func (re *Regexp) UnmarshalJSON(b []byte) error {
|
||||||
|
var s string
|
||||||
|
if err := json.Unmarshal(b, &s); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
r, err := NewRegexp(s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*re = r
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalJSON implements the json.Marshaler interface.
|
||||||
|
func (re Regexp) MarshalJSON() ([]byte, error) {
|
||||||
|
return json.Marshal(re.String())
|
||||||
|
}
|
||||||
|
|
||||||
// IsZero implements the yaml.IsZeroer interface.
|
// IsZero implements the yaml.IsZeroer interface.
|
||||||
func (re Regexp) IsZero() bool {
|
func (re Regexp) IsZero() bool {
|
||||||
return re.Regexp == DefaultRelabelConfig.Regex.Regexp
|
return re.Regexp == DefaultRelabelConfig.Regex.Regexp
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
package relabel
|
package relabel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -964,3 +965,35 @@ func TestRegexp_ShouldMarshalAndUnmarshalZeroValue(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Nil(t, unmarshalled.Regexp)
|
require.Nil(t, unmarshalled.Regexp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRegexp_JSONUnmarshalThenMarshal(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Empty regex",
|
||||||
|
input: `{"regex":""}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "string literal",
|
||||||
|
input: `{"regex":"foo"}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "regex",
|
||||||
|
input: `{"regex":".*foo.*"}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
var unmarshalled Config
|
||||||
|
err := json.Unmarshal([]byte(test.input), &unmarshalled)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
marshalled, err := json.Marshal(&unmarshalled)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.Equal(t, test.input, string(marshalled))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user