{"record":{"id":"6b5619cc11b93161","repo":"grpc/grpc-go","slug":"error-parsing-observability-config-invalid-cloud","errorCode":null,"errorMessage":"error parsing observability config: invalid cloud trace sampling rate %v","messagePattern":"error parsing observability config: invalid cloud trace sampling rate (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gcp/observability/config.go","lineNumber":120,"sourceCode":"\t\t\treturn fmt.Errorf(\"error in serverRPCEvent method: %v\", err)\n\t\t}\n\t}\n\treturn nil\n}\n\n// unmarshalAndVerifyConfig unmarshals a json string representing an\n// observability config into its internal go format, and also verifies the\n// configuration's fields for validity.\nfunc unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*config, error) {\n\tvar config config\n\tif err := json.Unmarshal(rawJSON, &config); err != nil {\n\t\treturn nil, fmt.Errorf(\"error parsing observability config: %v\", err)\n\t}\n\tif err := validateLoggingEvents(&config); err != nil {\n\t\treturn nil, fmt.Errorf(\"error parsing observability config: %v\", err)\n\t}\n\tif config.CloudTrace != nil && (config.CloudTrace.SamplingRate > 1 || config.CloudTrace.SamplingRate < 0) {\n\t\treturn nil, fmt.Errorf(\"error parsing observability config: invalid cloud trace sampling rate %v\", config.CloudTrace.SamplingRate)\n\t}\n\tlogger.Infof(\"Parsed ObservabilityConfig: %+v\", &config)\n\treturn &config, nil\n}\n\nfunc parseObservabilityConfig() (*config, error) {\n\tif f := envconfig.ObservabilityConfigFile; f != \"\" {\n\t\tif envconfig.ObservabilityConfig != \"\" {\n\t\t\tlogger.Warning(\"Ignoring GRPC_GCP_OBSERVABILITY_CONFIG and using GRPC_GCP_OBSERVABILITY_CONFIG_FILE contents.\")\n\t\t}\n\t\tcontent, err := os.ReadFile(f)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"error reading observability configuration file %q: %v\", f, err)\n\t\t}\n\t\treturn unmarshalAndVerifyConfig(content)\n\t} else if envconfig.ObservabilityConfig != \"\" {\n\t\treturn unmarshalAndVerifyConfig([]byte(envconfig.ObservabilityConfig))\n\t}","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/grpc/grpc-go/blob/03255a9237b6eb32710f6bc4f2de9a675b99fe36/gcp/observability/config.go#L102-L138","documentation":"When cloud_trace is present in the observability config, its sampling_rate must be a probability in [0.0, 1.0]. A value outside that inclusive range (negative or greater than 1) is rejected at config-parse time with the offending value echoed back.","triggerScenarios":"Setting \"cloud_trace\": { \"sampling_rate\": 5 } thinking it means 5%, or 1.5 (150%), or a negative number to disable tracing. The config treats the value as a probability, not a percentage.","commonSituations":"Confusing sampling_rate with a percentage (write 0.05 not 5); passing an integer percentage from a flag without dividing by 100; misreading the docs.","solutions":["Express the rate as a probability in [0, 1]: use 0.05 for 5%, 1.0 for every call, 0 for none.","Omit cloud_trace entirely to leave tracing disabled rather than using an out-of-range sentinel.","Validate the value in your config loader: `if r < 0 || r > 1 { return error }`."],"exampleFix":"// before\n{ \"cloud_trace\": { \"sampling_rate\": 5 } }\n\n// after\n{ \"cloud_trace\": { \"sampling_rate\": 0.05 } }","handlingStrategy":"validation","validationCode":"func validateSamplingRate(r float64) error {\n    if r < 0 || r > 1 {\n        return fmt.Errorf(\"sampling_rate %v not in [0,1]\", r)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if cfg.CloudTrace != nil {\n    if err := validateSamplingRate(cfg.CloudTrace.SamplingRate); err != nil {\n        return err\n    }\n}","preventionTips":["Treat sampling_rate as a probability, not a percent (0.05 == 5%).","Omit cloud_trace to disable rather than using out-of-range sentinels.","Validate in your config loader."],"tags":["grpc","observability","cloud-trace","config","validation","gcp"],"analyzedSha":"03255a9237b6eb32710f6bc4f2de9a675b99fe36","analyzedAt":"2026-08-07T00:29:34.215Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}