-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from LiveRamp/attributes
New Attributes Collector and Status Collector bugfix.
- Loading branch information
Showing
4 changed files
with
138 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/golang/protobuf/proto" | ||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
type Attributes struct { | ||
CurrentStatus *CurrentStatus | ||
} | ||
|
||
func NewAttributesFor(currentStatus *CurrentStatus) *Attributes { | ||
return &Attributes{ | ||
CurrentStatus: currentStatus, | ||
} | ||
} | ||
|
||
func (instance *Attributes) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- instance.Desc() | ||
} | ||
|
||
func (instance *Attributes) Desc() *prometheus.Desc { | ||
return prometheus.NewDesc( | ||
fmt.Sprintf("%s_monitor_attribute", namespace), | ||
"Attributes of the target monitor", | ||
[]string{}, | ||
prometheus.Labels{}, | ||
) | ||
} | ||
|
||
func (instance *Attributes) Collect(ch chan<- prometheus.Metric) { | ||
for _, monitor := range (*instance).CurrentStatus.Data.Monitors { | ||
element := &AttributesElement{ | ||
Parent: instance, | ||
Monitor: monitor, | ||
} | ||
ch <- element | ||
} | ||
for _, monitorGroup := range (*instance).CurrentStatus.Data.MonitorGroups { | ||
for _, monitor := range monitorGroup.Monitors { | ||
element := &AttributesElement{ | ||
Parent: instance, | ||
MonitorGroup: monitorGroup, | ||
Monitor: monitor, | ||
} | ||
ch <- element | ||
} | ||
} | ||
} | ||
|
||
type AttributesElement struct { | ||
Parent *Attributes | ||
MonitorGroup CurrentStatusMonitorGroup | ||
Monitor CurrentStatusMonitor | ||
} | ||
|
||
func (instance *AttributesElement) Write(out *dto.Metric) error { | ||
var attrValue, err = instance.Monitor.AttributeValue() | ||
// Eat the error by setting |attrValue| to something invalid. We don't want | ||
// a '-' entry to prevent collecting other metrics, and we must write | ||
// something to |out| in this pass. | ||
if err != nil { | ||
attrValue = -1 | ||
} | ||
|
||
out.Gauge = &dto.Gauge{Value: proto.Float64(float64(attrValue))} | ||
label := []*dto.LabelPair{ | ||
labelPairFor("attributeKey", instance.Monitor.AttributeKey), | ||
labelPairFor("monitorId", instance.Monitor.Id), | ||
labelPairFor("monitorDisplayName", instance.Monitor.Name), | ||
labelPairFor("monitorGroupId", instance.MonitorGroup.Id), | ||
labelPairFor("monitorGroupDisplayName", instance.MonitorGroup.Name), | ||
} | ||
out.Label = label | ||
return nil | ||
} | ||
|
||
func (instance *AttributesElement) Desc() *prometheus.Desc { | ||
return instance.Parent.Desc() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
) | ||
|
||
var ( | ||
// String value returned by current_status API to indicate 'no value'. | ||
dashValue = `"-"` | ||
|
||
// Error value returned by AttributeValue() in case |dashValue| is observed. | ||
errUndefined = errors.New("no metric value") | ||
) | ||
|
||
type CurrentStatus struct { | ||
Code int `json:"code"` | ||
ErrorCode int `json:"error_code"` | ||
Message string `json:"message"` | ||
Data CurrentStatusData `json:"data"` | ||
Code int `json:"code"` | ||
ErrorCode int `json:"error_code"` | ||
Message string `json:"message"` | ||
Data CurrentStatusData `json:"data"` | ||
} | ||
|
||
type CurrentStatusData struct { | ||
Monitors []CurrentStatusMonitor `json:"monitors"` | ||
Monitors []CurrentStatusMonitor `json:"monitors"` | ||
MonitorGroups []CurrentStatusMonitorGroup `json:"monitor_groups"` | ||
} | ||
|
||
type CurrentStatusMonitorGroup struct { | ||
Id string `json:"group_id"` | ||
Name string `json:"group_name"` | ||
Status int `json:"status"` | ||
Id string `json:"group_id"` | ||
Name string `json:"group_name"` | ||
Status int `json:"status"` | ||
Monitors []CurrentStatusMonitor `json:"monitors"` | ||
} | ||
|
||
type CurrentStatusMonitor struct { | ||
Id string `json:"monitor_id"` | ||
Name string `json:"name"` | ||
Type string `json:"monitor_type"` | ||
Status int `json:"status"` | ||
Locations []CurrentStatusLocation `json:"locations"` | ||
Id string `json:"monitor_id"` | ||
Name string `json:"name"` | ||
Type string `json:"monitor_type"` | ||
Status int `json:"status"` | ||
AttributeKey string `json:"attribute_key"` | ||
RawAttributeValue json.RawMessage `json:"attribute_value"` | ||
Locations []CurrentStatusLocation `json:"locations"` | ||
} | ||
|
||
type CurrentStatusLocation struct { | ||
Name string `json:"location_name"` | ||
Status int `json:"status"` | ||
Name string `json:"location_name"` | ||
Status int `json:"status"` | ||
} | ||
|
||
// If the raw |AttributeValue| equals the empty "-", coerce to -1. | ||
// Otherwise unmarshal as an integer. | ||
func (csm CurrentStatusMonitor) AttributeValue() (int, error) { | ||
if string(csm.RawAttributeValue) == dashValue { | ||
return 0, errUndefined | ||
} else { | ||
var i int | ||
var err = json.Unmarshal(csm.RawAttributeValue, &i) | ||
return i, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters