{"record":{"id":"746c54115568db4d","repo":"nats-io/nats-server","slug":"bad-sampling-percentage-should-be-1-100","errorCode":null,"errorMessage":"bad sampling percentage, should be 1-100","messagePattern":"bad sampling percentage, should be 1-100","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/errors.go","lineNumber":120,"sourceCode":"\tErrAccountExists = errors.New(\"account exists\")\n\n\t// ErrBadAccount represents a malformed or incorrect account.\n\tErrBadAccount = errors.New(\"bad account\")\n\n\t// ErrReservedAccount represents a reserved account that can not be created.\n\tErrReservedAccount = errors.New(\"reserved account\")\n\n\t// ErrMissingAccount is returned when an account does not exist.\n\tErrMissingAccount = errors.New(\"account missing\")\n\n\t// ErrMissingService is returned when an account does not have an exported service.\n\tErrMissingService = errors.New(\"service missing\")\n\n\t// ErrBadServiceType is returned when latency tracking is being applied to non-singleton response types.\n\tErrBadServiceType = errors.New(\"bad service response type\")\n\n\t// ErrBadSampling is returned when the sampling for latency tracking is not 1 >= sample <= 100.\n\tErrBadSampling = errors.New(\"bad sampling percentage, should be 1-100\")\n\n\t// ErrAccountValidation is returned when an account has failed validation.\n\tErrAccountValidation = errors.New(\"account validation failed\")\n\n\t// ErrAccountExpired is returned when an account has expired.\n\tErrAccountExpired = errors.New(\"account expired\")\n\n\t// ErrNoAccountResolver is returned when we attempt an update but do not have an account resolver.\n\tErrNoAccountResolver = errors.New(\"account resolver missing\")\n\n\t// ErrAccountResolverUpdateTooSoon is returned when we attempt an update too soon to last request.\n\tErrAccountResolverUpdateTooSoon = errors.New(\"account resolver update too soon\")\n\n\t// ErrAccountResolverSameClaims is returned when same claims have been fetched.\n\tErrAccountResolverSameClaims = errors.New(\"account resolver no new claims\")\n\n\t// ErrStreamImportAuthorization is returned when a stream import is not authorized.\n\tErrStreamImportAuthorization = errors.New(\"stream import not authorized\")","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/errors.go#L102-L138","documentation":"ErrBadSampling is returned when the sampling percentage for service latency tracking is outside the valid range 1-100. The library enforces 1 >= sample <= 100; a value of 0 means sampling was triggered by a header and is allowed to bypass the check.","triggerScenarios":"Calling TrackServiceExportWithSampling with sampling < 1 or > 100, e.g. -1 (as in TestAccountRequestReplyTrackLatency at server/accounts_test.go:1738) or 101. Only sampling==0 (header-triggered) skips validation.","commonSituations":"Passing an uninitialized/negative int as sampling; confusing percentage with fraction (0.5 instead of 50); computing the percentage dynamically and producing out-of-range values; user-supplied config values not clamped before use.","solutions":["Pass a sampling value between 1 and 100 inclusive (or 0 for header-triggered sampling)","Clamp or validate any dynamically computed/user-supplied sampling percentage before the call","Use a named constant for the default sampling (e.g. 100) instead of ad-hoc values","If sampling should be disabled entirely, do not call the tracking API rather than passing 0/negative"],"exampleFix":"// before\nacc.TrackServiceExportWithSampling(\"svc\", \"results\", -1)\n// after\nsampling := 100\nif sampling < 1 || sampling > 100 { sampling = 100 }\nerr := acc.TrackServiceExportWithSampling(\"svc\", \"results\", sampling)","handlingStrategy":"validation","validationCode":"func validSampling(s int) bool { return s == 0 || (s >= 1 && s <= 100) }\nif !validSampling(sampling) { return fmt.Errorf(\"sampling must be 1-100 (or 0 for header-triggered), got %d\", sampling) }","typeGuard":"null","tryCatchPattern":"if err := acc.TrackServiceExportWithSampling(svc, resp, sampling); err != nil {\n\tif errors.Is(err, ErrBadSampling) { sampling = 100 /* clamp and retry */ }\n}","preventionTips":["Clamp user/config-provided sampling percentages to [1,100]","Remember 0 is special (header-triggered), not \"disabled\"","Use integers, not fractions — 0.5 means invalid, 50 means 50%"],"tags":["nats","server","service-latency","validation"],"backgroundTag":"invalid-parameter-range","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}