<!-- 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 -->
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
|
"github.com/fleetdm/fleet/v4/server/platform/tracing"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
const traceSamplerSettingsID = 1
|
|
|
|
func (ds *Datastore) GetTraceSamplerSettings(ctx context.Context) (*tracing.Settings, error) {
|
|
const stmt = `
|
|
SELECT high_volume_ratio, standard_ratio, force_full, updated_at
|
|
FROM trace_sampler_settings
|
|
WHERE id = ?
|
|
`
|
|
var settings tracing.Settings
|
|
if err := sqlx.GetContext(ctx, ds.reader(ctx), &settings, stmt, traceSamplerSettingsID); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ctxerr.Wrap(ctx, notFound("TraceSamplerSettings"))
|
|
}
|
|
return nil, ctxerr.Wrap(ctx, err, "get trace_sampler_settings")
|
|
}
|
|
return &settings, nil
|
|
}
|
|
|
|
func (ds *Datastore) SetTraceSamplerSettings(ctx context.Context, settings *tracing.Settings) error {
|
|
const stmt = `
|
|
UPDATE trace_sampler_settings
|
|
SET high_volume_ratio = ?, standard_ratio = ?, force_full = ?
|
|
WHERE id = ?
|
|
`
|
|
res, err := ds.writer(ctx).ExecContext(ctx, stmt,
|
|
settings.HighVolumeRatio,
|
|
settings.StandardRatio,
|
|
settings.ForceFull,
|
|
traceSamplerSettingsID,
|
|
)
|
|
if err != nil {
|
|
return ctxerr.Wrap(ctx, err, "set trace_sampler_settings")
|
|
}
|
|
rows, err := res.RowsAffected()
|
|
if err != nil {
|
|
return ctxerr.Wrap(ctx, err, "set trace_sampler_settings: rows affected")
|
|
}
|
|
// The singleton row is seeded by the migration. A missing row means the invariant is broken.
|
|
if rows != 1 {
|
|
return ctxerr.Wrap(ctx, fmt.Errorf("set trace_sampler_settings: expected 1 row updated, got %d", rows))
|
|
}
|
|
return nil
|
|
}
|