func defaultNewMetricClient()

in pkg/export/export.go [321:374]


func defaultNewMetricClient(ctx context.Context, opts ExporterOpts) (metricServiceClient, error) {
	version, err := Version()
	if err != nil {
		return nil, fmt.Errorf("unable to fetch user agent version: %w", err)
	}

	// Identity User Agent for all gRPC requests.
	ua := strings.TrimSpace(fmt.Sprintf("%s/%s %s (env:%s;mode:%s)",
		ClientName, version, opts.UserAgentProduct, opts.UserAgentEnv, opts.UserAgentMode))

	clientOpts := []option.ClientOption{
		option.WithGRPCDialOption(grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor)),
		option.WithUserAgent(ua),
	}
	if opts.Endpoint != "" {
		clientOpts = append(clientOpts, option.WithEndpoint(opts.Endpoint))
	}
	// Disable auth when the exporter is disabled because we don't want a panic when default
	// credentials are not found.
	if opts.DisableAuth || opts.Disable {
		clientOpts = append(clientOpts,
			option.WithoutAuthentication(),
			option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
		)
	} else if opts.CredentialsFile == "" && len(opts.CredentialsFromJSON) == 0 {
		// If no credentials are found, gRPC panics so we check manually.
		_, err := google.FindDefaultCredentials(ctx)
		if err != nil {
			return nil, err
		}
	}
	if opts.CredentialsFile != "" {
		clientOpts = append(clientOpts, option.WithCredentialsFile(opts.CredentialsFile))
	} else if len(opts.CredentialsFromJSON) > 0 {
		clientOpts = append(clientOpts, option.WithCredentialsJSON(opts.CredentialsFromJSON))
	}

	if opts.TokenURL != "" && opts.TokenBody != "" {
		tokenSource := NewAltTokenSource(opts.TokenURL, opts.TokenBody)
		clientOpts = append(clientOpts, option.WithTokenSource(tokenSource))
	}
	if opts.QuotaProject != "" {
		clientOpts = append(clientOpts, option.WithQuotaProject(opts.QuotaProject))
	}
	client, err := monitoring.NewMetricClient(ctx, clientOpts...)
	if err != nil {
		return nil, err
	}
	if opts.Compression == CompressionGZIP {
		client.CallOptions.CreateTimeSeries = append(client.CallOptions.CreateTimeSeries,
			gax.WithGRPCOptions(grpc.UseCompressor(gzip.Name)))
	}
	return client, nil
}