grpc/grpc-go · error

error parsing observability config: invalid cloud trace samp

Error message

error parsing observability config: invalid cloud trace sampling rate %v

What it means

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.

Source

Thrown at gcp/observability/config.go:120

			return fmt.Errorf("error in serverRPCEvent method: %v", err)
		}
	}
	return nil
}

// unmarshalAndVerifyConfig unmarshals a json string representing an
// observability config into its internal go format, and also verifies the
// configuration's fields for validity.
func unmarshalAndVerifyConfig(rawJSON json.RawMessage) (*config, error) {
	var config config
	if err := json.Unmarshal(rawJSON, &config); err != nil {
		return nil, fmt.Errorf("error parsing observability config: %v", err)
	}
	if err := validateLoggingEvents(&config); err != nil {
		return nil, fmt.Errorf("error parsing observability config: %v", err)
	}
	if config.CloudTrace != nil && (config.CloudTrace.SamplingRate > 1 || config.CloudTrace.SamplingRate < 0) {
		return nil, fmt.Errorf("error parsing observability config: invalid cloud trace sampling rate %v", config.CloudTrace.SamplingRate)
	}
	logger.Infof("Parsed ObservabilityConfig: %+v", &config)
	return &config, nil
}

func parseObservabilityConfig() (*config, error) {
	if f := envconfig.ObservabilityConfigFile; f != "" {
		if envconfig.ObservabilityConfig != "" {
			logger.Warning("Ignoring GRPC_GCP_OBSERVABILITY_CONFIG and using GRPC_GCP_OBSERVABILITY_CONFIG_FILE contents.")
		}
		content, err := os.ReadFile(f)
		if err != nil {
			return nil, fmt.Errorf("error reading observability configuration file %q: %v", f, err)
		}
		return unmarshalAndVerifyConfig(content)
	} else if envconfig.ObservabilityConfig != "" {
		return unmarshalAndVerifyConfig([]byte(envconfig.ObservabilityConfig))
	}

View on GitHub (pinned to 03255a9237)

Solutions

  1. Express the rate as a probability in [0, 1]: use 0.05 for 5%, 1.0 for every call, 0 for none.
  2. Omit cloud_trace entirely to leave tracing disabled rather than using an out-of-range sentinel.
  3. Validate the value in your config loader: `if r < 0 || r > 1 { return error }`.

Example fix

// before
{ "cloud_trace": { "sampling_rate": 5 } }

// after
{ "cloud_trace": { "sampling_rate": 0.05 } }
Defensive patterns

Strategy: validation

Validate before calling

func validateSamplingRate(r float64) error {
    if r < 0 || r > 1 {
        return fmt.Errorf("sampling_rate %v not in [0,1]", r)
    }
    return nil
}

Try / catch

if cfg.CloudTrace != nil {
    if err := validateSamplingRate(cfg.CloudTrace.SamplingRate); err != nil {
        return err
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of grpc/grpc-go@03255a9237 (2026-08-07). Data as JSON: /api/errors/6b5619cc11b93161. Report an issue: GitHub.