feat(logging): add streams (#11435)

# Which Problems Are Solved

Streams allow differentiating logs produced by different components of
Zitadel.

# How the Problems Are Solved

The `backend/v3/instrumentation/logging` package now exposes convenience
function for setting and getting a logger from the context. As well as
high-level functions to emit log records at various levels. When
constructing a new logger a "stream" needs to be specified:

- **runtime**: General runtime logs, such as startup and shutdown
messages. Default for logs that do not belong to the other categories.
- **request**: Logs for incoming API and HTTP requests.
- **event_handler**: Logs for event handling in projections.
- **queue**: Logs for the job queue processing.
- **event_pusher**: Logs for event pushing to the database. Disabled by
default, contains sensitive information.

Each line from the returned logger contains a `stream` field as well as
a `version` field with the current Zitadel version.

## Runtime config

Streams can be enabled by passing an array of stream names in the
runtime config. Because some log streams may contain sensitive data
(especially events), it is now also possible to mask values by their
key.

# Additional Changes

- Wrap `slogctx` in the `logging` package. (Except API error converter
packages, because of import cycle)
- Add some docs to `logging` package so other devs understand how to add
logging to Zitadel
- Add `logging.OnError` and `logging.WithError` helper functions with
`Panic()` and `Fatal()` methods, to preserve current calls in the `cmd`
packages.
- Add instance context extractor.
- Only output request details in the request info log. Request ID
remains propagated through context.
- Moved middleware functionality into protocol specific packages. 
- Removed setting of URI to context in metric middleware. There were ony
setters and no getters. (Unused value)
- Reuse a single statusWriter in the middleware package for middlewares
that need to know the response status.

# Additional Context

- Closes #11333
- Closes #11331 
- Partly #11330

---------

Co-authored-by: Silvan <[email protected]>
This commit is contained in:
Tim Möhlmann
2026-02-04 11:51:43 +01:00
committed by GitHub
co-authored by Silvan
parent b99271755b
commit 11dbb1b277
112 changed files with 2679 additions and 981 deletions
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowAlreadyExists(parent error, id, message string) error {
return newZitadelError(KindAlreadyExists, parent, id, message)
return CreateZitadelError(KindAlreadyExists, parent, id, message, 1)
}
func ThrowAlreadyExistsf(parent error, id, format string, a ...any) error {
return newZitadelError(KindAlreadyExists, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindAlreadyExists, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsErrorAlreadyExists(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowDeadlineExceeded(parent error, id, message string) error {
return newZitadelError(KindDeadlineExceeded, parent, id, message)
return CreateZitadelError(KindDeadlineExceeded, parent, id, message, 1)
}
func ThrowDeadlineExceededf(parent error, id, format string, a ...any) error {
return newZitadelError(KindDeadlineExceeded, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindDeadlineExceeded, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsDeadlineExceeded(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowInternal(parent error, id, message string) error {
return newZitadelError(KindInternal, parent, id, message)
return CreateZitadelError(KindInternal, parent, id, message, 1)
}
func ThrowInternalf(parent error, id, format string, a ...any) error {
return newZitadelError(KindInternal, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindInternal, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsInternal(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowInvalidArgument(parent error, id, message string) error {
return newZitadelError(KindInvalidArgument, parent, id, message)
return CreateZitadelError(KindInvalidArgument, parent, id, message, 1)
}
func ThrowInvalidArgumentf(parent error, id, format string, a ...any) error {
return newZitadelError(KindInvalidArgument, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindInvalidArgument, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsErrorInvalidArgument(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowNotFound(parent error, id, message string) error {
return newZitadelError(KindNotFound, parent, id, message)
return CreateZitadelError(KindNotFound, parent, id, message, 1)
}
func ThrowNotFoundf(parent error, id, format string, a ...any) error {
return newZitadelError(KindNotFound, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindNotFound, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsNotFound(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowPermissionDenied(parent error, id, message string) error {
return newZitadelError(KindPermissionDenied, parent, id, message)
return CreateZitadelError(KindPermissionDenied, parent, id, message, 1)
}
func ThrowPermissionDeniedf(parent error, id, format string, a ...any) error {
return newZitadelError(KindPermissionDenied, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindPermissionDenied, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsPermissionDenied(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowPreconditionFailed(parent error, id, message string) error {
return newZitadelError(KindPreconditionFailed, parent, id, message)
return CreateZitadelError(KindPreconditionFailed, parent, id, message, 1)
}
func ThrowPreconditionFailedf(parent error, id, format string, a ...any) error {
return newZitadelError(KindPreconditionFailed, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindPreconditionFailed, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsPreconditionFailed(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowResourceExhausted(parent error, id, message string) error {
return newZitadelError(KindResourceExhausted, parent, id, message)
return CreateZitadelError(KindResourceExhausted, parent, id, message, 1)
}
func ThrowResourceExhaustedf(parent error, id, format string, a ...any) error {
return newZitadelError(KindResourceExhausted, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindResourceExhausted, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsResourceExhausted(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowUnauthenticated(parent error, id, message string) error {
return newZitadelError(KindUnauthenticated, parent, id, message)
return CreateZitadelError(KindUnauthenticated, parent, id, message, 1)
}
func ThrowUnauthenticatedf(parent error, id, format string, a ...any) error {
return newZitadelError(KindUnauthenticated, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindUnauthenticated, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsUnauthenticated(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowUnavailable(parent error, id, message string) error {
return newZitadelError(KindUnavailable, parent, id, message)
return CreateZitadelError(KindUnavailable, parent, id, message, 1)
}
func ThrowUnavailablef(parent error, id, format string, a ...any) error {
return newZitadelError(KindUnavailable, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindUnavailable, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsUnavailable(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowUnimplemented(parent error, id, message string) error {
return newZitadelError(KindUnimplemented, parent, id, message)
return CreateZitadelError(KindUnimplemented, parent, id, message, 1)
}
func ThrowUnimplementedf(parent error, id, format string, a ...any) error {
return newZitadelError(KindUnimplemented, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindUnimplemented, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsUnimplemented(err error) bool {
+2 -2
View File
@@ -3,11 +3,11 @@ package zerrors
import "fmt"
func ThrowUnknown(parent error, id, message string) error {
return newZitadelError(KindUnknown, parent, id, message)
return CreateZitadelError(KindUnknown, parent, id, message, 1)
}
func ThrowUnknownf(parent error, id, format string, a ...any) error {
return newZitadelError(KindUnknown, parent, id, fmt.Sprintf(format, a...))
return CreateZitadelError(KindUnknown, parent, id, fmt.Sprintf(format, a...), 1)
}
func IsUnknown(err error) bool {
+19 -8
View File
@@ -117,23 +117,34 @@ type ZitadelError struct {
}
func ThrowError(parent error, id, message string) error {
return newZitadelError(KindUnknown, parent, id, message)
return CreateZitadelError(KindUnknown, parent, id, message, 1)
}
func CreateZitadelError(kind Kind, parent error, id, message string) *ZitadelError {
return newZitadelError(kind, parent, id, message)
}
func newZitadelError(kind Kind, parent error, id, message string) *ZitadelError {
// CreateZitadelError creates a ZitadelError of the given kind.
// If ReportLocation or StackTrace are enabled, they are added to the error.
// The skip parameter defines how many stack frames to skip when determining
// the report location.
// A skip value of 0 means the location where CreateZitadelError is called.
// When the parent error is also a ZitadelError, the report location and stack trace
// are inherited from the parent error.
func CreateZitadelError(kind Kind, parent error, id, message string, skip int) *ZitadelError {
err := &ZitadelError{
Kind: kind,
Parent: parent,
ID: id,
Message: message,
}
var target *ZitadelError
if errors.As(parent, &target) {
// inherit stack trace and report location from parent error
err.reportLocation = target.reportLocation
err.stackTrace = target.stackTrace
err.hasStackTrace = target.hasStackTrace
return err
}
if enableReportLocation.Load() {
// skip 2: newZitadelError + Create / Throw function.
err.reportLocation = sloggcp.NewReportLocation(2)
err.reportLocation = sloggcp.NewReportLocation(skip + 1)
}
if enableStackTrace.Load() {
err.stackTrace = debug.Stack()
+6 -6
View File
@@ -19,7 +19,7 @@ func TestErrorMethod(t *testing.T) {
func TestZitadelError_Is(t *testing.T) {
parent := errors.New("parent error")
target := newZitadelError(KindAborted, parent, "id", "message")
target := CreateZitadelError(KindAborted, parent, "id", "message", 0)
tests := []struct {
name string // description of this test case
err error
@@ -32,27 +32,27 @@ func TestZitadelError_Is(t *testing.T) {
},
{
name: "different kind",
err: newZitadelError(KindNotFound, parent, "id", "message"),
err: CreateZitadelError(KindNotFound, parent, "id", "message", 0),
want: false,
},
{
name: "different id",
err: newZitadelError(KindAborted, parent, "otherID", "message"),
err: CreateZitadelError(KindAborted, parent, "otherID", "message", 0),
want: false,
},
{
name: "different message",
err: newZitadelError(KindAborted, parent, "id", "other message"),
err: CreateZitadelError(KindAborted, parent, "id", "other message", 0),
want: false,
},
{
name: "different parent",
err: newZitadelError(KindAborted, errors.New("other parent"), "id", "message"),
err: CreateZitadelError(KindAborted, errors.New("other parent"), "id", "message", 0),
want: false,
},
{
name: "same error",
err: newZitadelError(KindAborted, parent, "id", "message"),
err: CreateZitadelError(KindAborted, parent, "id", "message", 0),
want: true,
},
}