{"record":{"id":"b8c114056a7e1aeb","repo":"grpc/grpc-go","slug":"failed-to-instrument-opencensus-v","errorCode":null,"errorMessage":"failed to instrument OpenCensus: %v","messagePattern":"failed to instrument OpenCensus: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gcp/observability/observability.go","lineNumber":72,"sourceCode":"\t\treturn fmt.Errorf(\"no ObservabilityConfig found\")\n\t}\n\n\t// Set the project ID if it isn't configured manually.\n\tif err = ensureProjectIDInObservabilityConfig(ctx, config); err != nil {\n\t\treturn err\n\t}\n\n\t// Cleanup any created resources this function created in case this function\n\t// errors.\n\tdefer func() {\n\t\tif err != nil {\n\t\t\tEnd()\n\t\t}\n\t}()\n\n\t// Enabling tracing and metrics via OpenCensus\n\tif err = startOpenCensus(config); err != nil {\n\t\treturn fmt.Errorf(\"failed to instrument OpenCensus: %v\", err)\n\t}\n\n\tif err = startLogging(ctx, config); err != nil {\n\t\treturn fmt.Errorf(\"failed to start logging: %v\", err)\n\t}\n\n\t// Logging is controlled by the config at methods level.\n\treturn nil\n}\n\n// End is the clean-up API for gRPC Observability plugin. It is expected to be\n// invoked in the main function of the application. The suggested usage is\n// \"defer observability.End()\". This function also flushes data to upstream, and\n// cleanup resources.\n//\n// Note: this method should only be invoked once.\nfunc End() {\n\tstopLogging()","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/grpc/grpc-go/blob/0c51461d27177d997e14c642fe18c11668fc09a3/gcp/observability/observability.go#L54-L90","documentation":"observability.Start called startOpenCensus and it failed; the wrapped %v is most often the Stackdriver exporter creation error (error 256) or the views registration error (error 257). The deferred End() runs to clean up partial state. Emitted at observability.go:72.","triggerScenarios":"Config has cloud_trace and/or cloud_monitoring present, so startOpenCensus tries to build the Stackdriver exporter; that fails on bad ADC, bad project, disabled APIs, or the default views are already registered. Also when view.Register returns (error 257) because the views were registered by another package.","commonSituations":"App already calls opencensus view.Register for the same grpc views elsewhere; Stackdriver Monitoring/Trace API disabled; ADC missing; running Start twice in a test; conflicting OpenCensus instrumentation from another library.","solutions":["Read the inner %v: if it is error 256, fix ADC / project / API enablement for Stackdriver; if error 257, deregister the duplicate views or stop the conflicting instrumentation.","Make sure your app does not also register opencensus.Default grpc views elsewhere — Start owns them when cloud_monitoring is on.","Call observability.Start exactly once per process.","If you don't need metrics/tracing, drop cloud_trace and cloud_monitoring from the config to skip OpenCensus setup entirely."],"exampleFix":"// before\nexport GRPC_GCP_OBSERVABILITY_CONFIG='{\"project_id\":\"\",\"cloud_monitoring\":{}}'\n\n// after\nexport GRPC_GCP_OBSERVABILITY_CONFIG='{\"project_id\":\"my-proj\",\"cloud_monitoring\":{}}'\nexport GOOGLE_APPLICATION_CREDENTIALS=/etc/sa.json","handlingStrategy":"try-catch","validationCode":"// Pre-flight: can we build the Stackdriver exporter and register views?\n// Ensure default views are not already registered.\nfor _, v := range defaultViews {\n    if vFor, err := view.Find(v.Name); err == nil && vFor != nil {\n        return fmt.Errorf(\"view %s already registered; unregister before Start\", v.Name)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := observability.Start(ctx); err != nil {\n    if strings.Contains(err.Error(), \"failed to instrument OpenCensus\") {\n        // inner is 256 (exporter) or 257 (views); branch on substring\n        return fmt.Errorf(\"OpenCensus instrumentation failed (ADC/API/duplicate views): %w\", err)\n    }\n    return err\n}","preventionTips":["Do not register grpc opencensus default views yourself when cloud_monitoring is on.","Call Start exactly once per process.","Enable Cloud Monitoring + Trace APIs before deploy."],"tags":["observability","opencensus","stackdriver","startup"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}