fleet/server/contexts/ctxerr/metadata.go
Victor Lyuboslavsky c88cc953fb
Refactor endpoint_utils for modularization (#36484)
Resolves #37192

Separating generic endpoint_utils middleware logic from domain-specific
business logic. New bounded contexts would share the generic logic and
implement their own domain-specific logic. The two approaches used in
this PR are:
- Use common `platform` types
- Use interfaces

In the next PR we will move `endpointer_utils`, `authzcheck` and
`ratelimit` into `platform` directory.

# Checklist for submitter

- [x] Added changes file

## Testing

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



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

## Summary by CodeRabbit

* **Refactor**
* Restructured internal error handling and context management to support
bounded context architecture.
* Improved error context collection and telemetry observability through
a provider-based mechanism.
* Decoupled licensing and authentication concerns into interfaces for
better modularity.

* **Chores**
* Updated internal package dependencies to align with new architectural
boundaries.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-12-31 09:12:00 -06:00

35 lines
1.5 KiB
Go

package ctxerr
import "context"
// ErrorContextProvider provides contextual information for error handling.
// Implementations can provide data for both error storage and telemetry systems.
type ErrorContextProvider interface {
// GetDiagnosticContext returns attributes stored with errors for troubleshooting.
// Data is persisted to Redis and included in logs. Should contain diagnostic
// information like platform, versions, and status flags. Avoid including PII.
GetDiagnosticContext() map[string]any
// GetTelemetryContext returns attributes sent to observability systems
// (OpenTelemetry, Sentry). May include identifiers not stored with errors.
// Return nil if no telemetry context is available.
GetTelemetryContext() map[string]any
}
type errorContextProvidersKey struct{}
// AddErrorContextProvider returns a new context with the given provider added to
// the existing providers. This is useful when you want to add a provider
// without replacing existing ones.
func AddErrorContextProvider(ctx context.Context, provider ErrorContextProvider) context.Context {
existing := getErrorContextProviders(ctx)
providers := make([]ErrorContextProvider, len(existing)+1)
copy(providers, existing)
providers[len(existing)] = provider
return context.WithValue(ctx, errorContextProvidersKey{}, providers)
}
func getErrorContextProviders(ctx context.Context) []ErrorContextProvider {
providers, _ := ctx.Value(errorContextProvidersKey{}).([]ErrorContextProvider)
return providers
}