shared

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LogLevelDebug = "DEBUG"
	LogLevelInfo  = "INFO"
	LogLevelWarn  = "WARN"
	LogLevelError = "ERROR"
)

LogLevel constants

Variables

View Source
var CommonMetrics = struct {
	ConnectionEstablished string
	ConnectionClosed      string
	MessageSent           string
	MessageFailed         string
	AuthenticationFailed  string
	ConnectionDuration    string
	MessageSize           string
	ProcessingLatency     string
}{
	ConnectionEstablished: "ConnectionEstablished",
	ConnectionClosed:      "ConnectionClosed",
	MessageSent:           "MessageSent",
	MessageFailed:         "MessageFailed",
	AuthenticationFailed:  "AuthenticationFailed",
	ConnectionDuration:    "ConnectionDuration",
	MessageSize:           "MessageSize",
	ProcessingLatency:     "ProcessingLatency",
}

CommonMetrics provides metric name constants

View Source
var StandardAlarms = []CloudWatchAlarmConfig{
	{
		AlarmName:          "streamer-high-connection-failures",
		MetricName:         CommonMetrics.AuthenticationFailed,
		Namespace:          "Streamer",
		Statistic:          types.StatisticSum,
		Period:             300,
		EvaluationPeriods:  2,
		Threshold:          10,
		ComparisonOperator: types.ComparisonOperatorGreaterThanThreshold,
		AlarmDescription:   "Alert when authentication failures exceed threshold",
	},
	{
		AlarmName:          "streamer-high-message-failures",
		MetricName:         CommonMetrics.MessageFailed,
		Namespace:          "Streamer",
		Statistic:          types.StatisticSum,
		Period:             300,
		EvaluationPeriods:  2,
		Threshold:          50,
		ComparisonOperator: types.ComparisonOperatorGreaterThanThreshold,
		AlarmDescription:   "Alert when message failures exceed threshold",
	},
	{
		AlarmName:          "streamer-high-processing-latency",
		MetricName:         CommonMetrics.ProcessingLatency,
		Namespace:          "Streamer",
		Statistic:          types.StatisticAverage,
		Period:             300,
		EvaluationPeriods:  3,
		Threshold:          1000,
		ComparisonOperator: types.ComparisonOperatorGreaterThanThreshold,
		AlarmDescription:   "Alert when processing latency is too high",
	},
}

StandardAlarms provides common alarm configurations

Functions

func AddTraceAnnotation

func AddTraceAnnotation(ctx context.Context, key string, value string)

AddTraceAnnotation adds an annotation to the current segment

func AddTraceMetadata

func AddTraceMetadata(ctx context.Context, namespace, key string, value interface{})

AddTraceMetadata adds metadata to the current segment

func CaptureFunc

func CaptureFunc(ctx context.Context, name string, fn func(context.Context) error) error

CaptureFunc wraps a function with X-Ray tracing

func CaptureFuncWithData

func CaptureFuncWithData(ctx context.Context, name string, data TraceSegment, fn func(context.Context) error) error

CaptureFuncWithData wraps a function with X-Ray tracing and custom data

func CreateAlarm

func CreateAlarm(ctx context.Context, cwClient *cloudwatch.Client, config CloudWatchAlarmConfig) error

CreateAlarm creates a CloudWatch alarm

func EndSubsegment

func EndSubsegment(seg *xray.Segment, err error)

EndSubsegment ends an X-Ray subsegment and records any error

func GenerateJWT

func GenerateJWT(userID, tenantID string, permissions []string, secret string, duration time.Duration) (string, error)

GenerateJWT generates a JWT token for testing

func LogMetric

func LogMetric(ctx context.Context, metricName string, metadata map[string]interface{})

LogMetric logs a metric event (for CloudWatch Metrics)

func RecordError

func RecordError(ctx context.Context, err error)

RecordError records an error in the current segment

func StartSubsegment

func StartSubsegment(ctx context.Context, name string, data TraceSegment) (context.Context, *xray.Segment)

StartSubsegment starts a new X-Ray subsegment with custom data

func ValidateJWT

func ValidateJWT(tokenString, secret string) (map[string]interface{}, error)

ValidateJWT validates a JWT token and returns the claims

Types

