@langwatch/server 3.1.0-beta.8 → 3.1.0-beta.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -6
- package/packages/server/dist/cli.cjs +19 -16
- package/cmd/service/main.go +0 -68
- package/go.mod +0 -117
- package/go.sum +0 -299
- package/pkg/breaker/breaker.go +0 -157
- package/pkg/breaker/breaker_test.go +0 -131
- package/pkg/clog/LICENSE +0 -26
- package/pkg/clog/clog.go +0 -90
- package/pkg/clog/clog_test.go +0 -56
- package/pkg/clog/panic.go +0 -39
- package/pkg/config/LICENSE +0 -26
- package/pkg/config/config.go +0 -86
- package/pkg/config/config_test.go +0 -70
- package/pkg/config/otel.go +0 -50
- package/pkg/config/server.go +0 -61
- package/pkg/config/validate.go +0 -80
- package/pkg/config/validate_test.go +0 -74
- package/pkg/contexts/contexts_test.go +0 -34
- package/pkg/contexts/request_id.go +0 -1
- package/pkg/contexts/service_info.go +0 -33
- package/pkg/errfuncs/errfuncs.go +0 -9
- package/pkg/errfuncs/errfuncs_test.go +0 -28
- package/pkg/forkedcontext/forkedcontext.go +0 -57
- package/pkg/health/health.go +0 -123
- package/pkg/health/health_test.go +0 -84
- package/pkg/herr/LICENSE +0 -26
- package/pkg/herr/code.go +0 -14
- package/pkg/herr/herr.go +0 -112
- package/pkg/herr/herr_test.go +0 -78
- package/pkg/herr/http.go +0 -133
- package/pkg/herr/http_test.go +0 -103
- package/pkg/herr/validate.go +0 -72
- package/pkg/herr/validate_test.go +0 -75
- package/pkg/httpmiddleware/bearer.go +0 -47
- package/pkg/httpmiddleware/httputil_test.go +0 -167
- package/pkg/httpmiddleware/inflight.go +0 -23
- package/pkg/httpmiddleware/maxbody.go +0 -30
- package/pkg/httpmiddleware/recover.go +0 -38
- package/pkg/httpmiddleware/requestid.go +0 -40
- package/pkg/httpmiddleware/telemetry.go +0 -96
- package/pkg/httpmiddleware/version.go +0 -16
- package/pkg/jwtverify/verifier.go +0 -72
- package/pkg/jwtverify/verifier_test.go +0 -134
- package/pkg/ksuid/LICENSE +0 -26
- package/pkg/ksuid/ksuid.go +0 -157
- package/pkg/ksuid/ksuid_test.go +0 -107
- package/pkg/ksuid/resources.go +0 -13
- package/pkg/lifecycle/group.go +0 -175
- package/pkg/lifecycle/group_test.go +0 -236
- package/pkg/lifecycle/service.go +0 -112
- package/pkg/netprobe/netprobe.go +0 -89
- package/pkg/netprobe/netprobe_test.go +0 -55
- package/pkg/otelsetup/otelsetup.go +0 -226
- package/pkg/otelsetup/otelsetup_test.go +0 -130
- package/pkg/ptr/LICENSE +0 -26
- package/pkg/ptr/ptr.go +0 -34
- package/pkg/ptr/ptr_test.go +0 -66
- package/pkg/retry/bench_test.go +0 -67
- package/pkg/retry/retry.go +0 -188
- package/pkg/retry/retry_test.go +0 -222
- package/pkg/slicefuncs/LICENSE +0 -26
- package/pkg/slicefuncs/slicefuncs.go +0 -10
- package/pkg/slicefuncs/slicefuncs_test.go +0 -29
- package/pkg/stacktrace/LICENSE +0 -26
- package/pkg/stacktrace/frames.go +0 -77
- package/pkg/stacktrace/frames_test.go +0 -73
- package/services/aigateway/BENCHMARKS.md +0 -110
- package/services/aigateway/README.md +0 -156
- package/services/aigateway/adapters/authresolver/bench_test.go +0 -13
- package/services/aigateway/adapters/authresolver/redis.go +0 -61
- package/services/aigateway/adapters/authresolver/service.go +0 -442
- package/services/aigateway/adapters/authresolver/service_test.go +0 -435
- package/services/aigateway/adapters/budget/bench_test.go +0 -50
- package/services/aigateway/adapters/budget/budget.go +0 -59
- package/services/aigateway/adapters/cacherules/evaluator.go +0 -134
- package/services/aigateway/adapters/cacherules/evaluator_test.go +0 -207
- package/services/aigateway/adapters/controlplane/bench_test.go +0 -40
- package/services/aigateway/adapters/controlplane/client.go +0 -213
- package/services/aigateway/adapters/controlplane/client_test.go +0 -73
- package/services/aigateway/adapters/controlplane/config_wire.go +0 -310
- package/services/aigateway/adapters/controlplane/guardrails.go +0 -88
- package/services/aigateway/adapters/controlplane/signer.go +0 -95
- package/services/aigateway/adapters/controlplane/signer_test.go +0 -64
- package/services/aigateway/adapters/customertracebridge/context.go +0 -48
- package/services/aigateway/adapters/customertracebridge/context_test.go +0 -24
- package/services/aigateway/adapters/customertracebridge/emitter.go +0 -329
- package/services/aigateway/adapters/customertracebridge/emitter_test.go +0 -93
- package/services/aigateway/adapters/customertracebridge/registry.go +0 -82
- package/services/aigateway/adapters/customertracebridge/transport.go +0 -122
- package/services/aigateway/adapters/gatewaytracer/attrs.go +0 -28
- package/services/aigateway/adapters/gatewaytracer/attrs_genai.go +0 -24
- package/services/aigateway/adapters/gatewaytracer/headers.go +0 -8
- package/services/aigateway/adapters/gatewaytracer/tracer.go +0 -101
- package/services/aigateway/adapters/httpapi/bench_test.go +0 -83
- package/services/aigateway/adapters/httpapi/middleware.go +0 -123
- package/services/aigateway/adapters/httpapi/router.go +0 -635
- package/services/aigateway/adapters/httpapi/router_test.go +0 -633
- package/services/aigateway/adapters/modelresolver/resolver.go +0 -107
- package/services/aigateway/adapters/modelresolver/resolver_test.go +0 -140
- package/services/aigateway/adapters/policy/matcher.go +0 -250
- package/services/aigateway/adapters/policy/matcher_test.go +0 -124
- package/services/aigateway/adapters/providers/bifrost.go +0 -798
- package/services/aigateway/adapters/providers/bifrost_parser.go +0 -201
- package/services/aigateway/adapters/providers/bifrost_test.go +0 -181
- package/services/aigateway/adapters/ratelimit/limiter.go +0 -122
- package/services/aigateway/adapters/ratelimit/limiter_test.go +0 -81
- package/services/aigateway/app/app.go +0 -104
- package/services/aigateway/app/dispatch.go +0 -89
- package/services/aigateway/app/dispatch_test.go +0 -475
- package/services/aigateway/app/handlers.go +0 -79
- package/services/aigateway/app/peek_bench_test.go +0 -21
- package/services/aigateway/app/pipeline/bench_test.go +0 -34
- package/services/aigateway/app/pipeline/budget.go +0 -55
- package/services/aigateway/app/pipeline/cache.go +0 -34
- package/services/aigateway/app/pipeline/cachecontrol.go +0 -86
- package/services/aigateway/app/pipeline/cachecontrol_test.go +0 -110
- package/services/aigateway/app/pipeline/guardrail.go +0 -121
- package/services/aigateway/app/pipeline/pipeline.go +0 -159
- package/services/aigateway/app/pipeline/policy.go +0 -23
- package/services/aigateway/app/pipeline/ratelimit.go +0 -21
- package/services/aigateway/app/pipeline/rawframing_test.go +0 -67
- package/services/aigateway/app/pipeline/resolve.go +0 -44
- package/services/aigateway/app/pipeline/trace.go +0 -129
- package/services/aigateway/app/ports.go +0 -61
- package/services/aigateway/cmd/root.go +0 -42
- package/services/aigateway/config.go +0 -86
- package/services/aigateway/deps.go +0 -177
- package/services/aigateway/domain/bundle.go +0 -203
- package/services/aigateway/domain/errors.go +0 -23
- package/services/aigateway/domain/model.go +0 -24
- package/services/aigateway/domain/provider.go +0 -38
- package/services/aigateway/domain/request.go +0 -65
- package/services/aigateway/domain/response.go +0 -56
- package/services/aigateway/domain/verdict.go +0 -55
- package/services/aigateway/serve.go +0 -45
- package/services/aigateway/tests/matrix/README.md +0 -71
- package/services/aigateway/tests/matrix/anthropic_test.go +0 -61
- package/services/aigateway/tests/matrix/azure_test.go +0 -52
- package/services/aigateway/tests/matrix/bedrock_test.go +0 -69
- package/services/aigateway/tests/matrix/gemini_test.go +0 -117
- package/services/aigateway/tests/matrix/matrix_test.go +0 -601
- package/services/aigateway/tests/matrix/openai_test.go +0 -66
- package/services/aigateway/tests/matrix/vertex_test.go +0 -160
package/pkg/breaker/breaker.go
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
// Package breaker implements a sliding-window circuit breaker keyed by slot ID.
|
|
2
|
-
//
|
|
3
|
-
// Three states: closed → open → half-open → closed.
|
|
4
|
-
// State transitions happen lazily on the next request (no background goroutine).
|
|
5
|
-
package breaker
|
|
6
|
-
|
|
7
|
-
import (
|
|
8
|
-
"sync"
|
|
9
|
-
"time"
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
// State represents the breaker's current mode.
|
|
13
|
-
type State int
|
|
14
|
-
|
|
15
|
-
const (
|
|
16
|
-
Closed State = iota // requests pass; failures counted
|
|
17
|
-
Open // requests short-circuited
|
|
18
|
-
HalfOpen // single probe allowed
|
|
19
|
-
)
|
|
20
|
-
|
|
21
|
-
// Options tunes the breaker. Zero values use sensible defaults.
|
|
22
|
-
type Options struct {
|
|
23
|
-
Window time.Duration // failure counting window (default 30s)
|
|
24
|
-
Threshold int // failures before opening (default 10)
|
|
25
|
-
OpenDuration time.Duration // time in open state before probing (default 60s)
|
|
26
|
-
Clock func() time.Time // injectable clock for testing
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
func (o *Options) withDefaults() {
|
|
30
|
-
if o.Window == 0 {
|
|
31
|
-
o.Window = 30 * time.Second
|
|
32
|
-
}
|
|
33
|
-
if o.Threshold == 0 {
|
|
34
|
-
o.Threshold = 10
|
|
35
|
-
}
|
|
36
|
-
if o.OpenDuration == 0 {
|
|
37
|
-
o.OpenDuration = 60 * time.Second
|
|
38
|
-
}
|
|
39
|
-
if o.Clock == nil {
|
|
40
|
-
o.Clock = time.Now
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Registry owns a concurrent map of breakers keyed by slot ID.
|
|
45
|
-
type Registry struct {
|
|
46
|
-
opts Options
|
|
47
|
-
mu sync.RWMutex
|
|
48
|
-
entries map[string]*slot
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// NewRegistry creates a breaker registry.
|
|
52
|
-
func NewRegistry(opts Options) *Registry {
|
|
53
|
-
opts.withDefaults()
|
|
54
|
-
return &Registry{opts: opts, entries: make(map[string]*slot)}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Allow reports whether a request to the given slot may proceed.
|
|
58
|
-
func (r *Registry) Allow(id string) bool {
|
|
59
|
-
s := r.getSlot(id)
|
|
60
|
-
s.mu.Lock()
|
|
61
|
-
defer s.mu.Unlock()
|
|
62
|
-
|
|
63
|
-
now := r.opts.Clock()
|
|
64
|
-
switch s.state {
|
|
65
|
-
case Open:
|
|
66
|
-
if now.After(s.openedAt.Add(r.opts.OpenDuration)) {
|
|
67
|
-
s.state = HalfOpen
|
|
68
|
-
s.inFlight = 1
|
|
69
|
-
return true
|
|
70
|
-
}
|
|
71
|
-
return false
|
|
72
|
-
case HalfOpen:
|
|
73
|
-
if s.inFlight > 0 {
|
|
74
|
-
return false
|
|
75
|
-
}
|
|
76
|
-
s.inFlight = 1
|
|
77
|
-
return true
|
|
78
|
-
default:
|
|
79
|
-
return true
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// RecordSuccess marks a successful attempt. Resets the breaker to closed.
|
|
84
|
-
func (r *Registry) RecordSuccess(id string) {
|
|
85
|
-
s := r.getSlot(id)
|
|
86
|
-
s.mu.Lock()
|
|
87
|
-
defer s.mu.Unlock()
|
|
88
|
-
s.state = Closed
|
|
89
|
-
s.failures = s.failures[:0]
|
|
90
|
-
s.inFlight = 0
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// RecordFailure marks a failed attempt. Opens the breaker if threshold is reached.
|
|
94
|
-
func (r *Registry) RecordFailure(id string) {
|
|
95
|
-
s := r.getSlot(id)
|
|
96
|
-
s.mu.Lock()
|
|
97
|
-
defer s.mu.Unlock()
|
|
98
|
-
|
|
99
|
-
now := r.opts.Clock()
|
|
100
|
-
cutoff := now.Add(-r.opts.Window)
|
|
101
|
-
|
|
102
|
-
pruned := s.failures[:0]
|
|
103
|
-
for _, t := range s.failures {
|
|
104
|
-
if t.After(cutoff) {
|
|
105
|
-
pruned = append(pruned, t)
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
pruned = append(pruned, now)
|
|
109
|
-
s.failures = pruned
|
|
110
|
-
s.inFlight = 0
|
|
111
|
-
|
|
112
|
-
if s.state == HalfOpen {
|
|
113
|
-
s.state = Open
|
|
114
|
-
s.openedAt = now
|
|
115
|
-
return
|
|
116
|
-
}
|
|
117
|
-
if len(pruned) >= r.opts.Threshold {
|
|
118
|
-
s.state = Open
|
|
119
|
-
s.openedAt = now
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// State returns the current state of a slot.
|
|
124
|
-
func (r *Registry) State(id string) State {
|
|
125
|
-
s := r.getSlot(id)
|
|
126
|
-
s.mu.Lock()
|
|
127
|
-
defer s.mu.Unlock()
|
|
128
|
-
if s.state == Open && r.opts.Clock().After(s.openedAt.Add(r.opts.OpenDuration)) {
|
|
129
|
-
return HalfOpen
|
|
130
|
-
}
|
|
131
|
-
return s.state
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
func (r *Registry) getSlot(id string) *slot {
|
|
135
|
-
r.mu.RLock()
|
|
136
|
-
s, ok := r.entries[id]
|
|
137
|
-
r.mu.RUnlock()
|
|
138
|
-
if ok {
|
|
139
|
-
return s
|
|
140
|
-
}
|
|
141
|
-
r.mu.Lock()
|
|
142
|
-
defer r.mu.Unlock()
|
|
143
|
-
if s, ok = r.entries[id]; ok {
|
|
144
|
-
return s
|
|
145
|
-
}
|
|
146
|
-
s = &slot{}
|
|
147
|
-
r.entries[id] = s
|
|
148
|
-
return s
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
type slot struct {
|
|
152
|
-
mu sync.Mutex
|
|
153
|
-
state State
|
|
154
|
-
failures []time.Time
|
|
155
|
-
openedAt time.Time
|
|
156
|
-
inFlight int
|
|
157
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
package breaker
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"testing"
|
|
5
|
-
"time"
|
|
6
|
-
|
|
7
|
-
"github.com/stretchr/testify/assert"
|
|
8
|
-
"github.com/stretchr/testify/require"
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
func testOpts(clock func() time.Time) Options {
|
|
12
|
-
return Options{
|
|
13
|
-
Threshold: 3,
|
|
14
|
-
Window: 10 * time.Second,
|
|
15
|
-
OpenDuration: 5 * time.Second,
|
|
16
|
-
Clock: clock,
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
func TestAllow_ClosedState(t *testing.T) {
|
|
21
|
-
now := time.Now()
|
|
22
|
-
r := NewRegistry(testOpts(func() time.Time { return now }))
|
|
23
|
-
|
|
24
|
-
assert.True(t, r.Allow("slot-a"), "new slot should allow requests (closed state)")
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
func TestRecordFailure_OpensAfterThreshold(t *testing.T) {
|
|
28
|
-
now := time.Now()
|
|
29
|
-
r := NewRegistry(testOpts(func() time.Time { return now }))
|
|
30
|
-
|
|
31
|
-
for i := 0; i < 3; i++ {
|
|
32
|
-
r.RecordFailure("slot-a")
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
assert.False(t, r.Allow("slot-a"), "breaker should be open after 3 failures")
|
|
36
|
-
assert.Equal(t, Open, r.State("slot-a"))
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
func TestAllow_OpensToHalfOpenAfterDuration(t *testing.T) {
|
|
40
|
-
now := time.Now()
|
|
41
|
-
r := NewRegistry(testOpts(func() time.Time { return now }))
|
|
42
|
-
|
|
43
|
-
// Trip the breaker.
|
|
44
|
-
for i := 0; i < 3; i++ {
|
|
45
|
-
r.RecordFailure("slot-a")
|
|
46
|
-
}
|
|
47
|
-
require.False(t, r.Allow("slot-a"))
|
|
48
|
-
|
|
49
|
-
// Advance past the open duration.
|
|
50
|
-
now = now.Add(6 * time.Second)
|
|
51
|
-
|
|
52
|
-
assert.True(t, r.Allow("slot-a"), "should allow one probe request in half-open")
|
|
53
|
-
// Second probe while in-flight should be rejected.
|
|
54
|
-
assert.False(t, r.Allow("slot-a"), "should reject second probe in half-open")
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
func TestRecordSuccess_ResetsToClosedFromHalfOpen(t *testing.T) {
|
|
58
|
-
now := time.Now()
|
|
59
|
-
r := NewRegistry(testOpts(func() time.Time { return now }))
|
|
60
|
-
|
|
61
|
-
for i := 0; i < 3; i++ {
|
|
62
|
-
r.RecordFailure("slot-a")
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
now = now.Add(6 * time.Second)
|
|
66
|
-
require.True(t, r.Allow("slot-a")) // half-open probe
|
|
67
|
-
|
|
68
|
-
r.RecordSuccess("slot-a")
|
|
69
|
-
|
|
70
|
-
assert.Equal(t, Closed, r.State("slot-a"))
|
|
71
|
-
assert.True(t, r.Allow("slot-a"), "should allow all requests after reset to closed")
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
func TestRecordFailure_HalfOpenReopens(t *testing.T) {
|
|
75
|
-
now := time.Now()
|
|
76
|
-
r := NewRegistry(testOpts(func() time.Time { return now }))
|
|
77
|
-
|
|
78
|
-
for i := 0; i < 3; i++ {
|
|
79
|
-
r.RecordFailure("slot-a")
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
now = now.Add(6 * time.Second)
|
|
83
|
-
require.True(t, r.Allow("slot-a")) // half-open probe
|
|
84
|
-
|
|
85
|
-
r.RecordFailure("slot-a")
|
|
86
|
-
|
|
87
|
-
assert.Equal(t, Open, r.State("slot-a"))
|
|
88
|
-
assert.False(t, r.Allow("slot-a"), "should be open again after half-open failure")
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
func TestState_ReportsCorrectly(t *testing.T) {
|
|
92
|
-
tests := []struct {
|
|
93
|
-
name string
|
|
94
|
-
setup func(r *Registry, advance func(d time.Duration))
|
|
95
|
-
want State
|
|
96
|
-
}{
|
|
97
|
-
{
|
|
98
|
-
name: "new slot is closed",
|
|
99
|
-
setup: func(_ *Registry, _ func(time.Duration)) {},
|
|
100
|
-
want: Closed,
|
|
101
|
-
},
|
|
102
|
-
{
|
|
103
|
-
name: "open after threshold failures",
|
|
104
|
-
setup: func(r *Registry, _ func(time.Duration)) {
|
|
105
|
-
for i := 0; i < 3; i++ {
|
|
106
|
-
r.RecordFailure("slot-a")
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
want: Open,
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
name: "half-open after open duration expires",
|
|
113
|
-
setup: func(r *Registry, advance func(time.Duration)) {
|
|
114
|
-
for i := 0; i < 3; i++ {
|
|
115
|
-
r.RecordFailure("slot-a")
|
|
116
|
-
}
|
|
117
|
-
advance(6 * time.Second)
|
|
118
|
-
},
|
|
119
|
-
want: HalfOpen,
|
|
120
|
-
},
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
for _, tc := range tests {
|
|
124
|
-
t.Run(tc.name, func(t *testing.T) {
|
|
125
|
-
now := time.Now()
|
|
126
|
-
r := NewRegistry(testOpts(func() time.Time { return now }))
|
|
127
|
-
tc.setup(r, func(d time.Duration) { now = now.Add(d) })
|
|
128
|
-
assert.Equal(t, tc.want, r.State("slot-a"))
|
|
129
|
-
})
|
|
130
|
-
}
|
|
131
|
-
}
|
package/pkg/clog/LICENSE
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022-2024 Mojo Men Ltd
|
|
4
|
-
Copyright (c) 2026 Reasoning Engine B.V.
|
|
5
|
-
|
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
in the Software without restriction, including without limitation the rights
|
|
9
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
furnished to do so, subject to the following conditions:
|
|
12
|
-
|
|
13
|
-
The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
copies or substantial portions of the Software.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
SOFTWARE.
|
|
23
|
-
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
This package is derived from https://github.com/wearemojo/mojo-public-go/tree/main/lib/clog
|
package/pkg/clog/clog.go
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
package clog
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"fmt"
|
|
6
|
-
|
|
7
|
-
prettyconsole "github.com/thessem/zap-prettyconsole"
|
|
8
|
-
"go.uber.org/zap"
|
|
9
|
-
"go.uber.org/zap/zapcore"
|
|
10
|
-
|
|
11
|
-
"github.com/langwatch/langwatch/pkg/contexts"
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
type contextKey struct{}
|
|
15
|
-
|
|
16
|
-
// Set stores a zap logger in the context.
|
|
17
|
-
func Set(ctx context.Context, logger *zap.Logger) context.Context {
|
|
18
|
-
return context.WithValue(ctx, contextKey{}, logger)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Get retrieves the zap logger from context, falling back to a global production logger.
|
|
22
|
-
func Get(ctx context.Context) *zap.Logger {
|
|
23
|
-
if l, ok := ctx.Value(contextKey{}).(*zap.Logger); ok && l != nil {
|
|
24
|
-
return l
|
|
25
|
-
}
|
|
26
|
-
l, _ := zap.NewProduction()
|
|
27
|
-
return l
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// With returns a child logger with additional fields, stored back into context.
|
|
31
|
-
func With(ctx context.Context, fields ...zap.Field) context.Context {
|
|
32
|
-
return Set(ctx, Get(ctx).With(fields...))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// New creates a configured zap logger for a service.
|
|
36
|
-
// If the context carries ServiceInfo, service/version/env fields are stamped
|
|
37
|
-
// on every log line automatically.
|
|
38
|
-
func New(ctx context.Context, cfg Config) *zap.Logger {
|
|
39
|
-
var logger *zap.Logger
|
|
40
|
-
|
|
41
|
-
if cfg.Format == "pretty" {
|
|
42
|
-
logger = prettyconsole.NewLogger(cfg.zapLevel())
|
|
43
|
-
} else {
|
|
44
|
-
zapCfg := zap.NewProductionConfig()
|
|
45
|
-
zapCfg.Level = zap.NewAtomicLevelAt(cfg.zapLevel())
|
|
46
|
-
logger, _ = zapCfg.Build()
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if info := contexts.GetServiceInfo(ctx); info != nil {
|
|
50
|
-
logger = logger.With(
|
|
51
|
-
zap.String("service", info.Service),
|
|
52
|
-
zap.String("version", info.Version),
|
|
53
|
-
zap.String("env", info.Environment),
|
|
54
|
-
)
|
|
55
|
-
}
|
|
56
|
-
return logger
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Config controls logging setup.
|
|
60
|
-
type Config struct {
|
|
61
|
-
Level string `env:"LEVEL"` // "debug", "info", "warn", "error"
|
|
62
|
-
Format string `env:"FORMAT"` // "json" (default), "pretty"
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Validate checks that config values are recognized.
|
|
66
|
-
func (c Config) Validate() error {
|
|
67
|
-
switch c.Format {
|
|
68
|
-
case "", "json", "pretty":
|
|
69
|
-
default:
|
|
70
|
-
return fmt.Errorf("clog: unknown LOG_FORMAT %q (want json or pretty)", c.Format)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if c.Level != "" {
|
|
74
|
-
var lvl zap.AtomicLevel
|
|
75
|
-
if err := lvl.UnmarshalText([]byte(c.Level)); err != nil {
|
|
76
|
-
return fmt.Errorf("clog: unknown LOG_LEVEL %q: %w", c.Level, err)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return nil
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
func (c Config) zapLevel() zapcore.Level {
|
|
84
|
-
if c.Level == "" {
|
|
85
|
-
return zapcore.InfoLevel
|
|
86
|
-
}
|
|
87
|
-
var lvl zapcore.Level
|
|
88
|
-
_ = lvl.UnmarshalText([]byte(c.Level))
|
|
89
|
-
return lvl
|
|
90
|
-
}
|
package/pkg/clog/clog_test.go
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
package clog
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"testing"
|
|
6
|
-
|
|
7
|
-
"github.com/stretchr/testify/assert"
|
|
8
|
-
"github.com/stretchr/testify/require"
|
|
9
|
-
"go.uber.org/zap"
|
|
10
|
-
|
|
11
|
-
"github.com/langwatch/langwatch/pkg/contexts"
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
func TestSetGet_Roundtrip(t *testing.T) {
|
|
15
|
-
logger := zap.NewNop()
|
|
16
|
-
ctx := Set(context.Background(), logger)
|
|
17
|
-
|
|
18
|
-
got := Get(ctx)
|
|
19
|
-
assert.Same(t, logger, got)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
func TestGet_Fallback(t *testing.T) {
|
|
23
|
-
logger := Get(context.Background())
|
|
24
|
-
|
|
25
|
-
require.NotNil(t, logger, "fallback logger should not be nil")
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
func TestNew_Debug(t *testing.T) {
|
|
29
|
-
logger := New(context.Background(), Config{Level: "debug"})
|
|
30
|
-
|
|
31
|
-
require.NotNil(t, logger)
|
|
32
|
-
assert.True(t, logger.Core().Enabled(zap.DebugLevel))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
func TestNew_Production(t *testing.T) {
|
|
36
|
-
logger := New(context.Background(), Config{Level: "info"})
|
|
37
|
-
|
|
38
|
-
require.NotNil(t, logger)
|
|
39
|
-
assert.False(t, logger.Core().Enabled(zap.DebugLevel))
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
func TestNew_StampsServiceInfo(t *testing.T) {
|
|
43
|
-
info := contexts.ServiceInfo{
|
|
44
|
-
Environment: "production",
|
|
45
|
-
Service: "aigateway",
|
|
46
|
-
Version: "v2.0.0",
|
|
47
|
-
}
|
|
48
|
-
ctx := contexts.SetServiceInfo(context.Background(), info)
|
|
49
|
-
|
|
50
|
-
logger := New(ctx, Config{Level: "info"})
|
|
51
|
-
|
|
52
|
-
require.NotNil(t, logger)
|
|
53
|
-
// The returned logger should differ from a bare one (fields were added).
|
|
54
|
-
bare := New(context.Background(), Config{Level: "info"})
|
|
55
|
-
assert.NotSame(t, bare, logger)
|
|
56
|
-
}
|
package/pkg/clog/panic.go
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
package clog
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"runtime"
|
|
6
|
-
|
|
7
|
-
"go.uber.org/zap"
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
// HandlePanic recovers from a panic, logs it with a stack trace, and
|
|
11
|
-
// optionally re-panics. Use propagate=true in HTTP handlers where
|
|
12
|
-
// upstream recovery exists (net/http catches panics per-request).
|
|
13
|
-
// Use propagate=false at service root to log and exit cleanly.
|
|
14
|
-
//
|
|
15
|
-
// Usage:
|
|
16
|
-
//
|
|
17
|
-
// defer clog.HandlePanic(ctx, false)
|
|
18
|
-
func HandlePanic(ctx context.Context, propagate bool) {
|
|
19
|
-
v := recover()
|
|
20
|
-
if v == nil {
|
|
21
|
-
return
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
stack := make([]byte, 1<<16)
|
|
25
|
-
stack = stack[:runtime.Stack(stack, false)]
|
|
26
|
-
|
|
27
|
-
logger := Get(ctx)
|
|
28
|
-
logger.Error("panic",
|
|
29
|
-
zap.Any("panic", v),
|
|
30
|
-
zap.ByteString("stack_trace", stack),
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
// Sync to ensure the panic log is flushed before exit/re-panic
|
|
34
|
-
_ = logger.Sync()
|
|
35
|
-
|
|
36
|
-
if propagate {
|
|
37
|
-
panic(v)
|
|
38
|
-
}
|
|
39
|
-
}
|
package/pkg/config/LICENSE
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022-2024 Mojo Men Ltd
|
|
4
|
-
Copyright (c) 2026 Reasoning Engine B.V.
|
|
5
|
-
|
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
-
in the Software without restriction, including without limitation the rights
|
|
9
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
-
furnished to do so, subject to the following conditions:
|
|
12
|
-
|
|
13
|
-
The above copyright notice and this permission notice shall be included in all
|
|
14
|
-
copies or substantial portions of the Software.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
-
SOFTWARE.
|
|
23
|
-
|
|
24
|
-
---
|
|
25
|
-
|
|
26
|
-
This package is derived from https://github.com/wearemojo/mojo-public-go/tree/main/lib/config
|
package/pkg/config/config.go
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
package config
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"fmt"
|
|
5
|
-
"os"
|
|
6
|
-
"reflect"
|
|
7
|
-
"strconv"
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
// Hydrate populates cfg (a pointer to a struct) from environment variables.
|
|
11
|
-
// Fields are tagged with `env:"VAR_NAME"`. Nested structs chain prefixes with "_".
|
|
12
|
-
func Hydrate(cfg any) error {
|
|
13
|
-
v := reflect.ValueOf(cfg)
|
|
14
|
-
if v.Kind() != reflect.Pointer || v.Elem().Kind() != reflect.Struct {
|
|
15
|
-
return fmt.Errorf("config.Hydrate: cfg must be a pointer to a struct")
|
|
16
|
-
}
|
|
17
|
-
return hydrateStruct(v.Elem(), v.Elem().Type(), "")
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
func hydrateStruct(v reflect.Value, t reflect.Type, prefix string) error {
|
|
21
|
-
for i := range v.NumField() {
|
|
22
|
-
field := v.Field(i)
|
|
23
|
-
fieldType := t.Field(i)
|
|
24
|
-
envTag := fieldType.Tag.Get("env")
|
|
25
|
-
|
|
26
|
-
if envTag == "" {
|
|
27
|
-
continue
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if prefix != "" {
|
|
31
|
-
envTag = prefix + "_" + envTag
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
switch field.Kind() {
|
|
35
|
-
case reflect.Struct:
|
|
36
|
-
if err := hydrateStruct(field, fieldType.Type, envTag); err != nil {
|
|
37
|
-
return err
|
|
38
|
-
}
|
|
39
|
-
case reflect.Ptr, reflect.Slice:
|
|
40
|
-
return fmt.Errorf("config.Hydrate: unsupported kind %s for field %s", field.Kind(), fieldType.Name)
|
|
41
|
-
default:
|
|
42
|
-
envValue := os.Getenv(envTag)
|
|
43
|
-
if envValue == "" || !field.CanSet() {
|
|
44
|
-
continue
|
|
45
|
-
}
|
|
46
|
-
if err := setField(field, envTag, envValue); err != nil {
|
|
47
|
-
return err
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return nil
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
func setField(field reflect.Value, tag, value string) error {
|
|
55
|
-
switch field.Kind() {
|
|
56
|
-
case reflect.String:
|
|
57
|
-
field.SetString(value)
|
|
58
|
-
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
59
|
-
v, err := strconv.ParseInt(value, 10, field.Type().Bits())
|
|
60
|
-
if err != nil {
|
|
61
|
-
return fmt.Errorf("config: failed to parse %s as int: %w", tag, err)
|
|
62
|
-
}
|
|
63
|
-
field.SetInt(v)
|
|
64
|
-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
65
|
-
v, err := strconv.ParseUint(value, 10, field.Type().Bits())
|
|
66
|
-
if err != nil {
|
|
67
|
-
return fmt.Errorf("config: failed to parse %s as uint: %w", tag, err)
|
|
68
|
-
}
|
|
69
|
-
field.SetUint(v)
|
|
70
|
-
case reflect.Float32, reflect.Float64:
|
|
71
|
-
v, err := strconv.ParseFloat(value, field.Type().Bits())
|
|
72
|
-
if err != nil {
|
|
73
|
-
return fmt.Errorf("config: failed to parse %s as float: %w", tag, err)
|
|
74
|
-
}
|
|
75
|
-
field.SetFloat(v)
|
|
76
|
-
case reflect.Bool:
|
|
77
|
-
v, err := strconv.ParseBool(value)
|
|
78
|
-
if err != nil {
|
|
79
|
-
return fmt.Errorf("config: failed to parse %s as bool: %w", tag, err)
|
|
80
|
-
}
|
|
81
|
-
field.SetBool(v)
|
|
82
|
-
default:
|
|
83
|
-
return fmt.Errorf("config: unsupported field type %s", field.Kind())
|
|
84
|
-
}
|
|
85
|
-
return nil
|
|
86
|
-
}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
package config
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"testing"
|
|
5
|
-
|
|
6
|
-
"github.com/stretchr/testify/assert"
|
|
7
|
-
"github.com/stretchr/testify/require"
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
func TestHydrate_String(t *testing.T) {
|
|
11
|
-
type cfg struct {
|
|
12
|
-
Host string `env:"HOST"`
|
|
13
|
-
}
|
|
14
|
-
t.Setenv("HOST", "localhost")
|
|
15
|
-
|
|
16
|
-
var c cfg
|
|
17
|
-
require.NoError(t, Hydrate(&c))
|
|
18
|
-
|
|
19
|
-
assert.Equal(t, "localhost", c.Host)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
func TestHydrate_Int(t *testing.T) {
|
|
23
|
-
type cfg struct {
|
|
24
|
-
Port int `env:"PORT"`
|
|
25
|
-
}
|
|
26
|
-
t.Setenv("PORT", "8080")
|
|
27
|
-
|
|
28
|
-
var c cfg
|
|
29
|
-
require.NoError(t, Hydrate(&c))
|
|
30
|
-
|
|
31
|
-
assert.Equal(t, 8080, c.Port)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
func TestHydrate_Bool(t *testing.T) {
|
|
35
|
-
type cfg struct {
|
|
36
|
-
Debug bool `env:"DEBUG"`
|
|
37
|
-
}
|
|
38
|
-
t.Setenv("DEBUG", "true")
|
|
39
|
-
|
|
40
|
-
var c cfg
|
|
41
|
-
require.NoError(t, Hydrate(&c))
|
|
42
|
-
|
|
43
|
-
assert.True(t, c.Debug)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
func TestHydrate_NestedStruct(t *testing.T) {
|
|
47
|
-
type db struct {
|
|
48
|
-
Host string `env:"HOST"`
|
|
49
|
-
Port int `env:"PORT"`
|
|
50
|
-
}
|
|
51
|
-
type cfg struct {
|
|
52
|
-
DB db `env:"DB"`
|
|
53
|
-
}
|
|
54
|
-
t.Setenv("DB_HOST", "pg.local")
|
|
55
|
-
t.Setenv("DB_PORT", "5432")
|
|
56
|
-
|
|
57
|
-
var c cfg
|
|
58
|
-
require.NoError(t, Hydrate(&c))
|
|
59
|
-
|
|
60
|
-
assert.Equal(t, "pg.local", c.DB.Host)
|
|
61
|
-
assert.Equal(t, 5432, c.DB.Port)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
func TestHydrate_Error_NonPointer(t *testing.T) {
|
|
65
|
-
type cfg struct {
|
|
66
|
-
Host string `env:"HOST"`
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
assert.Error(t, Hydrate(cfg{}))
|
|
70
|
-
}
|