github.com/thanos-io/thanos@v0.32.5/pkg/targets/targetspb/custom.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package targetspb 5 6 import ( 7 "strconv" 8 "strings" 9 10 "github.com/pkg/errors" 11 "github.com/prometheus/prometheus/model/labels" 12 13 "github.com/thanos-io/thanos/pkg/store/labelpb" 14 ) 15 16 func NewTargetsResponse(targets *TargetDiscovery) *TargetsResponse { 17 return &TargetsResponse{ 18 Result: &TargetsResponse_Targets{ 19 Targets: targets, 20 }, 21 } 22 } 23 24 func NewWarningTargetsResponse(warning error) *TargetsResponse { 25 return &TargetsResponse{ 26 Result: &TargetsResponse_Warning{ 27 Warning: warning.Error(), 28 }, 29 } 30 } 31 32 func (x *TargetHealth) UnmarshalJSON(entry []byte) error { 33 fieldStr, err := strconv.Unquote(string(entry)) 34 if err != nil { 35 return errors.Wrapf(err, "targetHealth: unquote %v", string(entry)) 36 } 37 38 if fieldStr == "" { 39 return errors.New("empty targetHealth") 40 } 41 42 state, ok := TargetHealth_value[strings.ToUpper(fieldStr)] 43 if !ok { 44 return errors.Errorf("unknown targetHealth: %v", string(entry)) 45 } 46 *x = TargetHealth(state) 47 return nil 48 } 49 50 func (x *TargetHealth) MarshalJSON() ([]byte, error) { 51 return []byte(strconv.Quote(strings.ToLower(x.String()))), nil 52 } 53 54 func (x TargetHealth) Compare(y TargetHealth) int { 55 return int(x) - int(y) 56 } 57 58 func (t1 *ActiveTarget) Compare(t2 *ActiveTarget) int { 59 if d := strings.Compare(t1.ScrapeUrl, t2.ScrapeUrl); d != 0 { 60 return d 61 } 62 63 if d := strings.Compare(t1.ScrapePool, t2.ScrapePool); d != 0 { 64 return d 65 } 66 67 if d := labels.Compare(t1.DiscoveredLabels.PromLabels(), t2.DiscoveredLabels.PromLabels()); d != 0 { 68 return d 69 } 70 71 if d := labels.Compare(t1.Labels.PromLabels(), t2.Labels.PromLabels()); d != 0 { 72 return d 73 } 74 75 return 0 76 } 77 78 func (t1 *ActiveTarget) CompareState(t2 *ActiveTarget) int { 79 if d := t1.Health.Compare(t2.Health); d != 0 { 80 return d 81 } 82 83 if t1.LastScrape.Before(t2.LastScrape) { 84 return 1 85 } 86 87 if t1.LastScrape.After(t2.LastScrape) { 88 return -1 89 } 90 91 return 0 92 } 93 94 func (t1 *DroppedTarget) Compare(t2 *DroppedTarget) int { 95 if d := labels.Compare(t1.DiscoveredLabels.PromLabels(), t2.DiscoveredLabels.PromLabels()); d != 0 { 96 return d 97 } 98 99 return 0 100 } 101 102 func (t *ActiveTarget) SetLabels(ls labels.Labels) { 103 var result labelpb.ZLabelSet 104 105 if len(ls) > 0 { 106 result = labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(ls)} 107 } 108 109 t.Labels = result 110 } 111 112 func (t *ActiveTarget) SetDiscoveredLabels(ls labels.Labels) { 113 var result labelpb.ZLabelSet 114 115 if len(ls) > 0 { 116 result = labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(ls)} 117 } 118 119 t.DiscoveredLabels = result 120 } 121 122 func (t *DroppedTarget) SetDiscoveredLabels(ls labels.Labels) { 123 var result labelpb.ZLabelSet 124 125 if len(ls) > 0 { 126 result = labelpb.ZLabelSet{Labels: labelpb.ZLabelsFromPromLabels(ls)} 127 } 128 129 t.DiscoveredLabels = result 130 }