type CloudWatchAlarmConfig

type CloudWatchAlarmConfig struct {
	AlarmName          string
	MetricName         string
	Namespace          string
	Statistic          types.Statistic
	Period             int32
	EvaluationPeriods  int32
	Threshold          float64
	ComparisonOperator types.ComparisonOperator
	AlarmDescription   string
	Dimensions         []types.Dimension
	SNSTopic           string
}

CloudWatchAlarmConfig represents alarm configuration

type CloudWatchMetrics

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

CloudWatchMetrics implements MetricsPublisher using CloudWatch

func NewCloudWatchMetrics

func NewCloudWatchMetrics(cfg aws.Config, namespace string) *CloudWatchMetrics

NewCloudWatchMetrics creates a new CloudWatch metrics client

func (*CloudWatchMetrics) PublishLatency

func (c *CloudWatchMetrics) PublishLatency(ctx context.Context, namespace, metricName string, duration time.Duration, dimensions ...types.Dimension) error

PublishLatency publishes a latency metric in milliseconds

func (*CloudWatchMetrics) PublishMetric

func (c *CloudWatchMetrics) PublishMetric(ctx context.Context, namespace, metricName string, value float64, unit types.StandardUnit, dimensions ...types.Dimension) error

PublishMetric publishes a single metric to CloudWatch

type JWTClaims

type JWTClaims struct {
	UserID      string   `json:"user_id"`
	TenantID    string   `json:"tenant_id"`
	Permissions []string `json:"permissions,omitempty"`
	jwt.RegisteredClaims
}

JWTClaims represents the expected JWT claims

type Logger

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

Logger provides structured logging

func NewLogger

func NewLogger(serviceName string) *Logger

NewLogger creates a new structured logger

func (*Logger) Debug

func (l *Logger) Debug(ctx context.Context, message string, metadata map[string]interface{})

Debug logs a debug message

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, message string, metadata map[string]interface{})

Error logs an error message

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, message string, metadata map[string]interface{})

Info logs an info message

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, level, message string, metadata map[string]interface{})

Log writes a structured log entry

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, message string, metadata map[string]interface{})

Warn logs a warning message

type MetricsDimensions

type MetricsDimensions struct{}

MetricsDimensions provides common dimension builders

func (MetricsDimensions) Action

func (MetricsDimensions) Action(action string) types.Dimension

Action returns a dimension for action type

func (MetricsDimensions) Environment

func (MetricsDimensions) Environment(env string) types.Dimension

Environment returns a dimension for environment

func (MetricsDimensions) ErrorType

func (MetricsDimensions) ErrorType(errType string) types.Dimension

ErrorType returns a dimension for error type

func (MetricsDimensions) Function

func (MetricsDimensions) Function(name string) types.Dimension

Function returns a dimension for Lambda function name

func (MetricsDimensions) TenantID

func (MetricsDimensions) TenantID(tenantID string) types.Dimension

TenantID returns a dimension for tenant

type MetricsPublisher

type MetricsPublisher interface {
	PublishMetric(ctx context.Context, namespace, metricName string, value float64, unit types.StandardUnit, dimensions ...types.Dimension) error
	PublishLatency(ctx context.Context, namespace, metricName string, duration time.Duration, dimensions ...types.Dimension) error
}

MetricsPublisher defines the CloudWatch metrics interface

type StructuredLog

type StructuredLog struct {
	Level         string                 `json:"level"`
	Message       string                 `json:"message"`
	RequestID     string                 `json:"request_id,omitempty"`
	CorrelationID string                 `json:"correlation_id,omitempty"`
	Timestamp     int64                  `json:"timestamp"`
	Metadata      map[string]interface{} `json:"metadata,omitempty"`
}

StructuredLog represents a structured log entry

type TraceSegment

type TraceSegment struct {
	ConnectionID string `json:"connection_id,omitempty"`
	UserID       string `json:"user_id,omitempty"`
	TenantID     string `json:"tenant_id,omitempty"`
	MessageType  string `json:"message_type,omitempty"`
	MessageSize  int    `json:"message_size,omitempty"`
	Action       string `json:"action,omitempty"`
}

TraceSegment represents custom X-Ray segment data

Jump to

Keyboard shortcuts

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