{"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/03255a9237b6eb32710f6bc4f2de9a675b99fe36/gcp/observability/observability.go#L54-L90","documentation":"A wrapper returned by Start() when startOpenCensus(config) fails. The inner error (interpolated via %v) is usually one of: Stackdriver exporter creation failure, view registration failure, or exporter type-assertion failure. Because Start() defers End() on error, partial resources are cleaned up, but no telemetry instrumentation is applied to subsequently created clients/servers.","triggerScenarios":"Invoking Start() with a config that enables cloud_trace or cloud_monitoring but whose ProjectID cannot be resolved, or whose Stackdriver exporter cannot authenticate. Also when default views have already been registered by a prior Start() call in the same process.","commonSituations":"Running on a machine without Application Default Credentials (no GOOGLE_APPLICATION_CREDENTIALS and no metadata server); specifying a non-existent GCP project ID; calling Start() twice (e.g. in a test binary and in the app).","solutions":["Inspect the wrapped error string after the colon; it identifies the underlying cause (project ID, auth, duplicate view registration).","Ensure Application Default Credentials are available: run `gcloud auth application-default login` or set GOOGLE_APPLICATION_CREDENTIALS.","Confirm config.ProjectID is valid and the account has monitoring/trace write scopes.","Call Start() exactly once per process; do not re-invoke after End()."],"exampleFix":"// before\nif err := observability.Start(ctx); err != nil {\n    log.Fatal(err) // \"failed to instrument OpenCensus: ...\"\n}\n// after\nif err := observability.Start(ctx); err != nil {\n    log.Fatalf(\"observability disabled, continuing without telemetry: %v\", err)\n}\n// and ensure ADC is present: gcloud auth application-default login","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := observability.Start(ctx); err != nil {\n    if strings.Contains(err.Error(), \"failed to instrument OpenCensus\") {\n        log.Printf(\"running without tracing/metrics: %v\", err)\n    } else {\n        return err\n    }\n}","preventionTips":["Always pair Start() with defer observability.End() so partial resources are cleaned up on failure.","Ensure Application Default Credentials are present before Start() in any environment enabling trace/metrics.","Call Start() exactly once per process; guard with sync.Once."],"tags":["grpc","observability","opencensus","gcp","authentication","startup"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}