Skip to content

Commit b928921

Browse files
authored
feat: add support for Cloud Monitoring and Cloud Trace (GoogleCloudPlatform#60)
This is a port of GoogleCloudPlatform/cloud-sql-proxy#1212
1 parent b6fcbf3 commit b928921

File tree

3 files changed

+1128
-16
lines changed

3 files changed

+1128
-16
lines changed

cmd/root.go

+59-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ import (
3131

3232
"cloud.google.com/go/alloydbconn"
3333
"contrib.go.opencensus.io/exporter/prometheus"
34+
"contrib.go.opencensus.io/exporter/stackdriver"
3435
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/alloydb"
3536
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/gcloud"
3637
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/proxy"
3738
"github.com/spf13/cobra"
39+
"go.opencensus.io/trace"
3840
"golang.org/x/oauth2"
3941
)
4042

@@ -79,8 +81,13 @@ type Command struct {
7981
*cobra.Command
8082
conf *proxy.Config
8183

82-
prometheusNamespace string
83-
httpPort string
84+
disableTraces bool
85+
telemetryTracingSampleRate int
86+
disableMetrics bool
87+
telemetryProject string
88+
telemetryPrefix string
89+
prometheusNamespace string
90+
httpPort string
8491
}
8592

8693
// Option is a function that configures a Command.
@@ -132,6 +139,16 @@ without having to manage any client SSL certificates.`,
132139
"Path to a service account key to use for authentication.")
133140
cmd.PersistentFlags().BoolVarP(&c.conf.GcloudAuth, "gcloud-auth", "g", false,
134141
"Use gcloud's user configuration to retrieve a token for authentication.")
142+
cmd.PersistentFlags().StringVar(&c.telemetryProject, "telemetry-project", "",
143+
"Enable Cloud Monitoring and Cloud Trace integration with the provided project ID.")
144+
cmd.PersistentFlags().BoolVar(&c.disableTraces, "disable-traces", false,
145+
"Disable Cloud Trace integration (used with telemetry-project)")
146+
cmd.PersistentFlags().IntVar(&c.telemetryTracingSampleRate, "telemetry-sample-rate", 10_000,
147+
"Configure the denominator of the probabilistic sample rate of traces sent to Cloud Trace\n(e.g., 10,000 traces 1/10,000 calls).")
148+
cmd.PersistentFlags().BoolVar(&c.disableMetrics, "disable-metrics", false,
149+
"Disable Cloud Monitoring integration (used with telemetry-project)")
150+
cmd.PersistentFlags().StringVar(&c.telemetryPrefix, "telemetry-prefix", "",
151+
"Prefix to use for Cloud Monitoring metrics.")
135152
cmd.PersistentFlags().StringVar(&c.prometheusNamespace, "prometheus-namespace", "",
136153
"Enable Prometheus for metric collection using the provided namespace")
137154
cmd.PersistentFlags().StringVar(&c.httpPort, "http-port", "9090",
@@ -208,6 +225,16 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
208225
return newBadCommandError("cannot specify --http-port without --prometheus-namespace")
209226
}
210227

228+
if !userHasSet("telemetry-project") && userHasSet("telemetry-prefix") {
229+
cmd.Println("Ignoring telementry-prefix as telemetry-project was not set")
230+
}
231+
if !userHasSet("telemetry-project") && userHasSet("disable-metrics") {
232+
cmd.Println("Ignoring disable-metrics as telemetry-project was not set")
233+
}
234+
if !userHasSet("telemetry-project") && userHasSet("disable-traces") {
235+
cmd.Println("Ignoring disable-traces as telemetry-project was not set")
236+
}
237+
211238
var ics []proxy.InstanceConnConfig
212239
for _, a := range args {
213240
// Assume no query params initially
@@ -280,6 +307,36 @@ func runSignalWrapper(cmd *Command) error {
280307
ctx, cancel := context.WithCancel(cmd.Context())
281308
defer cancel()
282309

310+
// Configure Cloud Trace and/or Cloud Monitoring based on command
311+
// invocation. If a project has not been enabled, no traces or metrics are
312+
// enabled.
313+
enableMetrics := !cmd.disableMetrics
314+
enableTraces := !cmd.disableTraces
315+
if cmd.telemetryProject != "" && (enableMetrics || enableTraces) {
316+
sd, err := stackdriver.NewExporter(stackdriver.Options{
317+
ProjectID: cmd.telemetryProject,
318+
MetricPrefix: cmd.telemetryPrefix,
319+
})
320+
if err != nil {
321+
return err
322+
}
323+
if enableMetrics {
324+
err = sd.StartMetricsExporter()
325+
if err != nil {
326+
return err
327+
}
328+
}
329+
if enableTraces {
330+
s := trace.ProbabilitySampler(1 / float64(cmd.telemetryTracingSampleRate))
331+
trace.ApplyConfig(trace.Config{DefaultSampler: s})
332+
trace.RegisterExporter(sd)
333+
}
334+
defer func() {
335+
sd.Flush()
336+
sd.StopMetricsExporter()
337+
}()
338+
}
339+
283340
shutdownCh := make(chan error)
284341

285342
if cmd.prometheusNamespace != "" {

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ go 1.16
55
require (
66
cloud.google.com/go/alloydbconn v0.1.1
77
contrib.go.opencensus.io/exporter/prometheus v0.4.1
8+
contrib.go.opencensus.io/exporter/stackdriver v0.13.13
89
github.com/google/go-cmp v0.5.8
910
github.com/lib/pq v1.10.5 // indirect
1011
github.com/spf13/cobra v1.5.0
12+
go.opencensus.io v0.23.0
1113
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87 // indirect
1214
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2
1315
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e

0 commit comments

Comments
 (0)