mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-29 07:29:42 -08:00
516579a97e
* discovery/targetgroup: support marshaling to JSON targetgroup.Group is able to be marshaled to and from YAML, but not with JSON. JSON is used for the http_sd API representation, so users implementing the API were required to implement their own type which is expected by the JSON unmarshaler. Signed-off-by: Robert Fratto <robertfratto@gmail.com> * fix lint error Signed-off-by: Robert Fratto <robertfratto@gmail.com> * Update discovery/targetgroup/targetgroup.go Co-authored-by: Julius Volz <julius.volz@gmail.com> Signed-off-by: Robert Fratto <robertfratto@gmail.com> * s/Json/JSON Signed-off-by: Robert Fratto <robertfratto@gmail.com> Co-authored-by: Julius Volz <julius.volz@gmail.com>
109 lines
3 KiB
Go
109 lines
3 KiB
Go
// Copyright 2013 The Prometheus Authors
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package targetgroup
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
|
|
"github.com/prometheus/common/model"
|
|
)
|
|
|
|
// Group is a set of targets with a common label set(production , test, staging etc.).
|
|
type Group struct {
|
|
// Targets is a list of targets identified by a label set. Each target is
|
|
// uniquely identifiable in the group by its address label.
|
|
Targets []model.LabelSet
|
|
// Labels is a set of labels that is common across all targets in the group.
|
|
Labels model.LabelSet
|
|
|
|
// Source is an identifier that describes a group of targets.
|
|
Source string
|
|
}
|
|
|
|
func (tg Group) String() string {
|
|
return tg.Source
|
|
}
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
func (tg *Group) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
g := struct {
|
|
Targets []string `yaml:"targets"`
|
|
Labels model.LabelSet `yaml:"labels"`
|
|
}{}
|
|
if err := unmarshal(&g); err != nil {
|
|
return err
|
|
}
|
|
tg.Targets = make([]model.LabelSet, 0, len(g.Targets))
|
|
for _, t := range g.Targets {
|
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
|
model.AddressLabel: model.LabelValue(t),
|
|
})
|
|
}
|
|
tg.Labels = g.Labels
|
|
return nil
|
|
}
|
|
|
|
// MarshalYAML implements the yaml.Marshaler interface.
|
|
func (tg Group) MarshalYAML() (interface{}, error) {
|
|
g := &struct {
|
|
Targets []string `yaml:"targets"`
|
|
Labels model.LabelSet `yaml:"labels,omitempty"`
|
|
}{
|
|
Targets: make([]string, 0, len(tg.Targets)),
|
|
Labels: tg.Labels,
|
|
}
|
|
for _, t := range tg.Targets {
|
|
g.Targets = append(g.Targets, string(t[model.AddressLabel]))
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
|
func (tg *Group) UnmarshalJSON(b []byte) error {
|
|
g := struct {
|
|
Targets []string `json:"targets"`
|
|
Labels model.LabelSet `json:"labels"`
|
|
}{}
|
|
|
|
dec := json.NewDecoder(bytes.NewReader(b))
|
|
dec.DisallowUnknownFields()
|
|
if err := dec.Decode(&g); err != nil {
|
|
return err
|
|
}
|
|
tg.Targets = make([]model.LabelSet, 0, len(g.Targets))
|
|
for _, t := range g.Targets {
|
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
|
model.AddressLabel: model.LabelValue(t),
|
|
})
|
|
}
|
|
tg.Labels = g.Labels
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
func (tg Group) MarshalJSON() ([]byte, error) {
|
|
g := &struct {
|
|
Targets []string `json:"targets"`
|
|
Labels model.LabelSet `json:"labels,omitempty"`
|
|
}{
|
|
Targets: make([]string, 0, len(tg.Targets)),
|
|
Labels: tg.Labels,
|
|
}
|
|
for _, t := range tg.Targets {
|
|
g.Targets = append(g.Targets, string(t[model.AddressLabel]))
|
|
}
|
|
return json.Marshal(g)
|
|
}
|