grpc/grpc-go · error

failed to create Stackdriver exporter

Error message

failed to create Stackdriver exporter: %v

What it means

newStackdriverExporter failed to construct the OpenCensus Stackdriver exporter. The wrapped %v comes from stackdriver.NewExporter, which dials monitoring.googleapis.com and cloudtrace.googleapis.com using ADC and the configured project. Emitted at opencensus.go:105; propagates up through error 254.

Solutions

  1. Enable Cloud Monitoring and Cloud Trace APIs on config.ProjectID.
  2. Grant the SA roles/monitoring.metricWriter and roles/cloudtrace.agent.
  3. Provide ADC: `gcloud auth application-default login` or set GOOGLE_APPLICATION_CREDENTIALS.
  4. On GCE/GKE, ensure the node service account has monitoring.write and trace.append scopes.
  5. Verify egress to monitoring.googleapis.com / cloudtrace.googleapis.com (or configure Private Service Connect).

Example fix

# before
$ gcloud services enable monitoring.googleapis.com trace.googleapis.com --project=my-proj  # forgot

# after
$ gcloud services enable monitoring.googleapis.com trace.googleapis.com --project=my-proj
$ gcloud auth application-default login
$ ./svc
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-flight: build (and close) a Stackdriver exporter with the same config.
import "contrib.go.opencensus.io/exporter/stackdriver"

func probeStackdriver(project string) error {
    e, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: project})
    if err != nil { return err }
    e.Flush()
    return e.Close()
}

if err := probeStackdriver(projectID); err != nil { return err }

Try / catch

if err := observability.Start(ctx); err != nil {
    if strings.Contains(err.Error(), "failed to create Stackdriver exporter") {
        log.Fatalf("Stackdriver exporter creation failed (ADC/API/scopes/network): %v", err)
    }
    return err
}

Prevention

When it happens

Trigger: Config has cloud_trace or cloud_monitoring and stackdriver.NewExporter fails: missing/invalid ADC, project id wrong, Stackdriver Monitoring or Trace API disabled in the project, missing monitoring write scope on the instance SA, or network blocked to googleapis.

Common situations: On-prem/CI without ADC; GCE instance created without the monitoring or trace.write scope; Stackdriver APIs not enabled; SA key rotated; private VPC without Private Access to Google APIs; cross-project export where the SA lacks roles/monitoring.metricWriter or roles/cloudtrace.agent.

Related errors


AI-assisted analysis of grpc/grpc-go@0c51461d27 (2026-08-11). Data as JSON: /api/errors/4fed3b4d256444a7. Report an issue: GitHub.

Appendix: source

Thrown at gcp/observability/opencensus.go:105

	// Custom labels completely overwrite any labels generated in the OpenCensus
	// library, including their label that uniquely identifies the process.
	// Thus, generate a unique process identifier here to uniquely identify
	// process for metrics exporting to function correctly.
	metricsLabels := make(map[string]string, len(config.Labels)+1)
	for k, v := range config.Labels {
		metricsLabels[k] = v
	}
	metricsLabels["opencensus_task"] = generateUniqueProcessIdentifier()
	exporter, err := stackdriver.NewExporter(stackdriver.Options{
		ProjectID:               config.ProjectID,
		MonitoredResource:       mr,
		DefaultMonitoringLabels: labelsToMonitoringLabels(metricsLabels),
		DefaultTraceAttributes:  labelsToTraceAttributes(config.Labels),
		MonitoringClientOptions: cOptsDisableLogTrace,
		TraceClientOptions:      cOptsDisableLogTrace,
	})
	if err != nil {
		return nil, fmt.Errorf("failed to create Stackdriver exporter: %v", err)
	}
	return exporter, nil
}

// generateUniqueProcessIdentifier returns a unique process identifier for the
// process this code is running in. This is the same way the OpenCensus library
// generates the unique process identifier, in the format of
// "go-<pid>@<hostname>".
func generateUniqueProcessIdentifier() string {
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "localhost"
	}
	return "go-" + strconv.Itoa(os.Getpid()) + "@" + hostname
}

// This method accepts config and exporter; the exporter argument is exposed to
// assist unit testing of the OpenCensus behavior.

View on GitHub (pinned to 0c51461d27)