Skip to content

Commit

Permalink
Set options.UserAgent so that the OpenCensus exporter does not overri… (
Browse files Browse the repository at this point in the history
#1620)

* Set options.UserAgent so that the OpenCensus exporter does not override the UA

* Add comments to clarify how user agent settings are used

* Fix comments
  • Loading branch information
james-bebbington committed Nov 17, 2020
1 parent 3761763 commit 098ed60
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions exporter/stackdriverexporter/stackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,23 @@ func (me *metricsExporter) Shutdown(context.Context) error {
return nil
}

func generateClientOptions(cfg *Config, version string) ([]option.ClientOption, error) {
userAgent := strings.ReplaceAll(cfg.UserAgent, "{{version}}", version)
func setVersionInUserAgent(cfg *Config, version string) {
cfg.UserAgent = strings.ReplaceAll(cfg.UserAgent, "{{version}}", version)
}

func generateClientOptions(cfg *Config) ([]option.ClientOption, error) {
var copts []option.ClientOption
if userAgent != "" {
copts = append(copts, option.WithUserAgent(userAgent))
// option.WithUserAgent is used by the Trace exporter, but not the Metric exporter (see comment below)
if cfg.UserAgent != "" {
copts = append(copts, option.WithUserAgent(cfg.UserAgent))
}
if cfg.Endpoint != "" {
if cfg.UseInsecure {
// WithGRPCConn option takes precedent over all other supplied options so need to provide user agent here as well
// option.WithGRPCConn option takes precedent over all other supplied options so the
// following user agent will be used by both exporters if we reach this branch
var dialOpts []grpc.DialOption
if userAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(userAgent))
if cfg.UserAgent != "" {
dialOpts = append(dialOpts, grpc.WithUserAgent(cfg.UserAgent))
}
conn, err := grpc.Dial(cfg.Endpoint, append(dialOpts, grpc.WithInsecure())...)
if err != nil {
Expand All @@ -94,12 +99,14 @@ func generateClientOptions(cfg *Config, version string) ([]option.ClientOption,
}

func newStackdriverTraceExporter(cfg *Config, params component.ExporterCreateParams) (component.TracesExporter, error) {
setVersionInUserAgent(cfg, params.ApplicationStartInfo.Version)

topts := []cloudtrace.Option{
cloudtrace.WithProjectID(cfg.ProjectID),
cloudtrace.WithTimeout(cfg.Timeout),
}

copts, err := generateClientOptions(cfg, params.ApplicationStartInfo.Version)
copts, err := generateClientOptions(cfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -184,6 +191,8 @@ func validateAndAppendDurationOption(topts []cloudtrace.Option, name string, val
}

func newStackdriverMetricsExporter(cfg *Config, params component.ExporterCreateParams) (component.MetricsExporter, error) {
setVersionInUserAgent(cfg, params.ApplicationStartInfo.Version)

// TODO: For each ProjectID, create a different exporter
// or at least a unique Stackdriver client per ProjectID.
options := stackdriver.Options{
Expand All @@ -199,7 +208,12 @@ func newStackdriverMetricsExporter(cfg *Config, params component.ExporterCreateP
Timeout: cfg.Timeout,
}

copts, err := generateClientOptions(cfg, params.ApplicationStartInfo.Version)
// note options.UserAgent overrides the option.WithUserAgent client option in the Metric exporter
if cfg.UserAgent != "" {
options.UserAgent = cfg.UserAgent
}

copts, err := generateClientOptions(cfg)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 098ed60

Please sign in to comment.