@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/config/otel.go
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
package config
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"strings"
|
|
6
|
-
|
|
7
|
-
"github.com/langwatch/langwatch/pkg/otelsetup"
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
// OTel holds OpenTelemetry exporter configuration.
|
|
11
|
-
type OTel struct {
|
|
12
|
-
OTLPEndpoint string `env:"OTLP_ENDPOINT"`
|
|
13
|
-
OTLPHeaders string `env:"OTLP_HEADERS"`
|
|
14
|
-
SampleRatio float64 `env:"SAMPLE_RATIO"`
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Configure initializes the OTel provider from the config, parsing headers and
|
|
18
|
-
// returning a Provider whose Shutdown method flushes pending telemetry.
|
|
19
|
-
func (o *OTel) Configure(ctx context.Context, nodeID string) (*otelsetup.Provider, error) {
|
|
20
|
-
endpoint := o.OTLPEndpoint
|
|
21
|
-
if endpoint != "" && !strings.HasSuffix(endpoint, "/v1/traces") {
|
|
22
|
-
endpoint = strings.TrimRight(endpoint, "/") + "/v1/traces"
|
|
23
|
-
}
|
|
24
|
-
return otelsetup.New(ctx, otelsetup.Options{
|
|
25
|
-
NodeID: nodeID,
|
|
26
|
-
OTLPEndpoint: endpoint,
|
|
27
|
-
OTLPHeaders: parseHeaders(o.OTLPHeaders),
|
|
28
|
-
SampleRatio: o.SampleRatio,
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// parseHeaders parses an OTLP headers string ("key=value,key2=value2") into a map.
|
|
33
|
-
// Follows the W3C Baggage / OTEL_EXPORTER_OTLP_HEADERS format.
|
|
34
|
-
func parseHeaders(raw string) map[string]string {
|
|
35
|
-
if raw == "" {
|
|
36
|
-
return nil
|
|
37
|
-
}
|
|
38
|
-
headers := make(map[string]string)
|
|
39
|
-
for _, pair := range strings.Split(raw, ",") {
|
|
40
|
-
k, v, ok := strings.Cut(pair, "=")
|
|
41
|
-
if !ok {
|
|
42
|
-
continue
|
|
43
|
-
}
|
|
44
|
-
headers[strings.TrimSpace(k)] = strings.TrimSpace(v)
|
|
45
|
-
}
|
|
46
|
-
if len(headers) == 0 {
|
|
47
|
-
return nil
|
|
48
|
-
}
|
|
49
|
-
return headers
|
|
50
|
-
}
|
package/pkg/config/server.go
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
package config
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"errors"
|
|
6
|
-
"net/http"
|
|
7
|
-
"os"
|
|
8
|
-
"os/signal"
|
|
9
|
-
"syscall"
|
|
10
|
-
"time"
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
const (
|
|
14
|
-
DefaultGracefulSeconds = 5
|
|
15
|
-
// DefaultMaxRequestBodyBytes sizes the body cap for 1M-context LLM
|
|
16
|
-
// workloads where a single request can legitimately carry multi-MB
|
|
17
|
-
// prompts (vision images, long tool-result blocks, 750K-token context).
|
|
18
|
-
// Earlier iters used 2 MiB which 413-rejected real enterprise traffic.
|
|
19
|
-
// 32 MiB gives ~2× headroom over observed worst-case (Gemini 1.5 Pro
|
|
20
|
-
// with full context + images ≈ 15 MB) while preserving DDoS protection.
|
|
21
|
-
DefaultMaxRequestBodyBytes = 32 * 1024 * 1024
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
// Server configures HTTP listen address, graceful shutdown, and request body cap.
|
|
25
|
-
type Server struct {
|
|
26
|
-
Addr string `env:"ADDR"`
|
|
27
|
-
GracefulSeconds int `env:"GRACEFUL_SECONDS"`
|
|
28
|
-
MaxRequestBodyBytes int64 `env:"MAX_REQUEST_BODY_BYTES"`
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// ListenAndServe starts the server and handles graceful shutdown on SIGTERM/SIGINT.
|
|
32
|
-
func (cfg *Server) ListenAndServe(srv *http.Server) error {
|
|
33
|
-
if srv.Addr == "" {
|
|
34
|
-
srv.Addr = cfg.Addr
|
|
35
|
-
}
|
|
36
|
-
if cfg.GracefulSeconds == 0 {
|
|
37
|
-
cfg.GracefulSeconds = DefaultGracefulSeconds
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
stop := make(chan os.Signal, 1)
|
|
41
|
-
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
|
|
42
|
-
|
|
43
|
-
errs := make(chan error, 1)
|
|
44
|
-
go func() {
|
|
45
|
-
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
|
|
46
|
-
errs <- err
|
|
47
|
-
}
|
|
48
|
-
}()
|
|
49
|
-
|
|
50
|
-
select {
|
|
51
|
-
case err := <-errs:
|
|
52
|
-
return err
|
|
53
|
-
case <-stop:
|
|
54
|
-
if cfg.GracefulSeconds > 0 {
|
|
55
|
-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(cfg.GracefulSeconds)*time.Second)
|
|
56
|
-
defer cancel()
|
|
57
|
-
return srv.Shutdown(ctx)
|
|
58
|
-
}
|
|
59
|
-
return nil
|
|
60
|
-
}
|
|
61
|
-
}
|
package/pkg/config/validate.go
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
package config
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"reflect"
|
|
6
|
-
"strings"
|
|
7
|
-
|
|
8
|
-
"github.com/go-playground/validator/v10"
|
|
9
|
-
|
|
10
|
-
"github.com/langwatch/langwatch/pkg/herr"
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
// ConfigInvalid is the herr code for configuration validation failures.
|
|
14
|
-
const ConfigInvalid herr.Code = "config_invalid"
|
|
15
|
-
|
|
16
|
-
// Validate checks struct fields tagged with `validate:"..."` and returns a
|
|
17
|
-
// herr.E with structured violations on failure. The env var path is resolved
|
|
18
|
-
// from `env` struct tags chained with "_" for nested structs.
|
|
19
|
-
func Validate(ctx context.Context, cfg any) error {
|
|
20
|
-
v := validator.New()
|
|
21
|
-
|
|
22
|
-
err := v.Struct(cfg)
|
|
23
|
-
if err == nil {
|
|
24
|
-
return nil
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
ve, ok := err.(validator.ValidationErrors)
|
|
28
|
-
if !ok {
|
|
29
|
-
return err
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
resolver := envPathResolver(reflect.TypeOf(cfg), "")
|
|
33
|
-
return herr.FromValidationErrors(ctx, ConfigInvalid, ve, func(field string) string {
|
|
34
|
-
return resolver[field]
|
|
35
|
-
})
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// envPathResolver builds a map from "FieldA.FieldB" → "PREFIX_A_PREFIX_B"
|
|
39
|
-
// by walking env struct tags.
|
|
40
|
-
func envPathResolver(t reflect.Type, prefix string) map[string]string {
|
|
41
|
-
if t.Kind() == reflect.Ptr {
|
|
42
|
-
t = t.Elem()
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
index := make(map[string]string)
|
|
46
|
-
for i := range t.NumField() {
|
|
47
|
-
field := t.Field(i)
|
|
48
|
-
envTag := field.Tag.Get("env")
|
|
49
|
-
if envTag == "" {
|
|
50
|
-
continue
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
fullEnv := envTag
|
|
54
|
-
if prefix != "" {
|
|
55
|
-
fullEnv = prefix + "_" + envTag
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if field.Type.Kind() == reflect.Struct {
|
|
59
|
-
for k, v := range envPathResolver(field.Type, fullEnv) {
|
|
60
|
-
index[field.Name+"."+k] = v
|
|
61
|
-
}
|
|
62
|
-
} else {
|
|
63
|
-
index[field.Name] = fullEnv
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return index
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// EnvVarName resolves the full environment variable name for a dotted field path
|
|
70
|
-
// (e.g. "ControlPlane.BaseURL" → "GATEWAY_CONTROL_PLANE_BASE_URL").
|
|
71
|
-
func EnvVarName(cfg any, fieldPath string) string {
|
|
72
|
-
resolver := envPathResolver(reflect.TypeOf(cfg), "")
|
|
73
|
-
return resolver[fieldPath]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// SnakeToEnv converts a dotted Go field path to approximate env-style for display.
|
|
77
|
-
// This is a fallback when env tags aren't available.
|
|
78
|
-
func SnakeToEnv(fieldPath string) string {
|
|
79
|
-
return strings.ReplaceAll(fieldPath, ".", "_")
|
|
80
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
package config
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"testing"
|
|
6
|
-
|
|
7
|
-
"github.com/stretchr/testify/assert"
|
|
8
|
-
"github.com/stretchr/testify/require"
|
|
9
|
-
|
|
10
|
-
"github.com/langwatch/langwatch/pkg/herr"
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
type testConfig struct {
|
|
14
|
-
Server struct {
|
|
15
|
-
Addr string `env:"ADDR" validate:"required"`
|
|
16
|
-
Port int `env:"PORT" validate:"required,min=1"`
|
|
17
|
-
} `env:"SERVER"`
|
|
18
|
-
Secret string `env:"SECRET" validate:"required"`
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
func TestValidate_ReturnsNilWhenValid(t *testing.T) {
|
|
22
|
-
cfg := testConfig{Secret: "abc"}
|
|
23
|
-
cfg.Server.Addr = ":8080"
|
|
24
|
-
cfg.Server.Port = 8080
|
|
25
|
-
|
|
26
|
-
err := Validate(context.Background(), cfg)
|
|
27
|
-
assert.NoError(t, err)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
func TestValidate_ReturnsHerrWithViolations(t *testing.T) {
|
|
31
|
-
cfg := testConfig{} // all zero values
|
|
32
|
-
|
|
33
|
-
err := Validate(context.Background(), cfg)
|
|
34
|
-
require.Error(t, err)
|
|
35
|
-
|
|
36
|
-
var e herr.E
|
|
37
|
-
require.ErrorAs(t, err, &e)
|
|
38
|
-
assert.Equal(t, ConfigInvalid, e.Code)
|
|
39
|
-
|
|
40
|
-
violations, ok := e.Meta["violations"].([]herr.Violation)
|
|
41
|
-
require.True(t, ok)
|
|
42
|
-
assert.Len(t, violations, 3)
|
|
43
|
-
|
|
44
|
-
envs := make(map[string]string)
|
|
45
|
-
for _, v := range violations {
|
|
46
|
-
envs[v.Field] = v.Tag
|
|
47
|
-
}
|
|
48
|
-
assert.Equal(t, "SERVER_ADDR", envs["Server.Addr"])
|
|
49
|
-
assert.Equal(t, "SERVER_PORT", envs["Server.Port"])
|
|
50
|
-
assert.Equal(t, "SECRET", envs["Secret"])
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
func TestValidate_ResolvesNestedEnvPrefix(t *testing.T) {
|
|
54
|
-
type inner struct {
|
|
55
|
-
Value string `env:"VALUE" validate:"required"`
|
|
56
|
-
}
|
|
57
|
-
type outer struct {
|
|
58
|
-
Inner inner `env:"INNER"`
|
|
59
|
-
}
|
|
60
|
-
type cfg struct {
|
|
61
|
-
Outer outer `env:"OUTER"`
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
err := Validate(context.Background(), cfg{})
|
|
65
|
-
require.Error(t, err)
|
|
66
|
-
|
|
67
|
-
var e herr.E
|
|
68
|
-
require.ErrorAs(t, err, &e)
|
|
69
|
-
|
|
70
|
-
violations := e.Meta["violations"].([]herr.Violation)
|
|
71
|
-
require.Len(t, violations, 1)
|
|
72
|
-
assert.Equal(t, "Outer.Inner.Value", violations[0].Field)
|
|
73
|
-
assert.Equal(t, "OUTER_INNER_VALUE", violations[0].Tag)
|
|
74
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
package contexts
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"testing"
|
|
6
|
-
|
|
7
|
-
"github.com/stretchr/testify/assert"
|
|
8
|
-
"github.com/stretchr/testify/require"
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
func TestServiceInfo_SetGet(t *testing.T) {
|
|
12
|
-
info := ServiceInfo{
|
|
13
|
-
Environment: "production",
|
|
14
|
-
Service: "gateway",
|
|
15
|
-
Version: "v1.2.3",
|
|
16
|
-
}
|
|
17
|
-
ctx := SetServiceInfo(context.Background(), info)
|
|
18
|
-
|
|
19
|
-
got := GetServiceInfo(ctx)
|
|
20
|
-
require.NotNil(t, got)
|
|
21
|
-
assert.Equal(t, "production", got.Environment)
|
|
22
|
-
assert.Equal(t, "gateway", got.Service)
|
|
23
|
-
assert.Equal(t, "v1.2.3", got.Version)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
func TestServiceInfo_Unset(t *testing.T) {
|
|
27
|
-
assert.Nil(t, GetServiceInfo(context.Background()))
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
func TestMustGetServiceInfo_Panics(t *testing.T) {
|
|
31
|
-
assert.Panics(t, func() {
|
|
32
|
-
MustGetServiceInfo(context.Background())
|
|
33
|
-
})
|
|
34
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
package contexts
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
package contexts
|
|
2
|
-
|
|
3
|
-
import "context"
|
|
4
|
-
|
|
5
|
-
// ServiceInfo holds metadata about the running service.
|
|
6
|
-
type ServiceInfo struct {
|
|
7
|
-
Environment string
|
|
8
|
-
Service string
|
|
9
|
-
Version string
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
type serviceInfoKey struct{}
|
|
13
|
-
|
|
14
|
-
// SetServiceInfo stores service metadata in the context.
|
|
15
|
-
func SetServiceInfo(ctx context.Context, info ServiceInfo) context.Context {
|
|
16
|
-
return context.WithValue(ctx, serviceInfoKey{}, info)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// GetServiceInfo retrieves service metadata from the context, or nil if unset.
|
|
20
|
-
func GetServiceInfo(ctx context.Context) *ServiceInfo {
|
|
21
|
-
if val, ok := ctx.Value(serviceInfoKey{}).(ServiceInfo); ok {
|
|
22
|
-
return &val
|
|
23
|
-
}
|
|
24
|
-
return nil
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// MustGetServiceInfo retrieves service metadata or panics.
|
|
28
|
-
func MustGetServiceInfo(ctx context.Context) *ServiceInfo {
|
|
29
|
-
if val, ok := ctx.Value(serviceInfoKey{}).(ServiceInfo); ok {
|
|
30
|
-
return &val
|
|
31
|
-
}
|
|
32
|
-
panic("service info not found in context")
|
|
33
|
-
}
|
package/pkg/errfuncs/errfuncs.go
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
package errfuncs
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"errors"
|
|
5
|
-
"fmt"
|
|
6
|
-
"os"
|
|
7
|
-
"testing"
|
|
8
|
-
|
|
9
|
-
"github.com/stretchr/testify/assert"
|
|
10
|
-
)
|
|
11
|
-
|
|
12
|
-
func TestAs_Match(t *testing.T) {
|
|
13
|
-
inner := &os.PathError{Op: "open", Path: "/tmp/x", Err: errors.New("no such file")}
|
|
14
|
-
wrapped := fmt.Errorf("wrapped: %w", inner)
|
|
15
|
-
|
|
16
|
-
target, ok := As[*os.PathError](wrapped)
|
|
17
|
-
|
|
18
|
-
assert.True(t, ok)
|
|
19
|
-
assert.Equal(t, "/tmp/x", target.Path)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
func TestAs_NoMatch(t *testing.T) {
|
|
23
|
-
err := errors.New("plain error")
|
|
24
|
-
|
|
25
|
-
_, ok := As[*os.PathError](err)
|
|
26
|
-
|
|
27
|
-
assert.False(t, ok)
|
|
28
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
package forkedcontext
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"context"
|
|
5
|
-
"fmt"
|
|
6
|
-
"time"
|
|
7
|
-
|
|
8
|
-
"go.uber.org/zap"
|
|
9
|
-
|
|
10
|
-
"github.com/langwatch/langwatch/pkg/clog"
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
// forkedContext preserves context values but strips deadlines and cancellations.
|
|
14
|
-
type forkedContext struct {
|
|
15
|
-
ctx context.Context
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
func (forkedContext) Deadline() (time.Time, bool) { return time.Time{}, false }
|
|
19
|
-
func (forkedContext) Done() <-chan struct{} { return nil }
|
|
20
|
-
func (forkedContext) Err() error { return nil }
|
|
21
|
-
func (d forkedContext) Value(key any) any { return d.ctx.Value(key) }
|
|
22
|
-
|
|
23
|
-
// Fork launches fn in a goroutine with a context that preserves values
|
|
24
|
-
// but is not canceled when the parent is. Errors are logged automatically.
|
|
25
|
-
func Fork(ctx context.Context, fn func(context.Context) error) {
|
|
26
|
-
newCtx := forkedContext{ctx}
|
|
27
|
-
go func() {
|
|
28
|
-
var err error
|
|
29
|
-
defer func() {
|
|
30
|
-
if r := recover(); r != nil {
|
|
31
|
-
err = fmt.Errorf("panic: %v", r)
|
|
32
|
-
}
|
|
33
|
-
if err != nil {
|
|
34
|
-
clog.Get(ctx).Error("forked context errored", zap.Error(err))
|
|
35
|
-
}
|
|
36
|
-
}()
|
|
37
|
-
err = fn(newCtx)
|
|
38
|
-
}()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// ForkWithTimeout is like Fork but adds a timeout to the forked context.
|
|
42
|
-
func ForkWithTimeout(ctx context.Context, timeout time.Duration, fn func(context.Context) error) {
|
|
43
|
-
newCtx, cancel := context.WithTimeout(forkedContext{ctx}, timeout)
|
|
44
|
-
go func() {
|
|
45
|
-
var err error
|
|
46
|
-
defer func() {
|
|
47
|
-
cancel()
|
|
48
|
-
if r := recover(); r != nil {
|
|
49
|
-
err = fmt.Errorf("panic: %v", r)
|
|
50
|
-
}
|
|
51
|
-
if err != nil {
|
|
52
|
-
clog.Get(ctx).Error("forked context errored", zap.Error(err))
|
|
53
|
-
}
|
|
54
|
-
}()
|
|
55
|
-
err = fn(newCtx)
|
|
56
|
-
}()
|
|
57
|
-
}
|
package/pkg/health/health.go
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
// Package health provides k8s-style liveness, readiness, and startup probe handlers.
|
|
2
|
-
package health
|
|
3
|
-
|
|
4
|
-
import (
|
|
5
|
-
"encoding/json"
|
|
6
|
-
"net/http"
|
|
7
|
-
"sync/atomic"
|
|
8
|
-
"time"
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
// Check is a probe function. Returns (ok, detail) where detail explains failure.
|
|
12
|
-
type Check func() (ok bool, detail string)
|
|
13
|
-
|
|
14
|
-
// Registry holds named checks and lifecycle state.
|
|
15
|
-
type Registry struct {
|
|
16
|
-
version string
|
|
17
|
-
startedAt time.Time
|
|
18
|
-
readiness []namedCheck
|
|
19
|
-
liveness []namedCheck
|
|
20
|
-
startup atomic.Bool
|
|
21
|
-
draining atomic.Bool
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
type namedCheck struct {
|
|
25
|
-
name string
|
|
26
|
-
fn Check
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// New creates a probe registry.
|
|
30
|
-
func New(version string) *Registry {
|
|
31
|
-
return &Registry{version: version, startedAt: time.Now()}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// RegisterLiveness adds a liveness check. Should be cheap, never external.
|
|
35
|
-
func (r *Registry) RegisterLiveness(name string, fn Check) {
|
|
36
|
-
r.liveness = append(r.liveness, namedCheck{name, fn})
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// RegisterReadiness adds a readiness check. May depend on external systems.
|
|
40
|
-
func (r *Registry) RegisterReadiness(name string, fn Check) {
|
|
41
|
-
r.readiness = append(r.readiness, namedCheck{name, fn})
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// MarkStarted signals startup completion.
|
|
45
|
-
func (r *Registry) MarkStarted() { r.startup.Store(true) }
|
|
46
|
-
|
|
47
|
-
// Started reports whether startup is complete.
|
|
48
|
-
func (r *Registry) Started() bool { return r.startup.Load() }
|
|
49
|
-
|
|
50
|
-
// MarkDraining signals graceful shutdown has begun. /readyz returns 503.
|
|
51
|
-
func (r *Registry) MarkDraining() { r.draining.Store(true) }
|
|
52
|
-
|
|
53
|
-
// Draining reports whether drain has begun.
|
|
54
|
-
func (r *Registry) Draining() bool { return r.draining.Load() }
|
|
55
|
-
|
|
56
|
-
// Liveness serves /healthz.
|
|
57
|
-
func (r *Registry) Liveness(w http.ResponseWriter, _ *http.Request) {
|
|
58
|
-
writeChecks(w, r.version, time.Since(r.startedAt), r.liveness, false)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Readiness serves /readyz.
|
|
62
|
-
func (r *Registry) Readiness(w http.ResponseWriter, _ *http.Request) {
|
|
63
|
-
if r.draining.Load() {
|
|
64
|
-
writeJSON(w, http.StatusServiceUnavailable, probeResponse{
|
|
65
|
-
Status: "draining",
|
|
66
|
-
Version: r.version,
|
|
67
|
-
UptimeS: time.Since(r.startedAt).Seconds(),
|
|
68
|
-
})
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
writeChecks(w, r.version, time.Since(r.startedAt), r.readiness, true)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Startup serves /startupz.
|
|
75
|
-
func (r *Registry) Startup(w http.ResponseWriter, _ *http.Request) {
|
|
76
|
-
if !r.Started() {
|
|
77
|
-
writeJSON(w, http.StatusServiceUnavailable, probeResponse{
|
|
78
|
-
Status: "starting",
|
|
79
|
-
Version: r.version,
|
|
80
|
-
UptimeS: time.Since(r.startedAt).Seconds(),
|
|
81
|
-
})
|
|
82
|
-
return
|
|
83
|
-
}
|
|
84
|
-
writeChecks(w, r.version, time.Since(r.startedAt), r.readiness, true)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
type probeResponse struct {
|
|
88
|
-
Status string `json:"status"`
|
|
89
|
-
Version string `json:"version"`
|
|
90
|
-
UptimeS float64 `json:"uptime_s"`
|
|
91
|
-
Checks map[string]string `json:"checks,omitempty"`
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
func writeChecks(w http.ResponseWriter, version string, uptime time.Duration, checks []namedCheck, details bool) {
|
|
95
|
-
results := make(map[string]string, len(checks))
|
|
96
|
-
allOK := true
|
|
97
|
-
for _, c := range checks {
|
|
98
|
-
ok, detail := c.fn()
|
|
99
|
-
if ok {
|
|
100
|
-
results[c.name] = "ok"
|
|
101
|
-
} else {
|
|
102
|
-
results[c.name] = detail
|
|
103
|
-
allOK = false
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
status := http.StatusOK
|
|
107
|
-
word := "ok"
|
|
108
|
-
if !allOK {
|
|
109
|
-
status = http.StatusServiceUnavailable
|
|
110
|
-
word = "degraded"
|
|
111
|
-
}
|
|
112
|
-
resp := probeResponse{Status: word, Version: version, UptimeS: uptime.Seconds()}
|
|
113
|
-
if details || !allOK {
|
|
114
|
-
resp.Checks = results
|
|
115
|
-
}
|
|
116
|
-
writeJSON(w, status, resp)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
120
|
-
w.Header().Set("Content-Type", "application/json")
|
|
121
|
-
w.WriteHeader(status)
|
|
122
|
-
_ = json.NewEncoder(w).Encode(v)
|
|
123
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
package health
|
|
2
|
-
|
|
3
|
-
import (
|
|
4
|
-
"encoding/json"
|
|
5
|
-
"net/http"
|
|
6
|
-
"net/http/httptest"
|
|
7
|
-
"testing"
|
|
8
|
-
|
|
9
|
-
"github.com/stretchr/testify/assert"
|
|
10
|
-
"github.com/stretchr/testify/require"
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
func TestLiveness_AllOK(t *testing.T) {
|
|
14
|
-
reg := New("v1.0.0")
|
|
15
|
-
reg.RegisterLiveness("check-a", func() (bool, string) { return true, "" })
|
|
16
|
-
reg.RegisterLiveness("check-b", func() (bool, string) { return true, "" })
|
|
17
|
-
|
|
18
|
-
rec := httptest.NewRecorder()
|
|
19
|
-
reg.Liveness(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil))
|
|
20
|
-
|
|
21
|
-
assert.Equal(t, http.StatusOK, rec.Code)
|
|
22
|
-
|
|
23
|
-
var body probeResponse
|
|
24
|
-
require.NoError(t, json.NewDecoder(rec.Body).Decode(&body))
|
|
25
|
-
assert.Equal(t, "ok", body.Status)
|
|
26
|
-
assert.Equal(t, "v1.0.0", body.Version)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
func TestLiveness_Degraded(t *testing.T) {
|
|
30
|
-
reg := New("v1.0.0")
|
|
31
|
-
reg.RegisterLiveness("good", func() (bool, string) { return true, "" })
|
|
32
|
-
reg.RegisterLiveness("bad", func() (bool, string) { return false, "connection lost" })
|
|
33
|
-
|
|
34
|
-
rec := httptest.NewRecorder()
|
|
35
|
-
reg.Liveness(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil))
|
|
36
|
-
|
|
37
|
-
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
|
|
38
|
-
|
|
39
|
-
var body probeResponse
|
|
40
|
-
require.NoError(t, json.NewDecoder(rec.Body).Decode(&body))
|
|
41
|
-
assert.Equal(t, "degraded", body.Status)
|
|
42
|
-
assert.Equal(t, "connection lost", body.Checks["bad"])
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
func TestReadiness_Draining(t *testing.T) {
|
|
46
|
-
reg := New("v1.0.0")
|
|
47
|
-
reg.MarkDraining()
|
|
48
|
-
|
|
49
|
-
rec := httptest.NewRecorder()
|
|
50
|
-
reg.Readiness(rec, httptest.NewRequest(http.MethodGet, "/readyz", nil))
|
|
51
|
-
|
|
52
|
-
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
|
|
53
|
-
|
|
54
|
-
var body probeResponse
|
|
55
|
-
require.NoError(t, json.NewDecoder(rec.Body).Decode(&body))
|
|
56
|
-
assert.Equal(t, "draining", body.Status)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
func TestStartup_NotStarted(t *testing.T) {
|
|
60
|
-
reg := New("v1.0.0")
|
|
61
|
-
|
|
62
|
-
rec := httptest.NewRecorder()
|
|
63
|
-
reg.Startup(rec, httptest.NewRequest(http.MethodGet, "/startupz", nil))
|
|
64
|
-
|
|
65
|
-
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
|
|
66
|
-
|
|
67
|
-
var body probeResponse
|
|
68
|
-
require.NoError(t, json.NewDecoder(rec.Body).Decode(&body))
|
|
69
|
-
assert.Equal(t, "starting", body.Status)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
func TestStartup_Started(t *testing.T) {
|
|
73
|
-
reg := New("v1.0.0")
|
|
74
|
-
reg.MarkStarted()
|
|
75
|
-
|
|
76
|
-
rec := httptest.NewRecorder()
|
|
77
|
-
reg.Startup(rec, httptest.NewRequest(http.MethodGet, "/startupz", nil))
|
|
78
|
-
|
|
79
|
-
assert.Equal(t, http.StatusOK, rec.Code)
|
|
80
|
-
|
|
81
|
-
var body probeResponse
|
|
82
|
-
require.NoError(t, json.NewDecoder(rec.Body).Decode(&body))
|
|
83
|
-
assert.Equal(t, "ok", body.Status)
|
|
84
|
-
}
|
package/pkg/herr/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/merr
|
package/pkg/herr/code.go
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
package herr
|
|
2
|
-
|
|
3
|
-
import "errors"
|
|
4
|
-
|
|
5
|
-
// Code is a string-based error code that implements the error interface.
|
|
6
|
-
type Code string
|
|
7
|
-
|
|
8
|
-
func (c Code) Error() string { return string(c) }
|
|
9
|
-
func (c Code) String() string { return string(c) }
|
|
10
|
-
|
|
11
|
-
// IsCode is a convenience wrapper for errors.Is with a Code target.
|
|
12
|
-
func IsCode(err error, code Code) bool {
|
|
13
|
-
return errors.Is(err, code)
|
|
14
|
-
}
|