{"record":{"id":"d48f4e710a54632b","repo":"grpc/grpc-go","slug":"failed-to-register-observability-views-v","errorCode":null,"errorMessage":"failed to register observability views: %v","messagePattern":"failed to register observability views: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gcp/observability/opencensus.go","lineNumber":146,"sourceCode":"\t\treturn nil\n\t}\n\n\tvar err error\n\texporter, err = newExporter(config)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tvar to opencensus.TraceOptions\n\tif config.CloudTrace != nil {\n\t\tto.TS = trace.ProbabilitySampler(config.CloudTrace.SamplingRate)\n\t\ttrace.RegisterExporter(exporter.(trace.Exporter))\n\t\tlogger.Infof(\"Start collecting and exporting trace spans with global_trace_sampling_rate=%.2f\", config.CloudTrace.SamplingRate)\n\t}\n\n\tif config.CloudMonitoring != nil {\n\t\tif err := view.Register(defaultViews...); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to register observability views: %v\", err)\n\t\t}\n\t\tview.SetReportingPeriod(defaultMetricsReportingInterval)\n\t\tview.RegisterExporter(exporter.(view.Exporter))\n\t\tlogger.Infof(\"Start collecting and exporting metrics\")\n\t}\n\n\tinternal.AddGlobalServerOptions.(func(opt ...grpc.ServerOption))(opencensus.ServerOption(to))\n\tinternal.AddGlobalDialOptions.(func(opt ...grpc.DialOption))(opencensus.DialOption(to))\n\tlogger.Infof(\"Enabled OpenCensus StatsHandlers for clients and servers\")\n\n\treturn nil\n}\n\n// stopOpenCensus flushes the exporter's and cleans up globals across all\n// packages if exporter was created.\nfunc stopOpenCensus() {\n\tif exporter != nil {\n\t\tinternal.ClearGlobalDialOptions()","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/grpc/grpc-go/blob/03255a9237b6eb32710f6bc4f2de9a675b99fe36/gcp/observability/opencensus.go#L128-L164","documentation":"Returned when view.Register(defaultViews...) fails. OpenCensus's view.Register fails if any of the supplied views are already registered (duplicate name). This typically indicates Start() was invoked twice, or another package already registered the gRPC OpenCensus views.","triggerScenarios":"Calling observability.Start() more than once in one process; importing both this observability package and a separate call to opencensus.RegisterViews/stats.Register that registers the same gRPC views; running the observability binary inside a test harness that already instrumented views.","commonSituations":"Mixing the gRPC Observability plugin with hand-rolled OpenCensus setup; init() in a helper package that also registers gRPC views; tests that run Start() per test case instead of once per process.","solutions":["Call observability.Start() exactly once at process startup.","Remove duplicate opencensus.RegisterViews/stats.Register calls for the gRPC default views.","Move Start() to TestMain or a sync.Once guard so repeated invocations are no-ops.","Call view.Unregister(defaultViews...) before re-registering if you must rebuild."],"exampleFix":"// before\nvar startOnce sync.Once\n// ...\nfunc setup() {\n    observability.Start(ctx) // called per request -> duplicate registration\n}\n// after\nvar startOnce sync.Once\nfunc setup() {\n    startOnce.Do(func() {\n        if err := observability.Start(ctx); err != nil { log.Fatal(err) }\n    })\n}","handlingStrategy":"validation","validationCode":"// Ensure Start() runs once per process.\nvar startOnce sync.Once\nvar startErr error\nfunc EnsureObservability(ctx context.Context) {\n    startOnce.Do(func() { startErr = observability.Start(ctx) })\n}","typeGuard":null,"tryCatchPattern":"if err := observability.Start(ctx); err != nil {\n    if strings.Contains(err.Error(), \"failed to register observability views\") {\n        // Duplicate registration: views already registered by another init; safe to continue.\n        log.Printf(\"views already registered: %v\", err)\n    }\n}","preventionTips":["Never call Start() more than once; wrap in sync.Once.","Remove duplicate opencensus view registration elsewhere in the binary.","In tests, register/unregister views in TestMain, not per-case."],"tags":["grpc","observability","opencensus","duplicate-registration","initialization"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}