@@ -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 != "" {
0 commit comments