grpc/grpc-go · error

failed to create Stackdriver exporter: %v

Error message

failed to create Stackdriver exporter: %v

What it means

Returned by newStackdriverExporter when stackdriver.NewExporter returns a non-nil error. The exporter is the shared trace+metrics sink, so this blocks all OpenCensus instrumentation. The error wraps the underlying Stackdriver cause (auth, quota, options validation, project ID).

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 03255a9237)

Solutions

  1. Confirm GOOGLE_APPLICATION_CREDENTIALS points to a valid key file or that a metadata server is reachable.
  2. Verify config.ProjectID matches an enabled Stackdriver/Monitoring project; enable the API with `gcloud services enable monitoring.googleapis.com`.
  3. On GCE/GKE, attach the https://www.googleapis.com/auth/monitoring and .../trace.append scopes.
  4. Reduce or validate custom Labels to stay within Stackdriver label limits.

Example fix

// before
exporter, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: ""})
// after
exporter, err := stackdriver.NewExporter(stackdriver.Options{
    ProjectID: "my-valid-project", // plus ADC in env
})
Defensive patterns

Strategy: validation

Validate before calling

// Validate project ID and credentials presence before relying on the exporter.
import "cloud.google.com/go/compute/metadata"
if project == "" && !metadata.OnGCE() {
    if os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" {
        return errors.New("no ADC: set GOOGLE_APPLICATION_CREDENTIALS or run on GCE")
    }
}

Try / catch

exporter, err := newStackdriverExporter(config)
if err != nil {
    return fmt.Errorf("cannot build Stackdriver exporter (check project/ADC): %w", err)
}

Prevention

When it happens

Trigger: Constructing the exporter with an empty or invalid ProjectID, missing Application Default Credentials, an unreachable Stackdriver/Monitoring API, or options that fail client-side validation (e.g. invalid DefaultMonitoringLabels).

Common situations: Workstation without ADC; misconfigured project ID; running on GCE/GKE without the cloud-platform scope; Stackdriver API disabled in the project; exceeding metrics custom-label limits.

Related errors


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