Skip to content
This repository was archived by the owner on Dec 3, 2024. It is now read-only.

Commit 12f414d

Browse files
authored
Merge pull request #10 from livesession/feat/custom-monitored-resource
Custom monitored resources
2 parents 132b04a + 1b92e0d commit 12f414d

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

stackdriver.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Sink struct {
5555
prefix string
5656
taskInfo *taskInfo
5757

58+
monitoredResource *monitoredrespb.MonitoredResource
59+
5860
mu sync.Mutex
5961
debugLogs bool
6062
}
@@ -104,6 +106,13 @@ type Config struct {
104106
// will log additional information that is helpful when debugging errors.
105107
// Optional. Defaults to false.
106108
DebugLogs bool
109+
110+
// MonitoredResource identifies the machine/service/resource that is monitored.
111+
// Different possible settings are defined here:
112+
// https://cloud.google.com/monitoring/api/resources
113+
//
114+
// Setting a nil MonitoredResource will run a defaultMonitoredResource function.
115+
MonitoredResource *monitoredrespb.MonitoredResource
107116
}
108117

109118
type taskInfo struct {
@@ -123,6 +132,20 @@ type BucketFn func([]string) []float64
123132
// writing to stackdriver.
124133
type ExtractLabelsFn func([]string, string) ([]string, []metrics.Label, error)
125134

135+
// defaultMonitoredResource returns default monitored resource
136+
func defaultMonitoredResource(taskInfo *taskInfo) *monitoredrespb.MonitoredResource {
137+
return &monitoredrespb.MonitoredResource{
138+
Type: "generic_task",
139+
Labels: map[string]string{
140+
"project_id": taskInfo.ProjectID,
141+
"location": taskInfo.Location,
142+
"namespace": taskInfo.Namespace,
143+
"job": taskInfo.Job,
144+
"task_id": taskInfo.TaskID,
145+
},
146+
}
147+
}
148+
126149
// DefaultBucketer is the default BucketFn used to determing bucketing values
127150
// for metrics.
128151
func DefaultBucketer(key []string) []float64 {
@@ -211,6 +234,12 @@ func NewSink(client *monitoring.MetricClient, config *Config) *Sink {
211234
s.taskInfo.TaskID = "go-" + strconv.Itoa(os.Getpid()) + "@" + hostname
212235
}
213236

237+
if config.MonitoredResource != nil {
238+
s.monitoredResource = config.MonitoredResource
239+
} else {
240+
s.monitoredResource = defaultMonitoredResource(s.taskInfo)
241+
}
242+
214243
s.reset()
215244

216245
// run cancelable goroutine that reports on interval
@@ -291,16 +320,7 @@ func (s *Sink) report(ctx context.Context) {
291320
end, rGauges, rCounters, rHistograms := s.deep()
292321

293322
// https://cloud.google.com/monitoring/api/resources
294-
resource := &monitoredrespb.MonitoredResource{
295-
Type: "generic_task",
296-
Labels: map[string]string{
297-
"project_id": s.taskInfo.ProjectID,
298-
"location": s.taskInfo.Location,
299-
"namespace": s.taskInfo.Namespace,
300-
"job": s.taskInfo.Job,
301-
"task_id": s.taskInfo.TaskID,
302-
},
303-
}
323+
resource := s.monitoredResource
304324

305325
ts := []*monitoringpb.TimeSeries{}
306326

stackdriver_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
distributionpb "google.golang.org/genproto/googleapis/api/distribution"
3131
"google.golang.org/genproto/googleapis/api/metric"
3232
metricpb "google.golang.org/genproto/googleapis/api/metric"
33+
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
3334
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
3435
"google.golang.org/grpc"
3536
"google.golang.org/grpc/test/bufconn"
@@ -91,6 +92,12 @@ func BenchmarkReport10(b *testing.B) { benchmarkCopy(10, 10, 10, b) }
9192
func BenchmarkReport50(b *testing.B) { benchmarkCopy(50, 50, 50, b) }
9293
func BenchmarkReport100(b *testing.B) { benchmarkCopy(100, 100, 100, b) }
9394

95+
func diffTest(t *testing.T, title string, x, y interface{}) {
96+
if diff := cmp.Diff(x, y); diff != "" {
97+
t.Errorf("%s mismatch (-want +got):\n%s", title, diff)
98+
}
99+
}
100+
94101
func sPtr(s string) *string {
95102
return &s
96103
}
@@ -1091,3 +1098,68 @@ func diffCreateMsg(want, got *monitoringpb.CreateTimeSeriesRequest) string {
10911098
}
10921099
return out
10931100
}
1101+
1102+
func monitoredResourceDiff(t *testing.T, s *Sink, labels map[string]string) {
1103+
diffTest(t, "Monitored Resource labels", s.monitoredResource.GetLabels(), labels)
1104+
}
1105+
1106+
func TestCustomMonitorResource(t *testing.T) {
1107+
labels := map[string]string{
1108+
"project_id": "project",
1109+
"location": "zone",
1110+
"cluster_name": "cluster",
1111+
"container_name": "container_name",
1112+
"namespace_name": "namespace_name",
1113+
"pod_name": "pod_name",
1114+
}
1115+
1116+
sink := NewSink(nil, &Config{
1117+
ProjectID: "example_project",
1118+
Prefix: sPtr(""),
1119+
MonitoredResource: &monitoredrespb.MonitoredResource{
1120+
Labels: labels,
1121+
Type: "k8s_container",
1122+
},
1123+
})
1124+
1125+
monitoredResourceDiff(t, sink, labels)
1126+
}
1127+
1128+
func TestCustomMonitorResourceWithDefaultLabels(t *testing.T) {
1129+
sink := NewSink(nil, &Config{
1130+
ProjectID: "example_project",
1131+
Prefix: sPtr(""),
1132+
})
1133+
1134+
labels := defaultMonitoredResource(sink.taskInfo).GetLabels()
1135+
1136+
monitoredResourceDiff(t, sink, labels)
1137+
}
1138+
1139+
func TestCustomMonitorResourceWithInvalidLabels(t *testing.T) {
1140+
labels := map[string]string{
1141+
"project_id": "project",
1142+
"location": "zone",
1143+
"cluster_name": "cluster",
1144+
"container_name": "container_name",
1145+
"namespace_name": "namespace_name",
1146+
"pod_name": "pod_name",
1147+
}
1148+
1149+
invalidLabels := map[string]string{
1150+
"project_id": "project",
1151+
}
1152+
1153+
sink := NewSink(nil, &Config{
1154+
ProjectID: "example_project",
1155+
Prefix: sPtr(""),
1156+
MonitoredResource: &monitoredrespb.MonitoredResource{
1157+
Labels: labels,
1158+
Type: "k8s_container",
1159+
},
1160+
})
1161+
1162+
if diff := cmp.Diff(sink.monitoredResource.GetLabels(), invalidLabels); diff == "" {
1163+
t.Error("Monitored Resource labels should not be equal")
1164+
}
1165+
}

0 commit comments

Comments
 (0)