Files
fleet/server/service/tracing_tiers.go
Victor Lyuboslavsky 59a673bc15 Added trace sampler to use OTEL in prod. (#46595)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #44652 

Docs: https://github.com/fleetdm/fleet/pull/46631

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements), JS
inline code is prevented especially for url redirects, and untrusted
data interpolated into shell scripts/commands is validated against shell
metacharacters.

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually

## Database migrations

- [x] Ensured the correct collation is explicitly set for character
columns (`COLLATE utf8mb4_unicode_ci`).

## New Fleet configuration settings

- [x] Setting(s) is/are explicitly excluded from GitOps

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Route-aware OpenTelemetry trace sampling with tiered default ratios
(very low for select high-volume routes, reduced rate for admin reads,
full sampling otherwise).
* Admin-only GET/PATCH /debug/trace_sampler to view and update sampling
ratios and a runtime "force full" toggle.
* Liveness probe endpoints (/healthz, /version, /metrics) are excluded
from tracing; settings propagate to replicas at runtime without restart.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-06-02 18:53:00 -05:00

63 lines
4.6 KiB
Go

package service
import (
"net/http"
"github.com/fleetdm/fleet/v4/server/platform/tracing"
)
// RegisterTracingTiers populates the trace sampling registry with the tier classifications for routes owned by this package
// (the legacy flat server/service handler).
//
// Bounded contexts that have moved out of this package (e.g. server/activity/) own their own registrations. They
// should expose a RegisterTracingTiers function the serve command calls during startup. The platform/tracing package itself
// stays free of route knowledge.
//
// Paths use "_version_" as the placeholder for the gorilla/mux fleetversion segment. The sampler normalizes incoming span
// names back to that form before lookup. Alternate path forms (e.g. /api/v1/osquery/...) are registered separately.
//
// Unregistered routes (including all cron jobs, enroll, SCEP, MDM checkin, command ack/result, GitOps batch, etc.) fall to
// TierAlways by design.
func RegisterTracingTiers(registry *tracing.Registry) {
// Hot agent endpoints. These dominate request volume at scale (tens of thousands of spans per second on a 100k-host
// fleet) without being individually interesting. Sampled at the configured high volume ratio (default 0.1%).
registry.Register(http.MethodPost, "/api/osquery/config", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/v1/osquery/config", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/osquery/distributed/read", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/v1/osquery/distributed/read", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/osquery/distributed/write", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/v1/osquery/distributed/write", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/osquery/log", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/v1/osquery/log", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/fleet/orbit/config", tracing.TierHighVolume)
registry.Register(http.MethodPost, "/api/fleet/orbit/device_token", tracing.TierHighVolume)
registry.Register(http.MethodHead, "/api/fleet/orbit/ping", tracing.TierHighVolume)
registry.Register(http.MethodHead, "/api/fleet/device/ping", tracing.TierHighVolume)
registry.Register(http.MethodHead, "/api/_version_/fleet/device/{token}/ping", tracing.TierHighVolume)
registry.Register(http.MethodGet, "/api/_version_/fleet/device/{token}/desktop", tracing.TierHighVolume)
// Admin / read endpoints. Moderate volume, moderate diagnostic value. Sampled at the configured standard ratio (default
// 2%). Starting set covers the highest traffic admin reads plus the per page load endpoints (config, me, version, etc.)
// that the UI hits on every navigation. Expand as we observe traffic patterns in dogfood and the customer pilot.
registry.Register(http.MethodGet, "/api/_version_/fleet/config", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/fleets", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/host_summary", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/hosts", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/hosts/{id}", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/hosts/count", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/hosts/identifier/{identifier}", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/hosts/summary/mdm", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/labels", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/me", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/policies", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/reports", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/software", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/software/titles", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/software/versions", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/spec/enroll_secret", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/users", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/version", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/charts/{metric}", tracing.TierStandard)
registry.Register(http.MethodGet, "/api/_version_/fleet/android_enterprise", tracing.TierStandard)
}