mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-06-05 23:03:48 +00:00
Improve tests and speed things up a little.
This commit is contained in:
@@ -11,7 +11,7 @@ help: ## display this help
|
|||||||
|
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: build ## run application
|
run: build ## run application
|
||||||
@LOG_LEVEL=debug PURGE_METRICS_ON_CRAWL=true BLOCK_SCHEMA_INTROSPECTION=true CACHE_TTL=10 JWT_ROLE_RATE_LIMIT=false JWT_ROLE_CLAIM_PATH="Hasura.x-hasura-default-role" JWT_USER_CLAIM_PATH="Hasura.x-hasura-user-id" HOST_GRAPHQL=https://hasura8.lan/ HEALTHCHECK_GRAPHQL_URL=https://hasura8.lan/v1/graphql ./graphql-proxy
|
@LOG_LEVEL=debug PURGE_METRICS_ON_CRAWL=true BLOCK_SCHEMA_INTROSPECTION=false CACHE_TTL=10 JWT_ROLE_RATE_LIMIT=false JWT_ROLE_CLAIM_PATH="Hasura.x-hasura-default-role" JWT_USER_CLAIM_PATH="Hasura.x-hasura-user-id" HOST_GRAPHQL=https://hasura8.lan/ HEALTHCHECK_GRAPHQL_URL=https://hasura8.lan/v1/graphql ./graphql-proxy
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build: ## build the binary
|
build: ## build the binary
|
||||||
|
|||||||
Vendored
+36
@@ -110,3 +110,39 @@ func (suite *CacheTestSuite) Test_CacheExpire() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *CacheTestSuite) Test_CacheCleanExpiredEntries() {
|
||||||
|
cache := New(5 * time.Second)
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
cache_value string
|
||||||
|
ttl time.Duration
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "test1",
|
||||||
|
cache_value: "test1-123",
|
||||||
|
ttl: 2 * time.Second,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "test2",
|
||||||
|
cache_value: "test2-123",
|
||||||
|
ttl: 5 * time.Second,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
suite.T().Run(tt.name, func(t *testing.T) {
|
||||||
|
cache.Set(tt.name, []byte(tt.name), tt.ttl)
|
||||||
|
c, ok := cache.Get(tt.name)
|
||||||
|
suite.Equal(true, ok)
|
||||||
|
suite.Equal(tt.name, string(c))
|
||||||
|
time.Sleep(tt.ttl)
|
||||||
|
c, ok = cache.Get(tt.name)
|
||||||
|
suite.Equal(false, ok)
|
||||||
|
suite.Equal("", string(c))
|
||||||
|
cache.CleanExpiredEntries()
|
||||||
|
c, ok = cache.Get(tt.name)
|
||||||
|
suite.Equal(false, ok)
|
||||||
|
suite.Equal("", string(c))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+10
@@ -38,6 +38,7 @@ var introspection_queries = []string{
|
|||||||
|
|
||||||
var introspectionQuerySet = map[string]struct{}{}
|
var introspectionQuerySet = map[string]struct{}{}
|
||||||
var introspectionAllowedQueries = map[string]struct{}{}
|
var introspectionAllowedQueries = map[string]struct{}{}
|
||||||
|
var allowedUrls = map[string]struct{}{}
|
||||||
|
|
||||||
func prepareQueriesAndExemptions() {
|
func prepareQueriesAndExemptions() {
|
||||||
introspectionQuerySet = map[string]struct{}{}
|
introspectionQuerySet = map[string]struct{}{}
|
||||||
@@ -57,6 +58,15 @@ func prepareQueriesAndExemptions() {
|
|||||||
}
|
}
|
||||||
return rsqs
|
return rsqs
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
allowedUrls = map[string]struct{}{}
|
||||||
|
allowedUrls = func() map[string]struct{} {
|
||||||
|
rsqs := make(map[string]struct{}, len(cfg.Server.AllowURLs))
|
||||||
|
for _, query := range cfg.Server.AllowURLs {
|
||||||
|
rsqs[strings.ToLower(query)] = struct{}{}
|
||||||
|
}
|
||||||
|
return rsqs
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGraphQLQuery(c *fiber.Ctx) (operationType, operationName string, cacheRequest bool, cache_time int, should_block bool, should_ignore bool) {
|
func parseGraphQLQuery(c *fiber.Ctx) (operationType, operationName string, cacheRequest bool, cache_time int, should_block bool, should_ignore bool) {
|
||||||
|
|||||||
@@ -243,6 +243,20 @@ func (suite *Tests) Test_parseGraphQLQuery() {
|
|||||||
returnCode: 200,
|
returnCode: 200,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "test invalid query",
|
||||||
|
suppliedQuery: queries{
|
||||||
|
body: "{\"query\":\"query MyQuery tg_users(where: {handle: {_eq: \\\"tozuo\\\"}}) { id __typename } \"}",
|
||||||
|
},
|
||||||
|
wantResults: results{
|
||||||
|
is_cached: false,
|
||||||
|
should_block: false,
|
||||||
|
should_ignore: true,
|
||||||
|
op_name: "",
|
||||||
|
op_type: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|||||||
@@ -49,15 +49,11 @@ func AddRequestUUID(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkAllowedURLs(c *fiber.Ctx) bool {
|
func checkAllowedURLs(c *fiber.Ctx) bool {
|
||||||
if len(cfg.Server.AllowURLs) == 0 {
|
if len(allowedUrls) == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for _, allowedURL := range cfg.Server.AllowURLs {
|
_, ok := allowedUrls[c.Path()]
|
||||||
if c.Path() == allowedURL {
|
return ok
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func healthCheck(c *fiber.Ctx) error {
|
func healthCheck(c *fiber.Ctx) error {
|
||||||
@@ -116,21 +112,20 @@ func processGraphQLRequest(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if should_ignore {
|
if should_ignore {
|
||||||
cfg.Logger.Debug("Request passed as-is - not a GraphQL")
|
cfg.Logger.Debug("Request passed as-is - probably not a GraphQL")
|
||||||
return proxyTheRequest(c)
|
return proxyTheRequest(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cache_time > 0 {
|
if cache_time > 0 {
|
||||||
cfg.Logger.Debug("Cache time set via query", map[string]interface{}{"cache_time": cache_time})
|
cfg.Logger.Debug("Cache time set via query", map[string]interface{}{"cache_time": cache_time})
|
||||||
cache_time = cfg.Cache.CacheTTL
|
} else {
|
||||||
}
|
// If not set via query, try setting via header
|
||||||
|
|
||||||
if cache_time == 0 && !cacheFromQuery {
|
|
||||||
cacheQuery := c.Request().Header.Peek("X-Cache-Graphql-Query")
|
cacheQuery := c.Request().Header.Peek("X-Cache-Graphql-Query")
|
||||||
if cacheQuery != nil {
|
if cacheQuery != nil {
|
||||||
cache_time, _ = strconv.Atoi(string(cacheQuery))
|
cache_time, _ = strconv.Atoi(string(cacheQuery))
|
||||||
cfg.Logger.Debug("Cache time set via header", map[string]interface{}{"cache_time": cache_time})
|
cfg.Logger.Debug("Cache time set via header", map[string]interface{}{"cache_time": cache_time})
|
||||||
cacheFromQuery = true
|
} else {
|
||||||
|
cache_time = cfg.Cache.CacheTTL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +151,7 @@ func processGraphQLRequest(c *fiber.Ctx) error {
|
|||||||
timeTaken := time.Since(startTime)
|
timeTaken := time.Since(startTime)
|
||||||
|
|
||||||
// Logging & Monitoring
|
// Logging & Monitoring
|
||||||
logAndMonitorRequest(c, extractedUserID, opType, opName, wasCached, timeTaken, startTime)
|
go logAndMonitorRequest(c, extractedUserID, opType, opName, wasCached, timeTaken, startTime)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user