Skip to content

Commit

Permalink
Augment stackdriver adapter config with a metric type field (istio#4607)
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue.

Augment stackdriver adapter config with a metric type field

This will allow stackdriver adapter to export non-custom metric
  • Loading branch information
bianpengyuan authored and istio-merge-robot committed Mar 30, 2018
1 parent 27866f8 commit 6349f14
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ <h2 id="Params.MetricInfo">Params.MetricInfo</h2>
This field must be provided for metrics declared to be of type DISTRIBUTION.
This field will be ignored for non-distribution metric kinds.</p>

</td>
</tr>
<tr id="Params.MetricInfo.metric_type">
<td><code>metricType</code></td>
<td><code>string</code></td>
<td>
<p>Stackdriver metric type name, e.g.
istio.io/service/server/request_count. If this is not provided, a
concantenation of custom metric prefix (custom.googleapis.com/) and
Istio metric name will be used.</p>

</td>
</tr>
</tbody>
Expand Down
170 changes: 108 additions & 62 deletions mixer/adapter/stackdriver/config/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions mixer/adapter/stackdriver/config/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ message Params {
// This field will be ignored for non-distribution metric kinds.
BucketsDefinition buckets = 3;

// Stackdriver metric type name, e.g.
// istio.io/service/server/request_count. If this is not provided, a
// concantenation of custom metric prefix (custom.googleapis.com/) and
// Istio metric name will be used.
string metric_type = 4;
}

// A map of Istio metric name to Stackdriver metric info.
map<string, MetricInfo> metric_info = 8;

Expand Down
8 changes: 6 additions & 2 deletions mixer/adapter/stackdriver/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ func (b *builder) Build(ctx context.Context, env adapter.Env) (adapter.Handler,
env.Logger().Warningf("No stackdriver info found for metric %s, skipping it", name)
continue
}
mt := i.MetricType
if mt == "" {
mt = customMetricType(name)
}
// TODO: do we want to make sure that the definition conforms to stackdrvier requirements? Really that needs to happen during config validation
types[name] = info{
ttype: metricType(name),
ttype: mt,
vtype: t.Value,
minfo: i,
}
Expand Down Expand Up @@ -263,7 +267,7 @@ func toTypedVal(val interface{}, i info) *monitoringpb.TypedValue {
}
}

func metricType(name string) string {
func customMetricType(name string) string {
// TODO: figure out what, if anything, we need to do to sanitize these.
return customMetricPrefix + name
}
52 changes: 52 additions & 0 deletions mixer/adapter/stackdriver/metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,58 @@ func TestFactory_NewMetricsAspect_Errs(t *testing.T) {
}
}

func TestMetricType(t *testing.T) {
tests := []struct {
name string
cfg *config.Params
want string
}{
{
"custom metric",
&config.Params{
MetricInfo: map[string]*config.Params_MetricInfo{
"metric": {
MetricType: "istio.io/metric",
},
},
},
"istio.io/metric",
},
{
"metric type",
&config.Params{
MetricInfo: map[string]*config.Params_MetricInfo{
"metric": {},
},
},
customMetricPrefix + "metric",
},
}

for idx, tt := range tests {
t.Run(fmt.Sprintf("[%d] %s", idx, tt.name), func(t *testing.T) {
metrics := map[string]*metrict.Type{"metric": {}}
b := &builder{createClient: clientFunc(nil)}
b.SetMetricTypes(metrics)
b.SetAdapterConfig(tt.cfg)
env := test.NewEnv(t)
h, err := b.Build(context.Background(), env)
if err != nil {
t.Fatalf("Failed building metric handler: %v", err)
}
info := h.(*handler).metricInfo
if _, found := info["metric"]; !found {
t.Fatalf("Failed find info for metric, got %v", info)
}

got := info["metric"].ttype
if tt.want != got {
t.Errorf("Bad metric type: Build(%v) => got %v, wanted %v", tt.cfg, got, tt.want)
}
})
}
}

func TestRecord(t *testing.T) {
projectID := "pid"
resource := &monitoredres.MonitoredResource{
Expand Down

0 comments on commit 6349f14

Please sign in to comment.