Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #20 from IRCody/add_bool_type
Browse files Browse the repository at this point in the history
Added bool type to metric.Data allowed types
  • Loading branch information
candysmurf authored Aug 30, 2016
2 parents 57df726 + f947193 commit 952a517
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 91 deletions.
9 changes: 7 additions & 2 deletions v1/plugin/collector_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func (c *collectorProxy) CollectMetrics(ctx context.Context, arg *rpc.MetricsArg
}
mts := []*rpc.Metric{}
for _, mt := range r {
metric := toProtoMetric(mt)
metric, err := toProtoMetric(mt)
if err != nil {
return nil, err
}
mts = append(mts, metric)
}
reply := &rpc.MetricsReply{Metrics: mts}
Expand All @@ -60,7 +63,9 @@ func (c *collectorProxy) GetMetricTypes(ctx context.Context, arg *rpc.GetMetricT
}
metrics := []*rpc.Metric{}
for _, mt := range r {
metric := toProtoMetric(mt)
// We can ignore this error since we are not returning data from
// GetMetricTypes.
metric, _ := toProtoMetric(mt)
metrics = append(metrics, metric)
}
reply := &rpc.MetricsReply{
Expand Down
14 changes: 10 additions & 4 deletions v1/plugin/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ type Metric struct {
lastAdvertisedTime time.Time
}

func toProtoMetric(mt Metric) *rpc.Metric {
// Converts a metric to an protobuf metric.
// Returns an error in the case where the metric.Data is not one of the
// supported types.
func toProtoMetric(mt Metric) (*rpc.Metric, error) {
if mt.Timestamp == (time.Time{}) {
//Timestamp is unitialized, set to time.Now()
mt.Timestamp = time.Now()
Expand All @@ -51,7 +54,6 @@ func toProtoMetric(mt Metric) *rpc.Metric {
// lastAdvertisedTime is unitialized, set to time.Now()
mt.lastAdvertisedTime = time.Now()
}

metric := &rpc.Metric{
Namespace: toProtoNamespace(mt.Namespace),
Version: mt.Version,
Expand Down Expand Up @@ -82,12 +84,14 @@ func toProtoMetric(mt Metric) *rpc.Metric {
metric.Data = &rpc.Metric_Int64Data{t}
case []byte:
metric.Data = &rpc.Metric_BytesData{t}
case bool:
metric.Data = &rpc.Metric_BoolData{t}
case nil:
metric.Data = nil
default:
panic(fmt.Sprintf("unsupported type: %s", t))
return nil, fmt.Errorf("unsupported type: %s given in metric data", t)
}
return metric
return metric, nil
}

func fromProtoMetric(mt *rpc.Metric) Metric {
Expand Down Expand Up @@ -117,6 +121,8 @@ func fromProtoMetric(mt *rpc.Metric) Metric {
metric.Data = mt.GetInt64Data()
case *rpc.Metric_Int32Data:
metric.Data = mt.GetInt32Data()
case *rpc.Metric_BoolData:
metric.Data = mt.GetBoolData()
}

return metric
Expand Down
5 changes: 4 additions & 1 deletion v1/plugin/processor_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func (p *processorProxy) Process(ctx context.Context, arg *rpc.PubProcArg) (*rpc

mts := []*rpc.Metric{}
for _, mt := range r {
metric := toProtoMetric(mt)
metric, err := toProtoMetric(mt)
if err != nil {
return nil, err
}
mts = append(mts, metric)
}
reply := &rpc.MetricsReply{Metrics: mts}
Expand Down
Loading

0 comments on commit 952a517

Please sign in to comment.