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