feat: added configuration via env for context timeout (#2585)

This commit is contained in:
Daniël 2023-04-25 12:59:49 +02:00 committed by GitHub
parent b27bdac1f6
commit dcad77746a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 3 deletions

View File

@ -402,7 +402,7 @@ func setTimeoutMiddleware(next http.Handler) http.Handler {
// check if route is not excluded // check if route is not excluded
url := r.URL.Path url := r.URL.Path
if _, ok := baseconst.TimeoutExcludedRoutes[url]; !ok { if _, ok := baseconst.TimeoutExcludedRoutes[url]; !ok {
ctx, cancel = context.WithTimeout(r.Context(), baseconst.ContextTimeout*time.Second) ctx, cancel = context.WithTimeout(r.Context(), baseconst.ContextTimeout)
defer cancel() defer cancel()
} }

View File

@ -349,7 +349,7 @@ func setTimeoutMiddleware(next http.Handler) http.Handler {
// check if route is not excluded // check if route is not excluded
url := r.URL.Path url := r.URL.Path
if _, ok := constants.TimeoutExcludedRoutes[url]; !ok { if _, ok := constants.TimeoutExcludedRoutes[url]; !ok {
ctx, cancel = context.WithTimeout(r.Context(), constants.ContextTimeout*time.Second) ctx, cancel = context.WithTimeout(r.Context(), constants.ContextTimeout)
defer cancel() defer cancel()
} }

View File

@ -3,6 +3,7 @@ package constants
import ( import (
"os" "os"
"strconv" "strconv"
"time"
"go.signoz.io/signoz/pkg/query-service/model" "go.signoz.io/signoz/pkg/query-service/model"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3" v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
@ -74,6 +75,17 @@ var DEFAULT_FEATURE_SET = model.FeatureSet{
TimestampSort: IsTimestampSortFeatureEnabled(), TimestampSort: IsTimestampSortFeatureEnabled(),
} }
func GetContextTimeout() time.Duration {
contextTimeoutStr := GetOrDefaultEnv("CONTEXT_TIMEOUT", "60")
contextTimeoutDuration, err := time.ParseDuration(contextTimeoutStr + "s")
if err != nil {
return time.Minute
}
return contextTimeoutDuration
}
var ContextTimeout = GetContextTimeout()
const ( const (
TraceID = "traceID" TraceID = "traceID"
ServiceName = "serviceName" ServiceName = "serviceName"
@ -97,7 +109,6 @@ const (
ResponseStatusCode = "responseStatusCode" ResponseStatusCode = "responseStatusCode"
Descending = "descending" Descending = "descending"
Ascending = "ascending" Ascending = "ascending"
ContextTimeout = 60 // seconds
StatusPending = "pending" StatusPending = "pending"
StatusFailed = "failed" StatusFailed = "failed"
StatusSuccess = "success" StatusSuccess = "success"

View File

@ -3,6 +3,7 @@ package constants
import ( import (
"os" "os"
"testing" "testing"
"time"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
@ -19,3 +20,16 @@ func TestGetAlertManagerApiPrefix(t *testing.T) {
}) })
}) })
} }
func TestGetContextTimeout(t *testing.T) {
Convey("TestGetContextTimeout", t, func() {
res := GetContextTimeout()
So(res, ShouldEqual, time.Duration(60000000000))
Convey("WithEnvSet", func() {
os.Setenv("CONTEXT_TIMEOUT", "120")
res = GetContextTimeout()
So(res, ShouldEqual, time.Duration(120000000000))
})
})
}