github.com/google/cloudprober@v0.11.3/surfacers/stackdriver/stackdriver_test.go (about)

     1  // Copyright 2017-2021 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package stackdriver
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/google/cloudprober/logger"
    23  	"github.com/google/cloudprober/metrics"
    24  	"github.com/kylelemons/godebug/pretty"
    25  	monitoring "google.golang.org/api/monitoring/v3"
    26  )
    27  
    28  var (
    29  	stringVal = "string-test"
    30  	boolVal   = true
    31  	floatVal  = 123.456
    32  	intVal    = float64(123456)
    33  )
    34  
    35  func newTestSurfacer() SDSurfacer {
    36  	l, _ := logger.New(context.TODO(), "test-logger")
    37  	return SDSurfacer{
    38  		cache:       make(map[string]*monitoring.TimeSeries),
    39  		onGCE:       true,
    40  		projectName: "test-project",
    41  		l:           l,
    42  		resource: &monitoring.MonitoredResource{
    43  			Type: "gce_instance",
    44  			Labels: map[string]string{
    45  				"instance_id": "test-instance",
    46  				"zone":        "us-central1-a",
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func TestTimeSeries(t *testing.T) {
    53  	testTimestamp := time.Now()
    54  
    55  	oneVal := float64(1)
    56  
    57  	// Following variables are used for map value testing.
    58  	mapValue200 := float64(98)
    59  	mapValue500 := float64(2)
    60  	mapVal := metrics.NewMap("code", metrics.NewInt(0))
    61  	mapVal.IncKeyBy("200", metrics.NewInt(int64(mapValue200)))
    62  	mapVal.IncKeyBy("500", metrics.NewInt(int64(mapValue500)))
    63  
    64  	tests := []struct {
    65  		description string
    66  		surfacer    SDSurfacer
    67  		em          *metrics.EventMetrics
    68  		timeSeries  []*monitoring.TimeSeries
    69  	}{
    70  		{
    71  			description: "timeseries creation with a non-default float64 value",
    72  			surfacer:    newTestSurfacer(),
    73  			em:          metrics.NewEventMetrics(testTimestamp).AddMetric("test-message", metrics.NewInt(123456)),
    74  			timeSeries: []*monitoring.TimeSeries{
    75  				&monitoring.TimeSeries{
    76  					Metric: &monitoring.Metric{
    77  						Type: "custom.googleapis.com/cloudprober/test-message",
    78  					},
    79  					Resource: &monitoring.MonitoredResource{
    80  						Type: "gce_instance",
    81  						Labels: map[string]string{
    82  							"instance_id": "test-instance",
    83  							"zone":        "us-central1-a",
    84  						},
    85  					},
    86  					MetricKind: "CUMULATIVE",
    87  					ValueType:  "DOUBLE",
    88  					Unit:       "1",
    89  					Points: []*monitoring.Point{
    90  						{
    91  							Interval: &monitoring.TimeInterval{
    92  								StartTime: "0001-01-01T00:00:00Z",
    93  								EndTime:   testTimestamp.Format(time.RFC3339Nano),
    94  							},
    95  							Value: &monitoring.TypedValue{
    96  								DoubleValue: &intVal,
    97  							},
    98  						},
    99  					},
   100  				},
   101  			},
   102  		},
   103  		{
   104  			description: "timeseries creation with a non-default string value and labels",
   105  			surfacer:    newTestSurfacer(),
   106  			em: metrics.NewEventMetrics(testTimestamp).
   107  				AddMetric("version", metrics.NewString("versionXX")).
   108  				AddLabel("keyA", "valueA").
   109  				AddLabel("keyB", "valueB"),
   110  			timeSeries: []*monitoring.TimeSeries{
   111  				&monitoring.TimeSeries{
   112  					Metric: &monitoring.Metric{
   113  						Type: "custom.googleapis.com/cloudprober/version",
   114  						Labels: map[string]string{
   115  							"keyA": "valueA",
   116  							"keyB": "valueB",
   117  							"val":  "versionXX",
   118  						},
   119  					},
   120  					Resource: &monitoring.MonitoredResource{
   121  						Type: "gce_instance",
   122  						Labels: map[string]string{
   123  							"instance_id": "test-instance",
   124  							"zone":        "us-central1-a",
   125  						},
   126  					},
   127  					MetricKind: "CUMULATIVE",
   128  					ValueType:  "DOUBLE",
   129  					Unit:       "1",
   130  					Points: []*monitoring.Point{
   131  						{
   132  							Interval: &monitoring.TimeInterval{
   133  								StartTime: "0001-01-01T00:00:00Z",
   134  								EndTime:   testTimestamp.Format(time.RFC3339Nano),
   135  							},
   136  							Value: &monitoring.TypedValue{
   137  								DoubleValue: &oneVal,
   138  							},
   139  						},
   140  					},
   141  				},
   142  			},
   143  		},
   144  		{
   145  			description: "timeseries creation with a non-default map value and labels",
   146  			surfacer:    newTestSurfacer(),
   147  			em: metrics.NewEventMetrics(testTimestamp).
   148  				AddMetric("version", mapVal).
   149  				AddLabel("keyA", "valueA").
   150  				AddLabel("keyB", "valueB"),
   151  			timeSeries: []*monitoring.TimeSeries{
   152  				&monitoring.TimeSeries{
   153  					Metric: &monitoring.Metric{
   154  						Type: "custom.googleapis.com/cloudprober/version",
   155  						Labels: map[string]string{
   156  							"keyA": "valueA",
   157  							"keyB": "valueB",
   158  							"code": "200",
   159  						},
   160  					},
   161  					Resource: &monitoring.MonitoredResource{
   162  						Type: "gce_instance",
   163  						Labels: map[string]string{
   164  							"instance_id": "test-instance",
   165  							"zone":        "us-central1-a",
   166  						},
   167  					},
   168  					MetricKind: "CUMULATIVE",
   169  					ValueType:  "DOUBLE",
   170  					Unit:       "1",
   171  					Points: []*monitoring.Point{
   172  						{
   173  							Interval: &monitoring.TimeInterval{
   174  								StartTime: "0001-01-01T00:00:00Z",
   175  								EndTime:   testTimestamp.Format(time.RFC3339Nano),
   176  							},
   177  							Value: &monitoring.TypedValue{
   178  								DoubleValue: &mapValue200,
   179  							},
   180  						},
   181  					},
   182  				},
   183  				&monitoring.TimeSeries{
   184  					Metric: &monitoring.Metric{
   185  						Type: "custom.googleapis.com/cloudprober/version",
   186  						Labels: map[string]string{
   187  							"keyA": "valueA",
   188  							"keyB": "valueB",
   189  							"code": "500",
   190  						},
   191  					},
   192  					Resource: &monitoring.MonitoredResource{
   193  						Type: "gce_instance",
   194  						Labels: map[string]string{
   195  							"instance_id": "test-instance",
   196  							"zone":        "us-central1-a",
   197  						},
   198  					},
   199  					MetricKind: "CUMULATIVE",
   200  					ValueType:  "DOUBLE",
   201  					Unit:       "1",
   202  					Points: []*monitoring.Point{
   203  						{
   204  							Interval: &monitoring.TimeInterval{
   205  								StartTime: "0001-01-01T00:00:00Z",
   206  								EndTime:   testTimestamp.Format(time.RFC3339Nano),
   207  							},
   208  							Value: &monitoring.TypedValue{
   209  								DoubleValue: &mapValue500,
   210  							},
   211  						},
   212  					},
   213  				},
   214  			},
   215  		},
   216  	}
   217  	for _, tt := range tests {
   218  		// Generate a time series and check that it is correct
   219  		gotTimeSeries := tt.surfacer.recordEventMetrics(tt.em)
   220  		if diff := pretty.Compare(tt.timeSeries, gotTimeSeries); diff != "" {
   221  			t.Errorf("timeSeries() produced incorrect timeSeries (-want +got):\n%s\ntest description: %s", diff, tt.description)
   222  		}
   223  	}
   224  }