tagging

package
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package tagging labels requests based on configurable HTTP headers. Each rule names one header whose value carries request labels; labels flow into usage tracking and request logs, and rules can mark their header as do-not-pass so it is never forwarded to upstream providers.

Index

Constants

View Source
const DefaultDelimiter = ","

DefaultDelimiter separates multiple labels inside one header value.

Variables

This section is empty.

Functions

func ExtractLabels

func ExtractLabels(rules []Rule, headers http.Header) []string

ExtractLabels reads every rule's header and returns the deduplicated labels in rule order. Each header value is split by the rule's delimiter and each piece is whitespace-trimmed; when the rule has a prefix, it is trimmed from pieces that carry it, and pieces without it are kept as-is.

func IsValidationError

func IsValidationError(err error) bool

IsValidationError reports whether err stems from invalid caller input.

func NormalizeRules

func NormalizeRules(rules []Rule) error

NormalizeRules canonicalizes header names, applies the default delimiter, and rejects invalid, credential-bearing, or duplicate entries in place. Rejections are ValidationErrors.

func StripHeaderSet

func StripHeaderSet(rules []Rule) map[string]struct{}

StripHeaderSet returns the canonical header names marked do-not-pass.

Types

type MongoDBStore

type MongoDBStore struct {
	// contains filtered or unexported fields
}

MongoDBStore persists tagging rules in a settings collection.

func NewMongoDBStore

func NewMongoDBStore(_ context.Context, database *mongo.Database) (*MongoDBStore, error)

NewMongoDBStore creates a tagging store over the tagging_settings collection.

func (*MongoDBStore) Close

func (s *MongoDBStore) Close() error

Close is a no-op: the client is managed by the storage layer.

func (*MongoDBStore) GetRules

func (s *MongoDBStore) GetRules(ctx context.Context) ([]Rule, error)

func (*MongoDBStore) SaveRules

func (s *MongoDBStore) SaveRules(ctx context.Context, rules []Rule) error

type Result

type Result struct {
	Service *Service
	Store   Store
	// contains filtered or unexported fields
}

Result bundles the tagging service with its store.

func New

func New(ctx context.Context, cfg *config.Config, shared storage.Storage) (*Result, error)

New builds the tagging service on the shared storage connection and loads the persisted operator rules.

func (*Result) Close

func (r *Result) Close() error

type Rule

type Rule struct {
	// Header is the canonical HTTP header name to read labels from.
	Header string `json:"header" bson:"header"`

	// Prefix is optionally trimmed from the front of each label. Trimming only
	// affects the extracted label, never the forwarded header value.
	Prefix string `json:"prefix,omitempty" bson:"prefix,omitempty"`

	// DoNotPass strips the header before forwarding the request upstream.
	// Default: false (headers are passed through as-is).
	DoNotPass bool `json:"do_not_pass,omitempty" bson:"do_not_pass,omitempty"`

	// Delimiter splits one header value into multiple labels. Default: ",".
	Delimiter string `json:"delimiter,omitempty" bson:"delimiter,omitempty"`

	// Managed marks a rule declared in config/env; such rules are read-only in
	// the dashboard. Never persisted.
	Managed bool `json:"managed,omitempty" bson:"-"`
}

Rule configures label extraction from one request header. Managed rules come from config.yaml / TAGGING_HEADER_* env vars, override store rows with the same header name, and are read-only in the dashboard.

func ConfigRules

func ConfigRules(entries []config.TaggingHeaderConfig) []Rule

ConfigRules converts declarative config.yaml / TAGGING_HEADER_* entries into managed tagging rules. Entries are already normalized by config.Load.

func ListStored added in v0.2.4

func ListStored(ctx context.Context, shared storage.Storage) ([]Rule, error)

ListStored returns the tagging rules saved in the dashboard, normalized.

type SQLStore added in v0.1.60

type SQLStore struct {
	// contains filtered or unexported fields
}

SQLStore persists tagging rules in a key-value settings table.

func NewSQLStore added in v0.1.60

func NewSQLStore(ctx context.Context, db sqlx.DB) (*SQLStore, error)

NewSQLStore creates the tagging settings table when missing.

func (*SQLStore) Close added in v0.1.60

func (s *SQLStore) Close() error

Close is a no-op: the connection is managed by the storage layer.

func (*SQLStore) GetRules added in v0.1.60

func (s *SQLStore) GetRules(ctx context.Context) ([]Rule, error)

func (*SQLStore) SaveRules added in v0.1.60

func (s *SQLStore) SaveRules(ctx context.Context, rules []Rule) error

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service merges declarative (config/env) tagging rules over operator rules persisted in the store and serves label extraction on the request hot path.

func NewService

func NewService(configRules []Rule, store Store) *Service

NewService creates a tagging service. configRules must already be normalized by config.Load; store may be nil, in which case only config rules apply and dashboard edits are unavailable.

func (*Service) Editable

func (s *Service) Editable() bool

Editable reports whether operator rules can be persisted.

func (*Service) ExtractLabels

func (s *Service) ExtractLabels(headers http.Header) []string

ExtractLabels returns the request labels for the given inbound headers.

func (*Service) HasRules

func (s *Service) HasRules() bool

HasRules reports whether any tagging rule is currently active.

func (*Service) Refresh

func (s *Service) Refresh(ctx context.Context) error

Refresh reloads operator rules from the store and swaps the merged snapshot.

func (*Service) Rules

func (s *Service) Rules() []Rule

Rules returns the effective rules: managed config rules first, then operator rules from the store.

func (*Service) SaveRules

func (s *Service) SaveRules(ctx context.Context, rules []Rule) ([]Rule, error)

SaveRules validates and persists the operator-managed rule set (replacing the previous set), refreshes the snapshot, and returns the merged view. Rules whose header is declared in config/env are rejected as read-only.

func (*Service) SetChangeNotifier added in v0.1.109

func (s *Service) SetChangeNotifier(fn func(context.Context))

SetChangeNotifier installs fn, called after every successful SaveRules so other gateway instances reload the rules. Call it before serving.

func (*Service) StripHeaders

func (s *Service) StripHeaders() map[string]struct{}

StripHeaders returns the canonical header names that must not be forwarded to upstream providers. Callers must treat the returned map as read-only.

type Store

type Store interface {
	// GetRules returns the persisted operator rules, empty when none were saved.
	GetRules(ctx context.Context) ([]Rule, error)

	// SaveRules replaces the persisted operator rule set.
	SaveRules(ctx context.Context, rules []Rule) error

	// Close releases store resources.
	Close() error
}

Store persists the operator-managed tagging rules (the dashboard-editable set). Declarative config/env rules are never stored.

type ValidationError

type ValidationError struct {
	// contains filtered or unexported fields
}

ValidationError marks rule failures caused by caller input, so API handlers can report them as a bad request instead of a storage failure.

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL