standardize commonly used attributes

Some commonly used at maal attributes have been encoded as consts with
convinience wrappers similar to those of semconv package from otel sdk.
Additionally some utils that can generate mutliple attributes were added.
This commit is contained in:
2024-05-16 13:45:13 +02:00
parent 6fb12e9e9d
commit ab5b70704d
8 changed files with 390 additions and 67 deletions

View File

@ -1,47 +1,51 @@
package level
import (
"git.ma-al.com/gora_filip/pkg/syslog"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type SyslogLevel uint8
type SeverityLevel uint8
const (
EMERG SyslogLevel = iota
ALERT
// This event requires an immediate action. If you suspect that occurence of an event may signal that the
// data will get lost, corrupted, or that the application will change its behaviour following the event in
// an undesired way, select the ALERT level.
ALERT SeverityLevel = iota
// A critical error has occured. Critical errors are such which can be tough to fix or made the users
// experience significantly worse but unlike errors that trigger ALERT they can be fixed at any moment.
CRIT
// An error has occured but it is not expected to cause any serious issues. These will be often
// `Internal Server Error` responses from an HTTP server.
ERR
WARNING
NOTICE
// Signals that something suspicious has happened, for example, a query took too long to execute, gaining access
// to a resource took multiple attempts, a conflict was automatically resolved, etc.
WARN
// Used to inform about standard, expected events of an application, like creation of a new object or a new
// log-in from a user. Information that could be:
// - used to audit the application,
// - resolve customer's complaints,
// - track history of significant changes,
// - calculate valuable statistics;
// should be collected and logged at this level.
INFO
// Verbose information that is useful and meaningful to application developers and system administrators.
DEBUG
// Extremely verbose information that can be used to investigate performance of specific parts of an application.
// It is transled to [syslog.DEBUG] by [IntoSyslogLevel].
TRACE
)
// Level Keyword Description
// 0 emergencies System is unusable
// 1 alerts Immediate action is needed
// 2 critical Critical conditions exist
// 3 errors Error conditions exist
// 4 warnings Warning conditions exist
// 5 notification Normal, but significant, conditions exist
// 6 informational Informational messages
// 7 debugging Debugging messages
func (l SyslogLevel) String() string {
func (l SeverityLevel) String() string {
switch l {
case EMERG:
return "EMERG"
case ALERT:
return "ALERT"
case CRIT:
return "CRIT"
case ERR:
return "ERR"
case WARNING:
case WARN:
return "WARN"
case NOTICE:
return "NOTICE"
case INFO:
return "INFO"
case DEBUG:
@ -51,10 +55,8 @@ func (l SyslogLevel) String() string {
}
}
func LevelFromString(level string) SyslogLevel {
func FromString(level string) SeverityLevel {
switch level {
case "EMERG":
return EMERG
case "ALERT":
return ALERT
case "CRIT":
@ -62,9 +64,7 @@ func LevelFromString(level string) SyslogLevel {
case "ERR":
return ERR
case "WARN":
return WARNING
case "NOTICE":
return NOTICE
return WARN
case "INFO":
return INFO
case "DEBUG":
@ -74,11 +74,27 @@ func LevelFromString(level string) SyslogLevel {
}
}
func (lvl SyslogLevel) SetAttribute(att ...attribute.KeyValue) trace.SpanStartEventOption {
att = append(att, attribute.Int("level", int(lvl)))
return trace.WithAttributes(
att...,
)
func (lvl SeverityLevel) IntoTraceAttribute() attribute.KeyValue {
return attribute.String("level", lvl.String())
}
func (lvl SeverityLevel) IntoSyslogLevel() syslog.SyslogLevel {
switch lvl {
case ALERT:
return syslog.ALERT
case CRIT:
return syslog.CRIT
case ERR:
return syslog.ERR
case WARN:
return syslog.WARNING
case INFO:
return syslog.INFO
case DEBUG:
return syslog.DEBUG
case TRACE:
return syslog.DEBUG
default:
return syslog.EMERG
}
}