Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions execute/runtime/router/grpc/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func (r *Router) newHandler(
ctx,
incomingMetadata.Copy(),
)
outgoingContext = e2enginegrpc.Inject(outgoingContext)

var response rawMessage
var responseHeader metadata.MD
Expand Down
94 changes: 86 additions & 8 deletions execute/runtime/router/grpc/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,84 @@ import (
"github.com/e2engine/core/pkg/log"
)

const testExecutionID = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

func TestRouterNormalizesExecutionIDUpstream(t *testing.T) {
upstreamMetadata := make(chan metadata.MD, 1)
upstream := newTestGRPCServer(t, func(_ any, stream grpcpkg.ServerStream) error {
var request rawMessage
if err := stream.RecvMsg(&request); err != nil {
return err
}
md, _ := metadata.FromIncomingContext(stream.Context())
upstreamMetadata <- md.Copy()
return stream.SendMsg(&request)
})
defer upstream.stop()

store := call.NewStoreFactory(&call.Config{
MaxCalls: 10,
MaxRecordedBytes: 1024 * 1024,
}).New()
router := newTestGRPCRouter(t, store)
router.Register(&runtime.Route{
ServiceID: "service-1",
ListenAddress: "127.0.0.1:0",
RuntimeAddress: upstream.address(),
})
if err := router.Mount(t.Context()); err != nil {
t.Fatalf("Mount() error = %v", err)
}
defer func() {
if err := router.Unmount(context.Background()); err != nil {
t.Errorf("Unmount() error = %v", err)
}
}()

connection, err := grpcpkg.NewClient(
router.mounted[0].listener.Addr().String(),
grpcpkg.WithTransportCredentials(insecure.NewCredentials()),
grpcpkg.WithDefaultCallOptions(grpcpkg.ForceCodec(rawCodec{})),
)
if err != nil {
t.Fatalf("NewClient() error = %v", err)
}
defer connection.Close()

tests := []struct {
name string
values []string
want []string
}{
{name: "valid", values: []string{testExecutionID}, want: []string{testExecutionID}},
{name: "invalid", values: []string{"execution-1"}},
{name: "multiple", values: []string{testExecutionID, testExecutionID}},
{name: "missing"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
md := metadata.Pairs("x-test", "preserved")
for _, value := range tt.values {
md.Append("e2engine-test-execution-id", value)
}
ctx := metadata.NewOutgoingContext(t.Context(), md)
request := rawMessage{1, 2, 3}
var response rawMessage
if err := connection.Invoke(ctx, "/test.Service/Method", &request, &response); err != nil {
t.Fatalf("Invoke() error = %v", err)
}

got := <-upstreamMetadata
if values := got.Get("e2engine-test-execution-id"); !slices.Equal(values, tt.want) {
t.Errorf("upstream execution IDs = %q, want %q", values, tt.want)
}
if values := got.Get("x-test"); !slices.Equal(values, []string{"preserved"}) {
t.Errorf("upstream x-test = %q, want [preserved]", values)
}
})
}
}

func TestRawCodec(t *testing.T) {
codec := rawCodec{}

Expand Down Expand Up @@ -225,7 +303,7 @@ func TestRouter(t *testing.T) {
"x-request",
"request-value",
),
"execution-1",
testExecutionID,
),
)

Expand Down Expand Up @@ -293,7 +371,7 @@ func TestRouter(t *testing.T) {
}

calls := store.Get(call.Filter{
TestExecutionID: "execution-1",
TestExecutionID: testExecutionID,
ServiceID: "service-1",
})

Expand Down Expand Up @@ -473,7 +551,7 @@ func TestRouterMessageSizeLimit(t *testing.T) {
t.Context(),
e2enginegrpc.WithTestExecutionID(
nil,
"execution-1",
testExecutionID,
),
)

Expand Down Expand Up @@ -505,7 +583,7 @@ func TestRouterMessageSizeLimit(t *testing.T) {
}

calls := store.Get(call.Filter{
TestExecutionID: "execution-1",
TestExecutionID: testExecutionID,
ServiceID: "service-1",
})

Expand Down Expand Up @@ -616,7 +694,7 @@ func TestRouterUpstreamError(t *testing.T) {
context.Background(),
e2enginegrpc.WithTestExecutionID(
nil,
"execution-1",
testExecutionID,
),
)

Expand All @@ -638,7 +716,7 @@ func TestRouterUpstreamError(t *testing.T) {
}

calls := store.Get(call.Filter{
TestExecutionID: "execution-1",
TestExecutionID: testExecutionID,
ServiceID: "service-1",
})

Expand Down Expand Up @@ -1084,7 +1162,7 @@ func TestRouterMetadataSizeLimit(t *testing.T) {
"x-request",
tt.requestValue,
),
"execution-1",
testExecutionID,
),
)

Expand Down Expand Up @@ -1149,7 +1227,7 @@ func TestRouterMetadataSizeLimit(t *testing.T) {
}

calls := store.Get(call.Filter{
TestExecutionID: "execution-1",
TestExecutionID: testExecutionID,
ServiceID: "service-1",
})

Expand Down
7 changes: 4 additions & 3 deletions execute/runtime/router/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestRequestCapturer(t *testing.T) {
req.Header.Set("X-Test", "test")
req.Header = e2enginehttp.WithTestExecutionID(
req.Header,
"execution-1",
testExecutionID,
)

capturer := newRequestCapturer(req, 1024)
Expand Down Expand Up @@ -77,9 +77,10 @@ func TestRequestCapturer(t *testing.T) {
)
}

if id := e2enginehttp.TestExecutionID(got.Headers); id != "execution-1" {
if id := e2enginehttp.TestExecutionID(got.Headers); id != testExecutionID {
t.Errorf(
"expected execution ID execution-1, got %q",
"expected execution ID %q, got %q",
testExecutionID,
id,
)
}
Expand Down
14 changes: 12 additions & 2 deletions execute/runtime/router/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http/httputil"
"net/url"

e2engine "github.com/e2engine/instrumentation-go"
e2enginehttp "github.com/e2engine/instrumentation-go/http"

"github.com/e2engine/core/execute/call"
Expand Down Expand Up @@ -81,7 +82,7 @@ func (r *Router) Mount(ctx context.Context) error {
transport.ResponseHeaderTimeout = r.cfg.ResponseHeaderTimeout
transport.MaxResponseHeaderBytes = r.cfg.MaxResponseHeaderBytes

proxy.Transport = transport
proxy.Transport = e2enginehttp.Transport(transport)

proxy.ErrorHandler = func(
w http.ResponseWriter,
Expand Down Expand Up @@ -117,7 +118,7 @@ func (r *Router) Mount(ctx context.Context) error {
proxy.ServeHTTP(response, req)

if errRecord := r.calls.Record(call.Call{
TestExecutionID: e2enginehttp.TestExecutionID(req.Header),
TestExecutionID: validTestExecutionID(req.Header),
ServiceID: route.ServiceID,
HTTP: &call.HTTPCall{
Request: request.GetRequest(),
Expand Down Expand Up @@ -193,6 +194,15 @@ func (r *Router) Mount(ctx context.Context) error {
return nil
}

func validTestExecutionID(header http.Header) string {
values := header.Values("E2Engine-Test-Execution-ID")
if len(values) != 1 || !e2engine.ValidTestExecutionID(values[0]) {
return ""
}

return values[0]
}

func (r *Router) Unmount(ctx context.Context) error {
err := r.unmountMountedRoutes(ctx, r.mounted)
r.mounted = nil
Expand Down
Loading
Loading