Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Add an option to set metric name prefix (#84)
Browse files Browse the repository at this point in the history
* Add an option for setting metric name prefix

* renaming

* renaming
  • Loading branch information
jjzeng-seattle authored Jun 15, 2020
1 parent e526276 commit f8c219d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
7 changes: 4 additions & 3 deletions ocagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Exporter struct {
compressor string
headers map[string]string
lastConnectErrPtr unsafe.Pointer
metricNamePerfix string

startOnce sync.Once
stopCh chan bool
Expand Down Expand Up @@ -513,14 +514,14 @@ func (ae *Exporter) uploadTraces(sdl []*trace.SpanData) {
}
}

func ocViewDataToPbMetrics(vdl []*view.Data) []*metricspb.Metric {
func ocViewDataToPbMetrics(vdl []*view.Data, metricNamePrefix string) []*metricspb.Metric {
if len(vdl) == 0 {
return nil
}
metrics := make([]*metricspb.Metric, 0, len(vdl))
for _, vd := range vdl {
if vd != nil {
vmetric, err := viewDataToMetric(vd)
vmetric, err := viewDataToMetric(vd, metricNamePrefix)
// TODO: (@odeke-em) somehow report this error, if it is non-nil.
if err == nil && vmetric != nil {
metrics = append(metrics, vmetric)
Expand All @@ -531,7 +532,7 @@ func ocViewDataToPbMetrics(vdl []*view.Data) []*metricspb.Metric {
}

func (ae *Exporter) uploadViewData(vdl []*view.Data) {
protoMetrics := ocViewDataToPbMetrics(vdl)
protoMetrics := ocViewDataToPbMetrics(vdl, ae.metricNamePerfix)
if len(protoMetrics) == 0 {
return
}
Expand Down
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,16 @@ func WithGRPCDialOption(opts ...grpc.DialOption) ExporterOption {
func (opts grpcDialOptions) withExporter(e *Exporter) {
e.grpcDialOptions = opts
}

type metricNamePrefixSetter string

var _ ExporterOption = (*metricNamePrefixSetter)(nil)

func (p metricNamePrefixSetter) withExporter(e *Exporter) {
e.metricNamePerfix = string(p)
}

// WithMetricNamePrefix provides an option for the caller to add a prefix to metric names.
func WithMetricNamePrefix(prefix string) ExporterOption {
return metricNamePrefixSetter(prefix)
}
12 changes: 8 additions & 4 deletions transform_stats_to_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ var (
errNilViewData = errors.New("expecting a non-nil view.Data")
)

func viewDataToMetric(vd *view.Data) (*metricspb.Metric, error) {
func viewDataToMetric(vd *view.Data, metricNamePrefix string) (*metricspb.Metric, error) {
if vd == nil {
return nil, errNilViewData
}

descriptor, err := viewToMetricDescriptor(vd.View)
descriptor, err := viewToMetricDescriptor(vd.View, metricNamePrefix)
if err != nil {
return nil, err
}
Expand All @@ -55,16 +55,20 @@ func viewDataToMetric(vd *view.Data) (*metricspb.Metric, error) {
return metric, nil
}

func viewToMetricDescriptor(v *view.View) (*metricspb.MetricDescriptor, error) {
func viewToMetricDescriptor(v *view.View, metricNamePrefix string) (*metricspb.MetricDescriptor, error) {
if v == nil {
return nil, errNilView
}
if v.Measure == nil {
return nil, errNilMeasure
}

name := stringOrCall(v.Name, v.Measure.Name)
if len(metricNamePrefix) > 0 {
name = metricNamePrefix + "/" + name
}
desc := &metricspb.MetricDescriptor{
Name: stringOrCall(v.Name, v.Measure.Name),
Name: name,
Description: stringOrCall(v.Description, v.Measure.Description),
Unit: v.Measure.Unit(),
Type: aggregationToMetricDescriptorType(v),
Expand Down
63 changes: 59 additions & 4 deletions transform_stats_to_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ var (
)

type test struct {
in *view.Data
want *metricspb.Metric
wantErr string
in *view.Data
metricNamePrefix string
want *metricspb.Metric
wantErr string
}

func TestViewDataToMetrics_Distribution(t *testing.T) {
Expand Down Expand Up @@ -1150,9 +1151,63 @@ func TestViewDataToMetrics_MissingVsEmptyLabelValues(t *testing.T) {
testViewDataToMetrics(t, tests)
}

func TestViewDataToMetrics_WithMetricNamePrefix(t *testing.T) {
startTime := time.Date(2018, 11, 25, 15, 38, 18, 997, time.UTC)
endTime := startTime.Add(100 * time.Millisecond)

tests := []*test{
// Testing with a stats.Float64 measure.
{
in: &view.Data{
Start: startTime,
End: endTime,
View: &view.View{
Name: "latency",
Description: "speed of the various runners",
Aggregation: view.Sum(),
Measure: mSprinterLatencyMs,
},
Rows: []*view.Row{
{
Data: &view.SumData{Value: 27},
},
},
},
metricNamePrefix: "foo.bar",
want: &metricspb.Metric{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: "foo.bar/latency",
Description: "speed of the various runners",
Unit: "ms",
Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE,
},
Timeseries: []*metricspb.TimeSeries{
{
StartTimestamp: &timestamp.Timestamp{
Seconds: 1543160298,
Nanos: 997,
},
Points: []*metricspb.Point{
{
Timestamp: &timestamp.Timestamp{
Seconds: 1543160298,
Nanos: 100000997,
},
Value: &metricspb.Point_DoubleValue{DoubleValue: 27},
},
},
},
},
},
},
}

testViewDataToMetrics(t, tests)
}

func testViewDataToMetrics(t *testing.T, tests []*test) {
for i, tt := range tests {
got, err := viewDataToMetric(tt.in)
got, err := viewDataToMetric(tt.in, tt.metricNamePrefix)
if tt.wantErr != "" {
continue
}
Expand Down

0 comments on commit f8c219d

Please sign in to comment.