github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/scrape/discovery/targetgroup/targetgroup.go (about) 1 // Copyright 2013 The Prometheus Authors 2 // Copyright 2021 The Pyroscope Authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package targetgroup 17 18 import ( 19 "bytes" 20 "encoding/json" 21 22 "github.com/pyroscope-io/pyroscope/pkg/flameql" 23 "github.com/pyroscope-io/pyroscope/pkg/scrape/model" 24 ) 25 26 // Group is a set of targets with a common label set(production , test, staging etc.). 27 type Group struct { 28 // Targets is a list of targets identified by a label set. Each target is 29 // uniquely identifiable in the group by its address label. 30 Targets []model.LabelSet 31 // Labels is a set of labels that is common across all targets in the group. 32 Labels model.LabelSet 33 // Source is an identifier that describes a group of targets. 34 Source string 35 } 36 37 func (tg Group) String() string { 38 return tg.Source 39 } 40 41 type targetGroup struct { 42 AppName string `yaml:"application" json:"application"` 43 SpyName string `yaml:"spy-name" json:"spy-name"` 44 Targets []string `yaml:"targets" json:"targets"` 45 Labels model.LabelSet `yaml:"labels" json:"labels"` 46 } 47 48 // UnmarshalYAML implements the yaml.Unmarshaler interface. 49 func (tg *Group) UnmarshalYAML(unmarshal func(interface{}) error) error { 50 var g targetGroup 51 if err := unmarshal(&g); err != nil { 52 return err 53 } 54 if err := flameql.ValidateAppName(g.AppName); err != nil { 55 return err 56 } 57 tg.Targets = make([]model.LabelSet, 0, len(g.Targets)) 58 for _, t := range g.Targets { 59 tg.Targets = append(tg.Targets, model.LabelSet{ 60 model.AddressLabel: model.LabelValue(t), 61 model.AppNameLabel: model.LabelValue(g.AppName), 62 model.SpyNameLabel: model.LabelValue(g.SpyName), 63 }) 64 } 65 tg.Labels = g.Labels 66 return nil 67 } 68 69 // UnmarshalJSON implements the json.Unmarshaler interface. 70 func (tg *Group) UnmarshalJSON(b []byte) error { 71 var g targetGroup 72 dec := json.NewDecoder(bytes.NewReader(b)) 73 dec.DisallowUnknownFields() 74 if err := dec.Decode(&g); err != nil { 75 return err 76 } 77 if err := flameql.ValidateAppName(g.AppName); err != nil { 78 return err 79 } 80 tg.Targets = make([]model.LabelSet, 0, len(g.Targets)) 81 for _, t := range g.Targets { 82 tg.Targets = append(tg.Targets, model.LabelSet{ 83 model.AddressLabel: model.LabelValue(t), 84 model.AppNameLabel: model.LabelValue(g.AppName), 85 model.SpyNameLabel: model.LabelValue(g.SpyName), 86 }) 87 } 88 tg.Labels = g.Labels 89 return nil 90 }