minsev

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: Apache-2.0 Imports: 4 Imported by: 3

Documentation

Overview

Package minsev provides an log.Processor that will not log any record with a severity below a configured threshold.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogProcessor

type LogProcessor struct {
	log.Processor
	// contains filtered or unexported fields
}

LogProcessor is an log.Processor implementation that wraps another log.Processor. It will pass-through calls to OnEmit and Enabled for records with severity greater than or equal to a minimum. All other method calls are passed to the wrapped log.Processor.

If the wrapped log.Processor is nil, calls to the LogProcessor methods will panic. Use NewLogProcessor to create a new LogProcessor that ensures no panics.

func NewLogProcessor

func NewLogProcessor(downstream log.Processor, severity Severitier) *LogProcessor

NewLogProcessor returns a new LogProcessor that wraps the downstream log.Processor.

severity reports the minimum record severity that will be logged. The LogProcessor discards records with lower severities. If severity is nil, SeverityInfo is used as a default. The LogProcessor calls severity.Severity for each record processed or queried; to adjust the minimum level dynamically, use a SeverityVar.

If downstream is nil a default No-Op log.Processor is used. The returned processor will not be enabled for nor emit any records.

func (*LogProcessor) Enabled

func (p *LogProcessor) Enabled(ctx context.Context, param log.EnabledParameters) bool

Enabled returns if the log.Processor that p wraps is enabled if the severity of param is greater than or equal to p.Minimum. Otherwise false is returned.

func (*LogProcessor) OnEmit

func (p *LogProcessor) OnEmit(ctx context.Context, record *log.Record) error

OnEmit passes ctx and r to the log.Processor that p wraps if the severity of record is greater than or equal to p.Minimum. Otherwise, record is dropped.

type Severitier added in v0.4.0

type Severitier interface {
	Severity() log.Severity
}

A Severitier provides a log.Severity value.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"strings"
	"sync"

	"go.opentelemetry.io/contrib/processors/minsev"
	"go.opentelemetry.io/otel/log"
	logsdk "go.opentelemetry.io/otel/sdk/log"
)

const key = "OTEL_LOG_LEVEL"

var getSeverity = sync.OnceValue(func() log.Severity {
	conv := map[string]log.Severity{
		"":      log.SeverityInfo, // Default to SeverityInfo for unset.
		"debug": log.SeverityDebug,
		"info":  log.SeverityInfo,
		"warn":  log.SeverityWarn,
		"error": log.SeverityError,
	}
	// log.SeverityUndefined for unknown values.
	return conv[strings.ToLower(os.Getenv(key))]
})

type EnvSeverity struct{}

func (EnvSeverity) Severity() log.Severity { return getSeverity() }

func main() {
	// Mock an environmental variable setup that would be done externally.
	_ = os.Setenv(key, "error")

	// Existing processor that emits telemetry.
	var processor logsdk.Processor = logsdk.NewBatchProcessor(nil)

	// Wrap the processor so that it filters by severity level defined
	// via environmental variable.
	processor = minsev.NewLogProcessor(processor, EnvSeverity{})
	lp := logsdk.NewLoggerProvider(
		logsdk.WithProcessor(processor),
	)

	// Show that Logs API respects the minimum severity level processor.
	l := lp.Logger("ExampleSeveritier")

	ctx := context.Background()
	params := log.EnabledParameters{Severity: log.SeverityDebug}
	fmt.Println(l.Enabled(ctx, params))

	params.Severity = log.SeverityError
	fmt.Println(l.Enabled(ctx, params))

}
Output:

false
true

type Severity added in v0.4.0

type Severity int

Severity represents a log record severity (also known as log level). Smaller numerical values correspond to less severe log records (such as debug events), larger numerical values correspond to more severe log records (such as errors and critical events).

const (
	// A fine-grained debugging log record. Typically disabled in default
	// configurations.
	SeverityTrace1 Severity = -8 // TRACE
	SeverityTrace2 Severity = -7 // TRACE2
	SeverityTrace3 Severity = -6 // TRACE3
	SeverityTrace4 Severity = -5 // TRACE4

	// A debugging log record.
	SeverityDebug1 Severity = -4 // DEBUG
	SeverityDebug2 Severity = -3 // DEBUG2
	SeverityDebug3 Severity = -2 // DEBUG3
	SeverityDebug4 Severity = -1 // DEBUG4

	// An informational log record. Indicates that an event happened.
	SeverityInfo1 Severity = 0 // INFO
	SeverityInfo2 Severity = 1 // INFO2
	SeverityInfo3 Severity = 2 // INFO3
	SeverityInfo4 Severity = 3 // INFO4

	// A warning log record. Not an error but is likely more important than an
	// informational event.
	SeverityWarn1 Severity = 4 // WARN
	SeverityWarn2 Severity = 5 // WARN2
	SeverityWarn3 Severity = 6 // WARN3
	SeverityWarn4 Severity = 7 // WARN4

	// An error log record. Something went wrong.
	SeverityError1 Severity = 8  // ERROR
	SeverityError2 Severity = 9  // ERROR2
	SeverityError3 Severity = 10 // ERROR3
	SeverityError4 Severity = 11 // ERROR4

	// A fatal log record such as application or system crash.
	SeverityFatal1 Severity = 12 // FATAL
	SeverityFatal2 Severity = 13 // FATAL2
	SeverityFatal3 Severity = 14 // FATAL3
	SeverityFatal4 Severity = 15 // FATAL4

	// Convenience definitions for the base severity of each level.
	SeverityTrace = SeverityTrace1
	SeverityDebug = SeverityDebug1
	SeverityInfo  = SeverityInfo1
	SeverityWarn  = SeverityWarn1
	SeverityError = SeverityError1
	SeverityFatal = SeverityFatal1
)

Severity values defined by OpenTelemetry.

func (Severity) Severity added in v0.4.0

func (s Severity) Severity() log.Severity

Severity returns the receiver translated to a log.Severity.

It implements Severitier.

type SeverityVar added in v0.4.0

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

A SeverityVar is a Severity variable, to allow a LogProcessor severity to change dynamically. It implements Severitier as well as a Set method, and it is safe for use by multiple goroutines.

The zero SeverityVar corresponds to SeverityInfo.

func (*SeverityVar) Set added in v0.4.0

func (v *SeverityVar) Set(l Severity)

Set sets v's Severity to l.

func (*SeverityVar) Severity added in v0.4.0

func (v *SeverityVar) Severity() log.Severity

Severity returns v's severity.

Jump to

Keyboard shortcuts

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