From 5f1e84f3e5d1f47861e8128b1df18f72c6e4be25 Mon Sep 17 00:00:00 2001 From: candysmurf <77.ears@gmail.com> Date: Thu, 1 Dec 2016 14:59:38 -0800 Subject: [PATCH] SDI-2274: GRPC based collectors **must** provide LastAdvertisedTime or framework panics --- control/plugin/client/grpc.go | 12 +++-- control/plugin/client/grpc_test.go | 79 ++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 control/plugin/client/grpc_test.go diff --git a/control/plugin/client/grpc.go b/control/plugin/client/grpc.go index eec248884..a7bd8a8a2 100644 --- a/control/plugin/client/grpc.go +++ b/control/plugin/client/grpc.go @@ -252,10 +252,6 @@ func (g *grpcClient) GetMetricTypes(config plugin.ConfigType) ([]core.Metric, er return nil, errors.New(reply.Error) } - for _, metric := range reply.Metrics { - metric.LastAdvertisedTime = ToTime(time.Now()) - } - results := ToCoreMetrics(reply.Metrics) return results, nil } @@ -305,6 +301,14 @@ func ToCoreMetrics(mts []*rpc.Metric) []core.Metric { } func ToCoreMetric(mt *rpc.Metric) core.Metric { + if mt.Timestamp == nil { + mt.Timestamp = ToTime(time.Now()) + } + + if mt.LastAdvertisedTime == nil { + mt.LastAdvertisedTime = ToTime(time.Now()) + } + ret := &metric{ namespace: ToCoreNamespace(mt.Namespace), version: int(mt.Version), diff --git a/control/plugin/client/grpc_test.go b/control/plugin/client/grpc_test.go new file mode 100644 index 000000000..6da5031b3 --- /dev/null +++ b/control/plugin/client/grpc_test.go @@ -0,0 +1,79 @@ +// +build small + +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 2016 Intel Corporation + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package client + +import ( + "testing" + "time" + + "github.com/intelsdi-x/snap/core" + . "github.com/smartystreets/goconvey/convey" +) + +func TestToCoreMetric(t *testing.T) { + tc := testCases() + Convey("Test ToCoreMetric", t, func() { + for _, c := range tc { + Convey(c.description, func() { + mt := ToMetric(c) + cmt := ToCoreMetric(mt) + So(cmt.Timestamp(), ShouldNotBeNil) + So(cmt.LastAdvertisedTime(), ShouldNotBeNil) + + if cmt.Version() == 2 { + So(cmt.Timestamp(), ShouldResemble, cmt.LastAdvertisedTime()) + } + }) + } + }) +} + +func testCases() []*metric { + now := time.Now() + tc := []*metric{ + &metric{ + namespace: core.NewNamespace("a", "b", "c"), + version: 1, + description: "No timeStamp and lastAdvertisedTime defined", + }, + &metric{ + namespace: core.NewNamespace("x", "y", "z"), + version: 1, + timeStamp: time.Now(), + description: "Has timestamp but no lastAdvertisedTime defined", + }, + &metric{ + namespace: core.NewNamespace("x", "y", "z"), + version: 1, + lastAdvertisedTime: time.Now(), + description: "No timestamp but has lastAdvertisedTime defined", + }, + &metric{ + namespace: core.NewNamespace("x", "y", "z"), + version: 2, + timeStamp: now, + lastAdvertisedTime: now, + description: "Has both timestamp and lastAdvertisedTime defined", + }, + } + return tc +}