mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# 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]>
301 lines
6.6 KiB
Go
301 lines
6.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"database/sql/driver"
|
|
_ "embed"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"github.com/shopspring/decimal"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/database/mock"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
func TestHandler_updateLastUpdated(t *testing.T) {
|
|
type fields struct {
|
|
projection Projection
|
|
mock *mock.SQLMock
|
|
}
|
|
type args struct {
|
|
updatedState *state
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
isErr func(t *testing.T, err error)
|
|
}{
|
|
{
|
|
name: "update fails",
|
|
fields: fields{
|
|
projection: &projection{
|
|
name: "instance",
|
|
},
|
|
mock: mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExcpectExec(updateStateStmt,
|
|
mock.WithExecErr(sql.ErrTxDone),
|
|
),
|
|
),
|
|
},
|
|
args: args{
|
|
updatedState: &state{
|
|
instanceID: "instance",
|
|
eventTimestamp: time.Now(),
|
|
position: decimal.NewFromInt(42),
|
|
},
|
|
},
|
|
isErr: func(t *testing.T, err error) {
|
|
if !errors.Is(err, sql.ErrTxDone) {
|
|
t.Errorf("unexpected error, want: %v, got %v", sql.ErrTxDone, err)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "no rows affected",
|
|
fields: fields{
|
|
projection: &projection{
|
|
name: "instance",
|
|
},
|
|
mock: mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExcpectExec(updateStateStmt,
|
|
mock.WithExecNoRowsAffected(),
|
|
),
|
|
),
|
|
},
|
|
args: args{
|
|
updatedState: &state{
|
|
instanceID: "instance",
|
|
eventTimestamp: time.Now(),
|
|
position: decimal.NewFromInt(42),
|
|
},
|
|
},
|
|
isErr: func(t *testing.T, err error) {
|
|
if !errors.Is(err, zerrors.ThrowInternal(nil, "V2-FGEKi", "")) {
|
|
t.Errorf("unexpected error, want: %v, got %v", sql.ErrTxDone, err)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
name: "success",
|
|
fields: fields{
|
|
projection: &projection{
|
|
name: "projection",
|
|
},
|
|
mock: mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExcpectExec(updateStateStmt,
|
|
mock.WithExecArgs(
|
|
"projection",
|
|
"instance",
|
|
"aggregate id",
|
|
eventstore.AggregateType("aggregate type"),
|
|
uint64(42),
|
|
mock.AnyType[time.Time]{},
|
|
decimal.NewFromInt(42),
|
|
uint32(0),
|
|
),
|
|
mock.WithExecRowsAffected(1),
|
|
),
|
|
),
|
|
},
|
|
args: args{
|
|
updatedState: &state{
|
|
instanceID: "instance",
|
|
eventTimestamp: time.Now(),
|
|
position: decimal.NewFromInt(42),
|
|
aggregateType: "aggregate type",
|
|
aggregateID: "aggregate id",
|
|
sequence: 42,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
if tt.isErr == nil {
|
|
tt.isErr = func(t *testing.T, err error) {
|
|
if err != nil {
|
|
t.Error("expected no error got:", err)
|
|
}
|
|
}
|
|
}
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tx, err := tt.fields.mock.DB.BeginTx(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatalf("unable to begin transaction: %v", err)
|
|
}
|
|
|
|
h := &Handler{
|
|
projection: tt.fields.projection,
|
|
}
|
|
err = h.setState(t.Context(), tx, tt.args.updatedState)
|
|
|
|
tt.isErr(t, err)
|
|
tt.fields.mock.Assert(t)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHandler_currentState(t *testing.T) {
|
|
testTime := time.Now()
|
|
type fields struct {
|
|
projection Projection
|
|
mock *mock.SQLMock
|
|
}
|
|
type args struct {
|
|
ctx context.Context
|
|
}
|
|
type want struct {
|
|
currentState *state
|
|
isErr func(t *testing.T, err error)
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
want want
|
|
}{
|
|
{
|
|
name: "connection done",
|
|
fields: fields{
|
|
projection: &projection{
|
|
name: "projection",
|
|
},
|
|
mock: mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExpectQuery(currentStateStmt,
|
|
mock.WithQueryArgs(
|
|
"instance",
|
|
"projection",
|
|
),
|
|
mock.WithQueryErr(sql.ErrConnDone),
|
|
),
|
|
),
|
|
},
|
|
args: args{
|
|
ctx: authz.WithInstanceID(context.Background(), "instance"),
|
|
},
|
|
want: want{
|
|
isErr: func(t *testing.T, err error) {
|
|
if !errors.Is(err, sql.ErrConnDone) {
|
|
t.Errorf("unexpected error, want: %v, got: %v", sql.ErrConnDone, err)
|
|
}
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "state locked",
|
|
fields: fields{
|
|
projection: &projection{
|
|
name: "projection",
|
|
},
|
|
mock: mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExpectQuery(currentStateStmt,
|
|
mock.WithQueryArgs(
|
|
"instance",
|
|
"projection",
|
|
),
|
|
mock.WithQueryErr(&pgconn.PgError{Code: "55P03"}),
|
|
),
|
|
),
|
|
},
|
|
args: args{
|
|
ctx: authz.WithInstanceID(context.Background(), "instance"),
|
|
},
|
|
want: want{
|
|
isErr: func(t *testing.T, err error) {
|
|
pgErr := new(pgconn.PgError)
|
|
if !errors.As(err, &pgErr) {
|
|
t.Errorf("error should be PgErr but was %T", err)
|
|
return
|
|
}
|
|
if pgErr.Code != "55P03" {
|
|
t.Errorf("expected code 55P03 got: %s", pgErr.Code)
|
|
}
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "success",
|
|
fields: fields{
|
|
projection: &projection{
|
|
name: "projection",
|
|
},
|
|
mock: mock.NewSQLMock(t,
|
|
mock.ExpectBegin(nil),
|
|
mock.ExpectQuery(currentStateStmt,
|
|
mock.WithQueryArgs(
|
|
"instance",
|
|
"projection",
|
|
),
|
|
mock.WithQueryResult(
|
|
[]string{"aggregate_id", "aggregate_type", "event_sequence", "event_date", "position", "offset"},
|
|
[][]driver.Value{
|
|
{
|
|
"aggregate id",
|
|
"aggregate type",
|
|
int64(42),
|
|
testTime,
|
|
decimal.NewFromInt(42).String(),
|
|
uint16(10),
|
|
},
|
|
},
|
|
),
|
|
),
|
|
),
|
|
},
|
|
args: args{
|
|
ctx: authz.WithInstanceID(context.Background(), "instance"),
|
|
},
|
|
want: want{
|
|
currentState: &state{
|
|
instanceID: "instance",
|
|
eventTimestamp: testTime,
|
|
position: decimal.NewFromInt(42),
|
|
aggregateType: "aggregate type",
|
|
aggregateID: "aggregate id",
|
|
sequence: 42,
|
|
offset: 10,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
if tt.want.isErr == nil {
|
|
tt.want.isErr = func(t *testing.T, err error) {
|
|
if err != nil {
|
|
t.Error("expected no error got:", err)
|
|
}
|
|
}
|
|
}
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
h := &Handler{
|
|
projection: tt.fields.projection,
|
|
}
|
|
|
|
tx, err := tt.fields.mock.DB.BeginTx(context.Background(), nil)
|
|
if err != nil {
|
|
t.Fatalf("unable to begin transaction: %v", err)
|
|
}
|
|
|
|
gotCurrentState, err := h.currentState(tt.args.ctx, tx)
|
|
|
|
tt.want.isErr(t, err)
|
|
if !reflect.DeepEqual(gotCurrentState, tt.want.currentState) {
|
|
t.Errorf("Handler.currentState() gotCurrentState = %v, want %v", gotCurrentState, tt.want.currentState)
|
|
}
|
|
tt.fields.mock.Assert(t)
|
|
})
|
|
}
|
|
}
|