@@ -31,10 +31,12 @@ import (
31
31
32
32
"cloud.google.com/go/alloydbconn"
33
33
"contrib.go.opencensus.io/exporter/prometheus"
34
+ "contrib.go.opencensus.io/exporter/stackdriver"
34
35
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/alloydb"
35
36
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/gcloud"
36
37
"github.com/GoogleCloudPlatform/alloydb-auth-proxy/internal/proxy"
37
38
"github.com/spf13/cobra"
39
+ "go.opencensus.io/trace"
38
40
"golang.org/x/oauth2"
39
41
)
40
42
@@ -79,8 +81,13 @@ type Command struct {
79
81
* cobra.Command
80
82
conf * proxy.Config
81
83
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
84
91
}
85
92
86
93
// Option is a function that configures a Command.
@@ -132,6 +139,16 @@ without having to manage any client SSL certificates.`,
132
139
"Path to a service account key to use for authentication." )
133
140
cmd .PersistentFlags ().BoolVarP (& c .conf .GcloudAuth , "gcloud-auth" , "g" , false ,
134
141
"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." )
135
152
cmd .PersistentFlags ().StringVar (& c .prometheusNamespace , "prometheus-namespace" , "" ,
136
153
"Enable Prometheus for metric collection using the provided namespace" )
137
154
cmd .PersistentFlags ().StringVar (& c .httpPort , "http-port" , "9090" ,
@@ -208,6 +225,16 @@ func parseConfig(cmd *cobra.Command, conf *proxy.Config, args []string) error {
208
225
return newBadCommandError ("cannot specify --http-port without --prometheus-namespace" )
209
226
}
210
227
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
+
211
238
var ics []proxy.InstanceConnConfig
212
239
for _ , a := range args {
213
240
// Assume no query params initially
@@ -280,6 +307,36 @@ func runSignalWrapper(cmd *Command) error {
280
307
ctx , cancel := context .WithCancel (cmd .Context ())
281
308
defer cancel ()
282
309
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
+
283
340
shutdownCh := make (chan error )
284
341
285
342
if cmd .prometheusNamespace != "" {
0 commit comments