mirror of
https://github.com/zitadel/zitadel.git
synced 2026-07-25 18:28:00 +00:00
# Which Problems Are Solved Zitadel did not provide easy correlation between errors, logs, traces and metrics. The configuration for those instrumentations was also not consistent, with some supporting different exporters then others. Implementation and parsing of config was also spaghettified over multiple packages, with awkward parsing and inconsistent naming of options. # How the Problems Are Solved All telemetry is now merged under the name "instrumentation". Why? 1. We thought it was a good idea in the past to call the milestone exporter `Telemtry` in the runtime config. Calling this `TelemetryV2` looks weird. 2. Not everything is a meter and not everything is sent (tele...). 3. It's also [defined](https://opentelemetry.io/docs/concepts/instrumentation/) as such by the OTEL documentation. ## New features - Adds structured, context based logging with trace-ID awareness - Static log fields are added to the context, such as service and request path - Static log fields are injected in each logline emitted by the application - Structured logs can also be send to an otel exporter - Structured logs can be printed to StdErr in text and JSON format - Error sinks make sure every error is logged at the correct level: - Warnings for client side errors (HTTP 400 range, Invalid request etc) - Error for server side errors (Internal server errors) - Metrics can now also be send to a OTEL collector. (previously they could only be scraped from `/debug/metrics` with prometheus) ## Exporters This change adds all the exporters supported by OTEL upstream and some google specific exporters for our cloud deployment. - StdOut / StdErr: all instrumentations - OTEL gRPC / HTTP: all instrumentations - Google: all instrumentations except logging - Prometheus (pull-based): only metrics The exception is profiling, which only supports the google exporting due to lack of support by OTEL upstream. ## Configuration and structure - All instrumentation is moved into the new `backend/v3/instrumentation` package. It reuses configuration types, so both code and runtime configuration are easier to understand. - The `internal/telemetry` packages are removed. - Instrumentation is started with a single function and a proper shutdown function is now provided. - Legacy configuration is still parsed from the runtime config, as long as the new configuration is disabled. This allows backporting this feature to v4 without breaking existing configurations. # Additional Changes - Devcontainer: set `$PATH` variable so installed go binaries can be run individually, without NX. - NX: install GCI tool to fix imports # Additional Context - Closes https://github.com/zitadel/zitadel/issues/8408 - Closes https://github.com/zitadel/zitadel/issues/6664 - Backport to v4
291 lines
6.7 KiB
Go
291 lines
6.7 KiB
Go
package http
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
func TestZitadelErrorToHTTPStatusCode(t *testing.T) {
|
|
type args struct {
|
|
err error
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantStatusCode int
|
|
wantOk bool
|
|
}{
|
|
{
|
|
name: "no error",
|
|
args: args{
|
|
err: nil,
|
|
},
|
|
wantStatusCode: http.StatusOK,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped already exists",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowAlreadyExists(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusConflict,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped deadline exceeded",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowDeadlineExceeded(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusGatewayTimeout,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped internal",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowInternal(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusInternalServerError,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped invalid argument",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowInvalidArgument(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusBadRequest,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped not found",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowNotFound(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusNotFound,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped permission denied",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowPermissionDenied(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusForbidden,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped precondition failed",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowPreconditionFailed(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusBadRequest,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped unauthenticated",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowUnauthenticated(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusUnauthorized,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped unavailable",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowUnavailable(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusServiceUnavailable,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped unimplemented",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowUnimplemented(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusNotImplemented,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "wrapped resource exhausted",
|
|
args: args{
|
|
err: fmt.Errorf("wrapped %w", zerrors.ThrowResourceExhausted(nil, "id", "message")),
|
|
},
|
|
wantStatusCode: http.StatusTooManyRequests,
|
|
wantOk: true,
|
|
},
|
|
{
|
|
name: "no zitadel error",
|
|
args: args{
|
|
err: errors.New("error"),
|
|
},
|
|
wantStatusCode: http.StatusInternalServerError,
|
|
wantOk: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotStatusCode, gotOk := ZitadelErrorToHTTPStatusCode(t.Context(), tt.args.err)
|
|
if gotStatusCode != tt.wantStatusCode {
|
|
t.Errorf("ZitadelErrorToHTTPStatusCode() gotStatusCode = %v, want %v", gotStatusCode, tt.wantStatusCode)
|
|
}
|
|
if gotOk != tt.wantOk {
|
|
t.Errorf("ZitadelErrorToHTTPStatusCode() gotOk = %v, want %v", gotOk, tt.wantOk)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHTTPStatusCodeToZitadelError(t *testing.T) {
|
|
type args struct {
|
|
statusCode int
|
|
id string
|
|
message string
|
|
parent error
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
wantErr error
|
|
}{
|
|
{
|
|
name: "StatusOK",
|
|
args: args{
|
|
statusCode: http.StatusOK,
|
|
},
|
|
wantErr: nil,
|
|
},
|
|
{
|
|
name: "StatusConflict",
|
|
args: args{
|
|
statusCode: http.StatusConflict,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowAlreadyExists(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusGatewayTimeout",
|
|
args: args{
|
|
statusCode: http.StatusGatewayTimeout,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowDeadlineExceeded(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusInternalServerError",
|
|
args: args{
|
|
statusCode: http.StatusInternalServerError,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowInternal(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusBadRequest",
|
|
args: args{
|
|
statusCode: http.StatusBadRequest,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowInvalidArgument(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusNotFound",
|
|
args: args{
|
|
statusCode: http.StatusNotFound,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowNotFound(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusForbidden",
|
|
args: args{
|
|
statusCode: http.StatusForbidden,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowPermissionDenied(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusUnauthorized",
|
|
args: args{
|
|
statusCode: http.StatusUnauthorized,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowUnauthenticated(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusServiceUnavailable",
|
|
args: args{
|
|
statusCode: http.StatusServiceUnavailable,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowUnavailable(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusNotImplemented",
|
|
args: args{
|
|
statusCode: http.StatusNotImplemented,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowUnimplemented(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "StatusTooManyRequests",
|
|
args: args{
|
|
statusCode: http.StatusTooManyRequests,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowResourceExhausted(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "Unknown",
|
|
args: args{
|
|
statusCode: 1000,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowUnknown(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "Unknown, test for statuscode",
|
|
args: args{
|
|
statusCode: 1000,
|
|
id: "id",
|
|
message: "message",
|
|
},
|
|
wantErr: zerrors.ThrowError(nil, "id", "message"),
|
|
},
|
|
{
|
|
name: "Unknown with parent",
|
|
args: args{
|
|
statusCode: 1000,
|
|
id: "id",
|
|
message: "message",
|
|
parent: io.EOF,
|
|
},
|
|
wantErr: zerrors.ThrowUnknown(io.EOF, "id", "message"),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := HTTPStatusCodeToZitadelError(tt.args.parent, tt.args.statusCode, tt.args.id, tt.args.message)
|
|
assert.ErrorIs(t, err, tt.wantErr)
|
|
if tt.args.parent != nil {
|
|
assert.ErrorIs(t, err, tt.args.parent)
|
|
}
|
|
})
|
|
}
|
|
}
|