Skip to content

Commit 213d97f

Browse files
committed
feat: add support for Cloud Monitoring and Cloud Trace (#1212)
Users may now enable metrics and traces from the Go connector by supplying a "telemetry-project" and project ID. Optionally, users may also set a metrics prefix. Separately, this commit separates metrics and tracing, allowing each to be enabled on their own. In addition, it adds support for configuring the default probabilistic sampling rate and otherwise uses the default where 1/10,000 calls will be traced.
1 parent 3977f38 commit 213d97f

File tree

3 files changed

+1047
-17
lines changed

3 files changed

+1047
-17
lines changed

cmd/root.go

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ import (
3131

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

@@ -67,8 +69,13 @@ type Command struct {
6769
*cobra.Command
6870
conf *proxy.Config
6971

70-
prometheusNamespace string
71-
httpPort string
72+
disableTraces bool
73+
telemetryTracingSampleRate int
74+
disableMetrics bool
75+
telemetryProject string
76+
telemetryPrefix string
77+
prometheusNamespace string
78+
httpPort string
7279
}
7380

7481
// Option is a function that configures a Command.
@@ -120,6 +127,16 @@ any client SSL certificates.`,
120127
"Path to a service account key to use for authentication.")
121128
cmd.PersistentFlags().BoolVarP(&c.conf.GcloudAuth, "gcloud-auth", "g", false,
122129
"Use gcloud's user configuration to retrieve a token for authentication.")
130+
cmd.PersistentFlags().StringVar(&c.telemetryProject, "telemetry-project", "",
131+
"Enable Cloud Monitoring and Cloud Trace integration with the provided project ID.")
132+
cmd.PersistentFlags().BoolVar(&c.disableTraces, "disable-traces", false,
133+
"Disable Cloud Trace integration (used with telemetry-project)")
134+
cmd.PersistentFlags().IntVar(&c.telemetryTracingSampleRate, "telemetry-sample-rate", 10_000,
135+
"Configure the denominator of the probabilistic sample rate of traces sent to Cloud Trace\n(e.g., 10,000 traces 1/10,000 calls).")
136+
cmd.PersistentFlags().BoolVar(&c.disableMetrics, "disable-metrics", false,
137+
"Disable Cloud Monitoring integration (used with telemetry-project)")
138+
cmd.PersistentFlags().StringVar(&c.telemetryPrefix, "telemetry-prefix", "",
139+
"Prefix to use for Cloud Monitoring metrics.")
123140
cmd.PersistentFlags().StringVar(&c.prometheusNamespace, "prometheus-namespace", "",
124141
"Enable Prometheus for metric collection using the provided namespace")
125142
cmd.PersistentFlags().StringVar(&c.httpPort, "http-port", "9090",
@@ -196,6 +213,16 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
196213
return newBadCommandError("cannot specify --http-port without --prometheus-namespace")
197214
}
198215

216+
if !userHasSet("telemetry-project") && userHasSet("telemetry-prefix") {
217+
cmd.Println("Ignoring telementry-prefix as telemetry-project was not set")
218+
}
219+
if !userHasSet("telemetry-project") && userHasSet("disable-metrics") {
220+
cmd.Println("Ignoring disable-metrics as telemetry-project was not set")
221+
}
222+
if !userHasSet("telemetry-project") && userHasSet("disable-traces") {
223+
cmd.Println("Ignoring disable-traces as telemetry-project was not set")
224+
}
225+
199226
var ics []proxy.InstanceConnConfig
200227
for _, a := range args {
201228
// Assume no query params initially
@@ -268,6 +295,36 @@ func runSignalWrapper(cmd *Command) error {
268295
ctx, cancel := context.WithCancel(cmd.Context())
269296
defer cancel()
270297

298+
// Configure Cloud Trace and/or Cloud Monitoring based on command
299+
// invocation. If a project has not been enabled, no traces or metrics are
300+
// enabled.
301+
enableMetrics := !cmd.disableMetrics
302+
enableTraces := !cmd.disableTraces
303+
if cmd.telemetryProject != "" && (enableMetrics || enableTraces) {
304+
sd, err := stackdriver.NewExporter(stackdriver.Options{
305+
ProjectID: cmd.telemetryProject,
306+
MetricPrefix: cmd.telemetryPrefix,
307+
})
308+
if err != nil {
309+
return err
310+
}
311+
if enableMetrics {
312+
err = sd.StartMetricsExporter()
313+
if err != nil {
314+
return err
315+
}
316+
}
317+
if enableTraces {
318+
s := trace.ProbabilitySampler(1 / float64(cmd.telemetryTracingSampleRate))
319+
trace.ApplyConfig(trace.Config{DefaultSampler: s})
320+
trace.RegisterExporter(sd)
321+
}
322+
defer func() {
323+
sd.Flush()
324+
sd.StopMetricsExporter()
325+
}()
326+
}
327+
271328
shutdownCh := make(chan error)
272329

273330
if cmd.prometheusNamespace != "" {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
cloud.google.com/go/cloudsqlconn v0.2.1-0.20220401153611-87e713b37755
77
cloud.google.com/go/compute v1.9.0
88
contrib.go.opencensus.io/exporter/prometheus v0.4.1
9+
contrib.go.opencensus.io/exporter/stackdriver v0.13.13
910
github.com/GoogleCloudPlatform/cloudsql-proxy v1.29.0
1011
github.com/coreos/go-systemd/v22 v22.3.2
1112
github.com/denisenkom/go-mssqldb v0.12.2
@@ -15,6 +16,7 @@ require (
1516
github.com/jackc/pgx/v4 v4.17.0
1617
github.com/lib/pq v1.10.6
1718
github.com/spf13/cobra v1.2.1
19+
go.opencensus.io v0.23.0
1820
go.uber.org/zap v1.22.0
1921
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
2022
golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094

0 commit comments

Comments
 (0)