googleinterns / cloud-operations-api-mock

Apache License 2.0
5 stars 2 forks source link

Crash during CreateTimeSeries without a resource #57

Open aabmass opened 3 years ago

aabmass commented 3 years ago

Mock server crashes when sending the following request without a Resource:

'projects/TEST-PROJECT', [metric {
  labels {
    key: "env"
    value: "prod"
  }
  type: "custom.googleapis.com/OpenTelemetry/opentelemetry/name"
}
points {
  interval {
    start_time {
      seconds: 1603470463
      nanos: 275206324
    }
    end_time {
      seconds: 12
    }
  }
  value {
    int64_value: 1
  }
}
]

repro

Invoke CreateTimeSeries:

$ grpc_cli_local call localhost:8080 google.monitoring.v3.MetricService.CreateTimeSeries 'name: "projects/TEST-PROJECT", time_series: [{metric: { labels: { key: "env", value: "prod" }, type: "custom.googleapis.com/OpenTelem etry/opentelemetry/name" }, points: { interval: { start_time: { seconds: 1603470463, nanos: 275206324 }, end_time: { seconds: 12 } }, value: { int64_value: 1 }}}]'

Output:

$ ./mock_server |& tee log.txt
2020/10/23 16:35:01 Listening on 127.0.0.1:8080
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x9939aa]

goroutine 6 [running]:
github.com/googleinterns/cloud-operations-api-mock/internal/validation.serializeTimeSeries(0xc0000a39e0, 0x0, 0x0)
        /usr/local/google/home/aaronabbott/repo/cloud-operations-api-mock/internal/validation/mock_metric_validation.go:475 +0x4aa
github.com/googleinterns/cloud-operations-api-mock/internal/validation.ValidateRateLimit(0xc0004a6180, 0x1, 0x1, 0xc0002b4480, 0xc0003c9b30, 0x556765)
        /usr/local/google/home/aaronabbott/repo/cloud-operations-api-mock/internal/validation/mock_metric_validation.go:267 +0x53
github.com/googleinterns/cloud-operations-api-mock/server/metric.(*MockMetricServer).CreateTimeSeries(0xc000225920, 0xb5f9c0, 0xc000533e30, 0xc00037e230, 0x0, 0x0, 0x0)
        /usr/local/google/home/aaronabbott/repo/cloud-operations-api-mock/server/metric/mock_metric.go:157 +0xb8
google.golang.org/genproto/googleapis/monitoring/v3._MetricService_CreateTimeSeries_Handler(0xa895a0, 0xc000225920, 0xb5f9c0, 0xc000533e30, 0xc0000a2480, 0x0, 0xb5f9c0, 0xc000533e30, 0xc00012e000, 0x7b)
        /usr/local/google/home/aaronabbott/go/pkg/mod/google.golang.org/genproto@v0.0.0-20200605102947-12044bf5ea91/googleapis/monitoring/v3/metric_service.pb.go:2309 +0x214
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000248340, 0xb63de0, 0xc000352600, 0xc00049ab00, 0xc0002b4540, 0xef1ac8, 0x0, 0x0, 0x0)
        /usr/local/google/home/aaronabbott/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1082 +0x522
google.golang.org/grpc.(*Server).handleStream(0xc000248340, 0xb63de0, 0xc000352600, 0xc00049ab00, 0x0)
        /usr/local/google/home/aaronabbott/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:1405 +0xcc5
google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc00039c040, 0xc000248340, 0xb63de0, 0xc000352600, 0xc00049ab00)
        /usr/local/google/home/aaronabbott/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:746 +0xa5
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /usr/local/google/home/aaronabbott/go/pkg/mod/google.golang.org/grpc@v1.29.1/server.go:744 +0xa5
aabmass commented 3 years ago

Fix:

diff --git a/internal/validation/mock_metric_validation.go b/internal/validation/mock_metric_validation.go
index 61c65d6..0ddc4ea 100644
--- a/internal/validation/mock_metric_validation.go
+++ b/internal/validation/mock_metric_validation.go
@@ -472,12 +472,14 @@ func serializeTimeSeries(ts *monitoring.TimeSeries) string {
        }

        // Add resource type.
-       result.WriteString("-" + ts.Resource.Type + "-")
+       if ts.Resource != nil {
+               result.WriteString("-" + ts.Resource.Type + "-")

-       // Add resource labels.
-       for k, v := range ts.Resource.Labels {
-               result.WriteString(k + " ")
-               result.WriteString(v + " ")
+               // Add resource labels.
+               for k, v := range ts.Resource.Labels {
+                       result.WriteString(k + " ")
+                       result.WriteString(v + " ")
+               }
        }

        return result.String()