mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-06-05 23:03:48 +00:00
fixup! fixup! fixup! fixup! fixup! Aligning struct fields for better memory management.
This commit is contained in:
@@ -1,15 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/avast/retry-go/v4"
|
"github.com/avast/retry-go/v4"
|
||||||
|
"github.com/goccy/go-json"
|
||||||
fiber "github.com/gofiber/fiber/v2"
|
fiber "github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/proxy"
|
"github.com/gofiber/fiber/v2/middleware/proxy"
|
||||||
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
|
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
|
||||||
libpack_monitoring "github.com/lukaszraczylo/graphql-monitoring-proxy/monitoring"
|
libpack_monitoring "github.com/lukaszraczylo/graphql-monitoring-proxy/monitoring"
|
||||||
|
libpack_trace "github.com/lukaszraczylo/graphql-monitoring-proxy/tracing"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,7 +32,7 @@ func createFasthttpClient(timeout int) *fasthttp.Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error {
|
func proxyTheRequest(c *fiber.Ctx, currentEndpoint string, ctx context.Context) error {
|
||||||
if !checkAllowedURLs(c) {
|
if !checkAllowedURLs(c) {
|
||||||
cfg.Logger.Error(&libpack_logger.LogMessage{
|
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||||
Message: "Request blocked",
|
Message: "Request blocked",
|
||||||
@@ -129,5 +132,25 @@ func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.Response().Header.Del(fiber.HeaderServer)
|
c.Response().Header.Del(fiber.HeaderServer)
|
||||||
|
if cfg.Trace.Enable {
|
||||||
|
tracingContext := libpack_trace.TraceContextInject(ctx)
|
||||||
|
if tracingContext == nil {
|
||||||
|
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Can't inject empty tracing context",
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
traceJsonEncoded, err := json.Marshal(tracingContext)
|
||||||
|
if err != nil {
|
||||||
|
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Can't convert tracing context to JSON",
|
||||||
|
Pairs: map[string]interface{}{
|
||||||
|
"error": err.Error(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.Response().Header.Set("X-Trace-Span", string(traceJsonEncoded))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,6 +77,26 @@ func checkAllowedURLs(c *fiber.Ctx) bool {
|
|||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractTraceHeaders(c *fiber.Ctx) (found bool, traceHeaders map[string]string) {
|
||||||
|
if !cfg.Trace.Enable {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
headers := c.Request().Header
|
||||||
|
traceHeader := headers.Peek("X-Trace-Span")
|
||||||
|
if traceHeader != nil {
|
||||||
|
traceHeaders = make(map[string]string)
|
||||||
|
if err := json.Unmarshal(traceHeader, &traceHeaders); err != nil {
|
||||||
|
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||||
|
Message: "Error unmarshalling tracer header",
|
||||||
|
Pairs: map[string]interface{}{"error": err},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func healthCheck(c *fiber.Ctx) error {
|
func healthCheck(c *fiber.Ctx) error {
|
||||||
if len(cfg.Server.HealthcheckGraphQL) > 0 {
|
if len(cfg.Server.HealthcheckGraphQL) > 0 {
|
||||||
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
cfg.Logger.Debug(&libpack_logger.LogMessage{
|
||||||
@@ -111,27 +131,15 @@ func processGraphQLRequest(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// Pre-fetch headers and trace header processing
|
// Pre-fetch headers and trace header processing
|
||||||
headers := c.Request().Header
|
headers := c.Request().Header
|
||||||
traceHeader := headers.Peek("X-Trace-Span")
|
|
||||||
authorization := headers.Peek("Authorization")
|
authorization := headers.Peek("Authorization")
|
||||||
|
ctx := context.Background()
|
||||||
|
traceHeaderFound, traceHeader := extractTraceHeaders(c)
|
||||||
|
|
||||||
if cfg.Trace.Enable && traceHeader != nil {
|
if traceHeaderFound {
|
||||||
traceHeaders := make(map[string]string)
|
ctx = libpack_trace.TraceContextExtract(ctx, traceHeader)
|
||||||
if err := json.Unmarshal(traceHeader, &traceHeaders); err != nil {
|
|
||||||
cfg.Logger.Error(&libpack_logger.LogMessage{
|
|
||||||
Message: "Error unmarshalling tracer header",
|
|
||||||
Pairs: map[string]interface{}{"error": err},
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
ctx := libpack_trace.TraceContextExtract(context.Background(), traceHeaders)
|
|
||||||
_, span := libpack_trace.ContinueSpanFromContext(ctx, "GraphQLRequest")
|
_, span := libpack_trace.ContinueSpanFromContext(ctx, "GraphQLRequest")
|
||||||
defer span.End()
|
defer span.End()
|
||||||
}
|
}
|
||||||
} else if cfg.Trace.Enable {
|
|
||||||
cfg.Logger.Warning(&libpack_logger.LogMessage{
|
|
||||||
Message: "No trace header found",
|
|
||||||
Pairs: nil,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// JWT and role extraction with pre-check
|
// JWT and role extraction with pre-check
|
||||||
if authorization != nil && (len(cfg.Client.JWTUserClaimPath) > 0 || len(cfg.Client.JWTRoleClaimPath) > 0) {
|
if authorization != nil && (len(cfg.Client.JWTUserClaimPath) > 0 || len(cfg.Client.JWTRoleClaimPath) > 0) {
|
||||||
@@ -170,7 +178,7 @@ func processGraphQLRequest(c *fiber.Ctx) error {
|
|||||||
Message: "Request passed as-is - probably not a GraphQL",
|
Message: "Request passed as-is - probably not a GraphQL",
|
||||||
Pairs: nil,
|
Pairs: nil,
|
||||||
})
|
})
|
||||||
return proxyTheRequest(c, parsedResult.activeEndpoint)
|
return proxyTheRequest(c, parsedResult.activeEndpoint, ctx)
|
||||||
}
|
}
|
||||||
// Cache handling logic
|
// Cache handling logic
|
||||||
queryCacheHash := libpack_cache.CalculateHash(c)
|
queryCacheHash := libpack_cache.CalculateHash(c)
|
||||||
@@ -223,10 +231,10 @@ func processGraphQLRequest(c *fiber.Ctx) error {
|
|||||||
Message: "Cache miss",
|
Message: "Cache miss",
|
||||||
Pairs: map[string]interface{}{"hash": queryCacheHash, "user_id": extractedUserID, "request_uuid": c.Locals("request_uuid")},
|
Pairs: map[string]interface{}{"hash": queryCacheHash, "user_id": extractedUserID, "request_uuid": c.Locals("request_uuid")},
|
||||||
})
|
})
|
||||||
proxyAndCacheTheRequest(c, queryCacheHash, parsedResult.cacheTime, parsedResult.activeEndpoint)
|
proxyAndCacheTheRequest(c, queryCacheHash, parsedResult.cacheTime, parsedResult.activeEndpoint, ctx)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := proxyTheRequest(c, parsedResult.activeEndpoint); err != nil {
|
if err := proxyTheRequest(c, parsedResult.activeEndpoint, ctx); err != nil {
|
||||||
cfg.Logger.Error(&libpack_logger.LogMessage{
|
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||||
Message: "Can't proxy the request",
|
Message: "Can't proxy the request",
|
||||||
Pairs: map[string]interface{}{"error": err.Error()},
|
Pairs: map[string]interface{}{"error": err.Error()},
|
||||||
@@ -242,8 +250,8 @@ func processGraphQLRequest(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Additional helper function to avoid code repetition
|
// Additional helper function to avoid code repetition
|
||||||
func proxyAndCacheTheRequest(c *fiber.Ctx, queryCacheHash string, cacheTime int, currentEndpoint string) {
|
func proxyAndCacheTheRequest(c *fiber.Ctx, queryCacheHash string, cacheTime int, currentEndpoint string, ctx context.Context) {
|
||||||
err := proxyTheRequest(c, currentEndpoint)
|
err := proxyTheRequest(c, currentEndpoint, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfg.Logger.Error(&libpack_logger.LogMessage{
|
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||||
Message: "Can't proxy the request",
|
Message: "Can't proxy the request",
|
||||||
|
|||||||
Reference in New Issue
Block a user