mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
* fork: own SigNoz platform deps under hanzoai, repath imports off upstream brand
Phase 1 of the full-fork mandate: eliminate upstream-branded SigNoz modules,
own them as public hanzoai forks, and repath all imports to hanzoai paths.
Owned deps (new public hanzoai forks, module renamed to hanzoai path):
github.com/SigNoz/signoz-otel-collector v0.144.3
-> github.com/hanzoai/signoz-otel-collector v0.144.6 (module renamed)
github.com/SigNoz/clickhouse-go-mock v0.14.0
-> github.com/hanzoai/clickhouse-go-mock v0.14.1 (module renamed)
github.com/SigNoz/govaluate v0.0.0-20240203125216-988004ccc7fd
-> github.com/hanzoai/govaluate v0.1.0 (module renamed)
github.com/SigNoz/expr (replace target for github.com/expr-lang/expr)
-> github.com/hanzoai/expr v1.17.8 (upstream module path preserved,
consumed via replace exactly like the prior SigNoz/expr fork)
In-repo repath: the vendored SigNoz source tree in pkg/, ee/, cmd/ is now
self-referenced as github.com/hanzoai/o11y/... instead of
github.com/SigNoz/signoz/... (1300+ import lines across 377 files), matching
the 750 files already on the hanzoai path. Zero SigNoz-branded module
references remain in go.mod or any .go import.
Generic ecosystem libraries (golang.org/x, google.golang.org, prometheus,
opentelemetry, gin, cobra, luxfi/*, etc.) are left untouched — only the
SigNoz platform fork is owned.
Pre-existing (NOT introduced here): origin/main does not `go build ./...`
because 22 packages are imported but absent from the vendored tree; 14 of
them (roletypes, grammar, cloudintegrations, resourcefilter, integrationtypes,
metricsexplorer, metrics_explorer, times, agg_rewrite, bucket_cache,
sqlitesqlstore, sqlschema/postgressqlschema, sqlstore/postgressqlstore,
cloudintegrations/services) exist in no upstream SigNoz version and must be
vendored into hanzoai/o11y from hanzoai/signoz@770f464f to finish. This
repath is orthogonal to that gap.
* docs(LLM.md): fork boundary map + pre-existing compile-gap inventory
---------
Co-authored-by: Hanzo Dev <[email protected]>
165 lines
4.0 KiB
Go
165 lines
4.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/o11y/pkg/errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRecovery(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
handler http.HandlerFunc
|
|
wantStatus int
|
|
wantLog bool
|
|
wantMessage string
|
|
wantErrorStatus bool
|
|
}{
|
|
{
|
|
name: "PanicWithString",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
panic("something went wrong")
|
|
},
|
|
wantStatus: http.StatusInternalServerError,
|
|
wantLog: true,
|
|
wantMessage: "something went wrong",
|
|
wantErrorStatus: true,
|
|
},
|
|
{
|
|
name: "PanicWithError",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
panic(errors.New(errors.TypeInternal, errors.CodeInternal, "db connection failed"))
|
|
},
|
|
wantStatus: http.StatusInternalServerError,
|
|
wantLog: true,
|
|
wantMessage: "db connection failed",
|
|
wantErrorStatus: true,
|
|
},
|
|
{
|
|
name: "PanicWithInteger",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
panic(42)
|
|
},
|
|
wantStatus: http.StatusInternalServerError,
|
|
wantLog: true,
|
|
wantMessage: "42",
|
|
wantErrorStatus: true,
|
|
},
|
|
{
|
|
name: "PanicWithDivisionByZero",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
divisor := 0
|
|
_ = 1 / divisor
|
|
},
|
|
wantStatus: http.StatusInternalServerError,
|
|
wantLog: true,
|
|
wantMessage: "runtime error: integer divide by zero",
|
|
wantErrorStatus: true,
|
|
},
|
|
{
|
|
name: "NoPanic",
|
|
handler: func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
},
|
|
wantStatus: http.StatusOK,
|
|
wantLog: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var records []slog.Record
|
|
logger := slog.New(newRecordCollector(&records))
|
|
|
|
m := NewRecovery(logger)
|
|
handler := m.Wrap(http.HandlerFunc(tc.handler))
|
|
|
|
rr := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
assert.Equal(t, tc.wantStatus, rr.Code)
|
|
|
|
if !tc.wantLog {
|
|
assert.Empty(t, records)
|
|
return
|
|
}
|
|
|
|
require.Len(t, records, 1)
|
|
|
|
err := extractException(t, records[0])
|
|
require.NotNil(t, err)
|
|
|
|
typ, _, message, _, _, _ := errors.Unwrapb(err)
|
|
assert.Equal(t, errors.TypeFatal, typ)
|
|
assert.Equal(t, tc.wantMessage, message)
|
|
|
|
type stacktracer interface {
|
|
Stacktrace() string
|
|
}
|
|
st, ok := err.(stacktracer)
|
|
require.True(t, ok, "error should implement stacktracer")
|
|
assert.NotEmpty(t, st.Stacktrace())
|
|
|
|
if tc.wantErrorStatus {
|
|
var body map[string]any
|
|
require.NoError(t, json.Unmarshal(rr.Body.Bytes(), &body))
|
|
assert.Equal(t, "error", body["status"])
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// extractException finds the "exception" attr in a log record and returns the error.
|
|
func extractException(t *testing.T, record slog.Record) error {
|
|
t.Helper()
|
|
var found error
|
|
record.Attrs(func(a slog.Attr) bool {
|
|
if a.Key == "exception" {
|
|
if err, ok := a.Value.Any().(error); ok {
|
|
found = err
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
return found
|
|
}
|
|
|
|
// recordCollector is an slog.Handler that collects records for assertion.
|
|
type recordCollector struct {
|
|
records *[]slog.Record
|
|
attrs []slog.Attr
|
|
}
|
|
|
|
func newRecordCollector(records *[]slog.Record) *recordCollector {
|
|
return &recordCollector{records: records}
|
|
}
|
|
|
|
func (h *recordCollector) Enabled(_ context.Context, _ slog.Level) bool { return true }
|
|
|
|
func (h *recordCollector) Handle(_ context.Context, record slog.Record) error {
|
|
for _, a := range h.attrs {
|
|
record.AddAttrs(a)
|
|
}
|
|
*h.records = append(*h.records, record)
|
|
return nil
|
|
}
|
|
|
|
func (h *recordCollector) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return &recordCollector{records: h.records, attrs: append(h.attrs, attrs...)}
|
|
}
|
|
|
|
func (h *recordCollector) WithGroup(_ string) slog.Handler { return h }
|