Skip to content

Commit

Permalink
Merge pull request #44 from oslokommune/fix/lambda-proxy-interface
Browse files Browse the repository at this point in the history
Fiks feilende type assertion når man kjørte en lambda uten tracing med GinRuntime
  • Loading branch information
eirikeve committed Apr 12, 2024
2 parents 65ce88a + d3a68af commit e465748
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
7 changes: 4 additions & 3 deletions aws/ginruntime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
)

func (e *GinEngine) lambdaProxy() func(ctx context.Context, req any) (any, error) {
func (e *GinEngine) lambdaProxy() any {
var proxy any

proxy = func(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
return ginadapter.NewV2(e.engine).ProxyWithContext(ctx, req)
}

if e.TracingEnabled() {
proxy = otellambda.InstrumentHandler(proxy, e.oTelLambdaOptions()...).(func(context.Context, any) (any, error))
proxy = otellambda.InstrumentHandler(proxy, e.oTelLambdaOptions()...)
}
return proxy.(func(context.Context, any) (any, error))

return proxy
}

func (e *GinEngine) StartServer() {
Expand Down
29 changes: 29 additions & 0 deletions aws/ginruntime/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ginruntime

import (
"context"
"os"
"testing"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambdacontext"
"go.opentelemetry.io/contrib/propagators/aws/xray"
"go.opentelemetry.io/otel/sdk/trace"
)

func TestServerLambdaProxyWithoutTracing(t *testing.T) {
engine := New(context.Background())
proxy := engine.lambdaProxy().(func(context.Context, events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error))
event := events.APIGatewayV2HTTPRequest{}
proxy(context.Background(), event)
}

func TestServerLambdaProxyWithTracing(t *testing.T) {
os.Setenv("AWS_LAMBDA_FUNCTION_NAME", "test")
lc := lambdacontext.LambdaContext{AwsRequestID: "test", InvokedFunctionArn: "test", Identity: lambdacontext.CognitoIdentity{}, ClientContext: lambdacontext.ClientContext{}}
ctx := lambdacontext.NewContext(context.Background(), &lc)
engine := New(ctx, WithTracing("test", NewInterceptingTracerProvider(func(span []trace.ReadOnlySpan) {}), &xray.Propagator{}))
proxy := engine.lambdaProxy().(func(context.Context, any) (any, error))
event := events.APIGatewayV2HTTPRequest{}
proxy(ctx, event)
}
2 changes: 1 addition & 1 deletion aws/ginruntime/tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestEnableTracing_TracesLambdaInvocationWithExpectedAttributes(t *testing.T
c.JSON(200, "bar")
})

proxy := engine.lambdaProxy()
proxy := engine.lambdaProxy().(func(context.Context, any) (any, error))
req := events.APIGatewayV2HTTPRequest{
RawPath: "/foo/test",
}
Expand Down
1 change: 1 addition & 0 deletions aws/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ require (
github.com/swaggest/jsonschema-go v0.3.66 // indirect
github.com/swaggest/refl v1.3.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go v1.2.12 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.opentelemetry.io/contrib/detectors/aws/lambda v0.46.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions aws/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go v1.2.12 h1:oRySHlrVC5izTtkkuUmr0leYV9ixxb2eXLnyUiNctis=
github.com/ugorji/go v1.2.12/go.mod h1:ww1EoEU2l94lsly/faL8Xxa015DInNutT/JBXRcn6G4=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=
Expand Down

0 comments on commit e465748

Please sign in to comment.