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

Resource per metric #224

Merged
merged 5 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 161 additions & 29 deletions metrics_proto_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,114 @@ func TestVariousCasesFromFile(t *testing.T) {
files := []string{
"ExportLabels",
"ExportMetricsOfAllTypes",
"BuiltInMetrics",
}
for _, file := range files {
tc := readTestCaseFromFiles(t, file)
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the agent.
conn := createConn(t, addr)
defer conn.Close()

// Finally create the OpenCensus stats exporter
se := createExporter(t, conn, defaultOpts)
executeTestCase(t, tc, se, server, nil)

}
}

func TestMetricsWithResourcePerPushCall(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
func TestMetricsWithPrefix(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()
tc := readTestCaseFromFiles(t, "SingleMetric")

prefixes := []string{
"example.com/",
"example.com/foo/",
}
metricName := "ocagent.io"
saveMetricType := tc.outTSR[0].TimeSeries[0].Metric.Type
saveMDRName := tc.outMDR[0].MetricDescriptor.Name
saveMDRDispName := tc.outMDR[0].MetricDescriptor.DisplayName

for _, prefix := range prefixes {
opts := defaultOpts
opts.MetricPrefix = prefix
se := createExporter(t, conn, opts)

mt := strings.Replace(saveMetricType, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Name = strings.Replace(saveMDRName, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Type = mt
tc.outMDR[0].MetricDescriptor.DisplayName = strings.Replace(saveMDRDispName, "OpenCensus/"+metricName, (prefix + metricName), -1)

tc.outTSR[0].TimeSeries[0].Metric.Type = mt

executeTestCase(t, tc, se, server, nil)
}
}

func TestMetricsWithPrefixWithDomain(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

tc := readTestCaseFromFiles(t, "SingleMetric")

prefixes := []string{
"custom.googleapis.com/prometheus/",
"external.googleapis.com/prometheus/",
}
metricName := "ocagent.io"
saveMetricType := strings.Replace(tc.outTSR[0].TimeSeries[0].Metric.Type, "custom.googleapis.com/opencensus/", "", -1)
saveMDRName := strings.Replace(tc.outMDR[0].MetricDescriptor.Name, "custom.googleapis.com/opencensus/", "", -1)
saveMDRDispName := tc.outMDR[0].MetricDescriptor.DisplayName

for _, prefix := range prefixes {
opts := defaultOpts
opts.MetricPrefix = prefix
se := createExporter(t, conn, opts)

mt := strings.Replace(saveMetricType, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Name = strings.Replace(saveMDRName, metricName, (prefix + metricName), -1)
tc.outMDR[0].MetricDescriptor.Type = mt
tc.outMDR[0].MetricDescriptor.DisplayName = strings.Replace(saveMDRDispName, "OpenCensus/"+metricName, (prefix + metricName), -1)

tc.outTSR[0].TimeSeries[0].Metric.Type = mt

executeTestCase(t, tc, se, server, nil)
}
}

func TestBuiltInMetricsUsingPrefix(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

tc := readTestCaseFromFiles(t, "SingleMetric")

prefixes := []string{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can also test "external.googleapis.com/prometheus/".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

"googleapis.com/",
"kubernetes.io/",
"istio.io/",
}
metricName := "ocagent.io"
saveMetricType := tc.outTSR[0].TimeSeries[0].Metric.Type

for _, prefix := range prefixes {
opts := defaultOpts
opts.MetricPrefix = prefix
se := createExporter(t, conn, opts)

// no CreateMetricDescriptorRequest expected.
tc.outMDR = nil

mt := strings.Replace(saveMetricType, "custom.googleapis.com/opencensus/"+metricName, (prefix + metricName), -1)
tc.outTSR[0].TimeSeries[0].Metric.Type = mt

executeTestCase(t, tc, se, server, nil)
}
}

func TestMetricsWithResourcePerPushCall(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

inResources, outResources := readTestResourcesFiles(t, "Resources")
inLen := len(inResources)
Expand All @@ -114,13 +198,64 @@ func TestMetricsWithResourcePerPushCall(t *testing.T) {
}
}

func TestMetricsWithResourceWithMissingFieldsPerPushCall(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
func TestMetricsWithResourcePerMetric(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

inResources, outResources := readTestResourcesFiles(t, "Resources")
inLen := len(inResources)
outLen := len(outResources)
if inLen != outLen {
t.Errorf("Data invalid: input Resource len (%d) != output Resource len (%d)\n", inLen, outLen)
return
}

tcSingleMetric := readTestCaseFromFiles(t, "SingleMetric")

for i, inRes := range inResources {
se := createExporter(t, conn, defaultOpts)

tc := *tcSingleMetric
tc.name = inRes.Type
tc.inMetric[0].Resource = inResources[i]
tc.outTSR[0].TimeSeries[0].Resource = outResources[i]

executeTestCase(t, &tc, se, server, nil)
}
}

func TestMetricsWithResourcePerMetricTakesPrecedence(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()
inResources, outResources := readTestResourcesFiles(t, "Resources")
inLen := len(inResources)
outLen := len(outResources)
if inLen != outLen {
t.Errorf("Data invalid: input Resource len (%d) != output Resource len (%d)\n", inLen, outLen)
return
}

tcSingleMetric := readTestCaseFromFiles(t, "SingleMetric")

// use the same resource for push call. Resource per metric should take precedence.
perPushRes := inResources[inLen-1]

for i, inRes := range inResources {
se := createExporter(t, conn, defaultOpts)

tc := *tcSingleMetric
tc.name = inRes.Type
tc.inMetric[0].Resource = inResources[i]
tc.outTSR[0].TimeSeries[0].Resource = outResources[i]

executeTestCase(t, &tc, se, server, perPushRes)
}
}

func TestMetricsWithResourceWithMissingFieldsPerPushCall(t *testing.T) {
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

inResources, outResources := readTestResourcesFiles(t, "ResourcesWithMissingFields")
inLen := len(inResources)
Expand All @@ -144,13 +279,9 @@ func TestMetricsWithResourceWithMissingFieldsPerPushCall(t *testing.T) {
}

func TestExportMaxTSPerRequest(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

// Finally create the OpenCensus stats exporter
se := createExporter(t, conn, defaultOpts)

Expand Down Expand Up @@ -187,13 +318,9 @@ func TestExportMaxTSPerRequest(t *testing.T) {
}

func TestExportMaxTSPerRequestAcrossTwoMetrics(t *testing.T) {
server, addr, doneFn := createFakeServer(t)
server, conn, doneFn := createFakeServerConn(t)
defer doneFn()

// Now create a gRPC connection to the server.
conn := createConn(t, addr)
defer conn.Close()

// Finally create the OpenCensus stats exporter
se := createExporter(t, conn, defaultOpts)

Expand Down Expand Up @@ -328,7 +455,9 @@ func readTestCaseFromFiles(t *testing.T, filename string) *testCases {
if err != nil {
t.Fatalf("error unmarshalling CreateMetricDescriptorRequest protos from file " + filename)
}
tc.outMDR = append(tc.outMDR, &outMDR)
if outMDR.Name != "" {
tc.outMDR = append(tc.outMDR, &outMDR)
}
}

// Read expected output CreateTimeSeriesRequest proto.
Expand Down Expand Up @@ -394,7 +523,7 @@ type fakeMetricsServer struct {
stackdriverMetricDescriptors []*monitoringpb.CreateMetricDescriptorRequest
}

func createFakeServer(t *testing.T) (*fakeMetricsServer, string, func()) {
func createFakeServerConn(t *testing.T) (*fakeMetricsServer, *grpc.ClientConn, func()) {
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Failed to bind to an available address: %v", err)
Expand All @@ -405,12 +534,15 @@ func createFakeServer(t *testing.T) (*fakeMetricsServer, string, func()) {
go func() {
_ = srv.Serve(ln)
}()
_, serverPortStr, _ := net.SplitHostPort(ln.Addr().String())
conn := createConn(t, "localhost:"+serverPortStr)

stop := func() {
conn.Close()
srv.Stop()
_ = ln.Close()
}
_, serverPortStr, _ := net.SplitHostPort(ln.Addr().String())
return server, "localhost:" + serverPortStr, stop
return server, conn, stop
}

func (server *fakeMetricsServer) forEachStackdriverTimeSeries(fn func(sdt *monitoringpb.CreateTimeSeriesRequest)) {
Expand Down
99 changes: 99 additions & 0 deletions testdata/BuiltInMetrics/inMetrics.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
metric_descriptor: <
name: "googleapis.com/calls"
description: "Number of calls"
unit: "1"
type: CUMULATIVE_INT64
label_keys: <
key: "empty_key"
>
label_keys: <
key: "operation_type"
>
>
timeseries: <
start_timestamp: <
seconds: 1543160298
nanos: 100000090
>
label_values: <
has_value: true
>
label_values: <
value: "value_0"
has_value: true
>
points: <
timestamp: <
seconds: 1543160298
nanos: 100000997
>
int64_value: 1
>
>
---
metric_descriptor: <
name: "kubernetes.io/latency"
description: "Description of latency"
unit: "ms"
type: CUMULATIVE_INT64
label_keys: <
key: "empty_key"
>
label_keys: <
key: "operation_type"
>
>
timeseries: <
start_timestamp: <
seconds: 1543160298
nanos: 100000090
>
label_values: <
has_value: true
>
label_values: <
value: "value_0"
has_value: true
>
points: <
timestamp: <
seconds: 1543160298
nanos: 100000997
>
int64_value: 1
>
>
---
metric_descriptor: <
name: "istio.io/latency"
description: "Description of latency"
unit: "ms"
type: CUMULATIVE_INT64
label_keys: <
key: "empty_key"
>
label_keys: <
key: "operation_type"
>
>
timeseries: <
start_timestamp: <
seconds: 1543160298
nanos: 100000090
>
label_values: <
has_value: true
>
label_values: <
value: "value_0"
has_value: true
>
points: <
timestamp: <
seconds: 1543160298
nanos: 100000997
>
int64_value: 1
>
>

2 changes: 2 additions & 0 deletions testdata/BuiltInMetrics/outMDR.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Empty file required to specify that no CreateMetricDescriptorRequest will be generated.

Loading