{"record":{"id":"4fed3b4d256444a7","repo":"grpc/grpc-go","slug":"failed-to-create-stackdriver-exporter-v","errorCode":null,"errorMessage":"failed to create Stackdriver exporter: %v","messagePattern":"failed to create Stackdriver exporter: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gcp/observability/opencensus.go","lineNumber":105,"sourceCode":"\t// Custom labels completely overwrite any labels generated in the OpenCensus\n\t// library, including their label that uniquely identifies the process.\n\t// Thus, generate a unique process identifier here to uniquely identify\n\t// process for metrics exporting to function correctly.\n\tmetricsLabels := make(map[string]string, len(config.Labels)+1)\n\tfor k, v := range config.Labels {\n\t\tmetricsLabels[k] = v\n\t}\n\tmetricsLabels[\"opencensus_task\"] = generateUniqueProcessIdentifier()\n\texporter, err := stackdriver.NewExporter(stackdriver.Options{\n\t\tProjectID:               config.ProjectID,\n\t\tMonitoredResource:       mr,\n\t\tDefaultMonitoringLabels: labelsToMonitoringLabels(metricsLabels),\n\t\tDefaultTraceAttributes:  labelsToTraceAttributes(config.Labels),\n\t\tMonitoringClientOptions: cOptsDisableLogTrace,\n\t\tTraceClientOptions:      cOptsDisableLogTrace,\n\t})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create Stackdriver exporter: %v\", err)\n\t}\n\treturn exporter, nil\n}\n\n// generateUniqueProcessIdentifier returns a unique process identifier for the\n// process this code is running in. This is the same way the OpenCensus library\n// generates the unique process identifier, in the format of\n// \"go-<pid>@<hostname>\".\nfunc generateUniqueProcessIdentifier() string {\n\thostname, err := os.Hostname()\n\tif err != nil {\n\t\thostname = \"localhost\"\n\t}\n\treturn \"go-\" + strconv.Itoa(os.Getpid()) + \"@\" + hostname\n}\n\n// This method accepts config and exporter; the exporter argument is exposed to\n// assist unit testing of the OpenCensus behavior.","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/grpc/grpc-go/blob/0c51461d27177d997e14c642fe18c11668fc09a3/gcp/observability/opencensus.go#L87-L123","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Enable Cloud Monitoring and Cloud Trace APIs on config.ProjectID.","Grant the SA roles/monitoring.metricWriter and roles/cloudtrace.agent.","Provide ADC: `gcloud auth application-default login` or set GOOGLE_APPLICATION_CREDENTIALS.","On GCE/GKE, ensure the node service account has monitoring.write and trace.append scopes.","Verify egress to monitoring.googleapis.com / cloudtrace.googleapis.com (or configure Private Service Connect)."],"exampleFix":"# before\n$ gcloud services enable monitoring.googleapis.com trace.googleapis.com --project=my-proj  # forgot\n\n# after\n$ gcloud services enable monitoring.googleapis.com trace.googleapis.com --project=my-proj\n$ gcloud auth application-default login\n$ ./svc","handlingStrategy":"try-catch","validationCode":"// Pre-flight: build (and close) a Stackdriver exporter with the same config.\nimport \"contrib.go.opencensus.io/exporter/stackdriver\"\n\nfunc probeStackdriver(project string) error {\n    e, err := stackdriver.NewExporter(stackdriver.Options{ProjectID: project})\n    if err != nil { return err }\n    e.Flush()\n    return e.Close()\n}\n\nif err := probeStackdriver(projectID); err != nil { return err }","typeGuard":null,"tryCatchPattern":"if err := observability.Start(ctx); err != nil {\n    if strings.Contains(err.Error(), \"failed to create Stackdriver exporter\") {\n        log.Fatalf(\"Stackdriver exporter creation failed (ADC/API/scopes/network): %v\", err)\n    }\n    return err\n}","preventionTips":["Enable Cloud Monitoring and Cloud Trace APIs on the target project.","Grant roles/monitoring.metricWriter and roles/cloudtrace.agent to the SA.","On GCE/GKE ensure node SA has monitoring.write and trace.append scopes."],"tags":["observability","opencensus","stackdriver","gcp"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}