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

Commit e236fa3

Browse files
authored
LastValue should export as gauge metric kind (#17)
* Fix the bug for the situation when the aggregation type is LastValue. The metric kind should be gauge instead of cummulative * Fix the format * Fix the minor bugs for that second and nanos conflicts in the newGaugePoints * Duplicate the Point create part instead of the whole TimeSeries. Remove the useless input argument in the newGaugePoint method * Modify the format of stats.go
1 parent 20ba8ea commit e236fa3

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

stats.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ func (e *statsExporter) createMeasure(ctx context.Context, vd *view.Data) error
242242
metricType := namespacedViewName(viewName)
243243
var valueType metricpb.MetricDescriptor_ValueType
244244
unit := m.Unit()
245+
// Default metric Kind
246+
metricKind := metricpb.MetricDescriptor_CUMULATIVE
245247

246248
switch agg.Type {
247249
case view.AggTypeCount:
@@ -259,6 +261,7 @@ func (e *statsExporter) createMeasure(ctx context.Context, vd *view.Data) error
259261
case view.AggTypeDistribution:
260262
valueType = metricpb.MetricDescriptor_DISTRIBUTION
261263
case view.AggTypeLastValue:
264+
metricKind = metricpb.MetricDescriptor_GAUGE
262265
switch m.(type) {
263266
case *stats.Int64Measure:
264267
valueType = metricpb.MetricDescriptor_INT64
@@ -269,7 +272,6 @@ func (e *statsExporter) createMeasure(ctx context.Context, vd *view.Data) error
269272
return fmt.Errorf("unsupported aggregation type: %s", agg.Type.String())
270273
}
271274

272-
metricKind := metricpb.MetricDescriptor_CUMULATIVE
273275
displayNamePrefix := defaultDisplayNamePrefix
274276
if e.o.MetricPrefix != "" {
275277
displayNamePrefix = e.o.MetricPrefix
@@ -297,6 +299,15 @@ func (e *statsExporter) createMeasure(ctx context.Context, vd *view.Data) error
297299
}
298300

299301
func newPoint(v *view.View, row *view.Row, start, end time.Time) *monitoringpb.Point {
302+
switch v.Aggregation.Type {
303+
case view.AggTypeLastValue:
304+
return newGaugePoint(v, row, end)
305+
default:
306+
return newCumulativePoint(v, row, start, end)
307+
}
308+
}
309+
310+
func newCumulativePoint(v *view.View, row *view.Row, start, end time.Time) *monitoringpb.Point {
300311
return &monitoringpb.Point{
301312
Interval: &monitoringpb.TimeInterval{
302313
StartTime: &timestamp.Timestamp{
@@ -312,6 +323,20 @@ func newPoint(v *view.View, row *view.Row, start, end time.Time) *monitoringpb.P
312323
}
313324
}
314325

326+
func newGaugePoint(v *view.View, row *view.Row, end time.Time) *monitoringpb.Point {
327+
gaugeTime := &timestamp.Timestamp{
328+
Seconds: end.Unix(),
329+
Nanos: int32(end.Nanosecond()),
330+
}
331+
return &monitoringpb.Point{
332+
Interval: &monitoringpb.TimeInterval{
333+
StartTime: gaugeTime,
334+
EndTime: gaugeTime,
335+
},
336+
Value: newTypedValue(v, row),
337+
}
338+
}
339+
315340
func newTypedValue(vd *view.View, r *view.Row) *monitoringpb.TypedValue {
316341
switch v := r.Data.(type) {
317342
case *view.CountData:

stats_test.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ func TestExporter_makeReq(t *testing.T) {
8585
Measure: m,
8686
Aggregation: view.Count(),
8787
}
88+
89+
lastValueView := &view.View{
90+
Name: "lasttestview",
91+
Description: "desc",
92+
TagKeys: []tag.Key{key},
93+
Measure: m,
94+
Aggregation: view.LastValue(),
95+
}
96+
8897
distView := &view.View{
8998
Name: "distview",
9099
Description: "desc",
@@ -247,13 +256,13 @@ func TestExporter_makeReq(t *testing.T) {
247256
{
248257
name: "last value agg",
249258
projID: "proj-id",
250-
vd: newTestViewData(v, start, end, &last1, &last2),
259+
vd: newTestViewData(lastValueView, start, end, &last1, &last2),
251260
want: []*monitoringpb.CreateTimeSeriesRequest{{
252261
Name: monitoring.MetricProjectPath("proj-id"),
253262
TimeSeries: []*monitoringpb.TimeSeries{
254263
{
255264
Metric: &metricpb.Metric{
256-
Type: "custom.googleapis.com/opencensus/testview",
265+
Type: "custom.googleapis.com/opencensus/lasttestview",
257266
Labels: map[string]string{
258267
"test_key": "test-value-1",
259268
opencensusTaskKey: taskValue,
@@ -266,7 +275,7 @@ func TestExporter_makeReq(t *testing.T) {
266275
{
267276
Interval: &monitoringpb.TimeInterval{
268277
StartTime: &timestamp.Timestamp{
269-
Seconds: start.Unix(),
278+
Seconds: end.Unix(),
270279
Nanos: int32(start.Nanosecond()),
271280
},
272281
EndTime: &timestamp.Timestamp{
@@ -282,7 +291,7 @@ func TestExporter_makeReq(t *testing.T) {
282291
},
283292
{
284293
Metric: &metricpb.Metric{
285-
Type: "custom.googleapis.com/opencensus/testview",
294+
Type: "custom.googleapis.com/opencensus/lasttestview",
286295
Labels: map[string]string{
287296
"test_key": "test-value-2",
288297
opencensusTaskKey: taskValue,
@@ -295,7 +304,7 @@ func TestExporter_makeReq(t *testing.T) {
295304
{
296305
Interval: &monitoringpb.TimeInterval{
297306
StartTime: &timestamp.Timestamp{
298-
Seconds: start.Unix(),
307+
Seconds: end.Unix(),
299308
Nanos: int32(start.Nanosecond()),
300309
},
301310
EndTime: &timestamp.Timestamp{

0 commit comments

Comments
 (0)