Skip to content

Commit

Permalink
feat(trace-detail): merge branch 'main' into query-service-waterfall
Browse files Browse the repository at this point in the history
  • Loading branch information
vikrantgupta25 committed Jan 22, 2025
2 parents 3336876 + df5ab64 commit 2d99c29
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 142 deletions.
10 changes: 10 additions & 0 deletions conf/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,13 @@ sqlstore:
sqlite:
# The path to the SQLite database file.
path: /var/lib/signoz/signoz.db


##################### APIServer #####################
apiserver:
timeout:
default: 60s
max: 600s
excluded_routes:
- /api/v1/logs/tail
- /api/v3/logs/livetail
30 changes: 11 additions & 19 deletions ee/query-service/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
const AppDbEngine = "sqlite"

type ServerOptions struct {
Config signoz.Config
SigNoz *signoz.SigNoz
PromConfigPath string
SkipTopLvlOpsPath string
Expand Down Expand Up @@ -324,7 +325,11 @@ func (s *Server) createPrivateServer(apiHandler *api.APIHandler) (*http.Server,

r := baseapp.NewRouter()

r.Use(setTimeoutMiddleware)
r.Use(middleware.NewTimeout(zap.L(),
s.serverOptions.Config.APIServer.Timeout.ExcludedRoutes,
s.serverOptions.Config.APIServer.Timeout.Default,
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(s.analyticsMiddleware)
r.Use(middleware.NewLogging(zap.L()).Wrap)
r.Use(baseapp.LogCommentEnricher)
Expand Down Expand Up @@ -367,7 +372,11 @@ func (s *Server) createPublicServer(apiHandler *api.APIHandler, web web.Web) (*h
}
am := baseapp.NewAuthMiddleware(getUserFromRequest)

r.Use(setTimeoutMiddleware)
r.Use(middleware.NewTimeout(zap.L(),
s.serverOptions.Config.APIServer.Timeout.ExcludedRoutes,
s.serverOptions.Config.APIServer.Timeout.Default,
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(s.analyticsMiddleware)
r.Use(middleware.NewLogging(zap.L()).Wrap)
r.Use(baseapp.LogCommentEnricher)
Expand Down Expand Up @@ -570,23 +579,6 @@ func (s *Server) analyticsMiddleware(next http.Handler) http.Handler {
})
}

// TODO(remove): Implemented at pkg/http/middleware/timeout.go
func setTimeoutMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var cancel context.CancelFunc
// check if route is not excluded
url := r.URL.Path
if _, ok := baseconst.TimeoutExcludedRoutes[url]; !ok {
ctx, cancel = context.WithTimeout(r.Context(), baseconst.ContextTimeout)
defer cancel()
}

r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}

// initListeners initialises listeners of the server
func (s *Server) initListeners() error {
// listen on public port
Expand Down
1 change: 1 addition & 0 deletions ee/query-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func main() {
}

serverOptions := &app.ServerOptions{
Config: config,
SigNoz: signoz,
HTTPHostPort: baseconst.HTTPHostPort,
PromConfigPath: promConfigPath,
Expand Down
42 changes: 42 additions & 0 deletions pkg/apiserver/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package apiserver

import (
"time"

"go.signoz.io/signoz/pkg/factory"
)

// Config holds the configuration for config.
type Config struct {
Timeout Timeout `mapstructure:"timeout"`
}

type Timeout struct {
// The default context timeout that can be overridden by the request
Default time.Duration `mapstructure:"default"`
// The maximum allowed context timeout
Max time.Duration `mapstructure:"max"`
// The list of routes that are excluded from the timeout
ExcludedRoutes []string `mapstructure:"excluded_routes"`
}

func NewConfigFactory() factory.ConfigFactory {
return factory.NewConfigFactory(factory.MustNewName("apiserver"), newConfig)
}

func newConfig() factory.Config {
return &Config{
Timeout: Timeout{
Default: 60 * time.Second,
Max: 600 * time.Second,
ExcludedRoutes: []string{
"/api/v1/logs/tail",
"/api/v3/logs/livetail",
},
},
}
}

func (c Config) Validate() error {
return nil
}
51 changes: 51 additions & 0 deletions pkg/apiserver/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package apiserver

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.signoz.io/signoz/pkg/config"
"go.signoz.io/signoz/pkg/config/envprovider"
"go.signoz.io/signoz/pkg/factory"
)

func TestNewWithEnvProvider(t *testing.T) {
t.Setenv("SIGNOZ_APISERVER_TIMEOUT_DEFAULT", "70s")
t.Setenv("SIGNOZ_APISERVER_TIMEOUT_MAX", "700s")
t.Setenv("SIGNOZ_APISERVER_TIMEOUT_EXCLUDED__ROUTES", "/excluded1,/excluded2")

conf, err := config.New(
context.Background(),
config.ResolverConfig{
Uris: []string{"env:"},
ProviderFactories: []config.ProviderFactory{
envprovider.NewFactory(),
},
},
[]factory.ConfigFactory{
NewConfigFactory(),
},
)
require.NoError(t, err)

actual := &Config{}
err = conf.Unmarshal("apiserver", actual)

require.NoError(t, err)

expected := &Config{
Timeout: Timeout{
Default: 70 * time.Second,
Max: 700 * time.Second,
ExcludedRoutes: []string{
"/excluded1",
"/excluded2",
},
},
}

assert.Equal(t, expected, actual)
}
7 changes: 4 additions & 3 deletions pkg/http/middleware/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ type Timeout struct {
maxTimeout time.Duration
}

func NewTimeout(logger *zap.Logger, excluded map[string]struct{}, defaultTimeout time.Duration, maxTimeout time.Duration) *Timeout {
func NewTimeout(logger *zap.Logger, excludedRoutes []string, defaultTimeout time.Duration, maxTimeout time.Duration) *Timeout {
if logger == nil {
panic("cannot build timeout, logger is empty")
}

if excluded == nil {
excluded = make(map[string]struct{})
excluded := make(map[string]struct{}, len(excludedRoutes))
for _, route := range excludedRoutes {
excluded[route] = struct{}{}
}

if defaultTimeout.Seconds() == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/http/middleware/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestTimeout(t *testing.T) {
writeTimeout := 6 * time.Second
defaultTimeout := 2 * time.Second
maxTimeout := 4 * time.Second
m := NewTimeout(zap.NewNop(), map[string]struct{}{"/excluded": {}}, defaultTimeout, maxTimeout)
m := NewTimeout(zap.NewNop(), []string{"/excluded"}, defaultTimeout, maxTimeout)

listener, err := net.Listen("tcp", "localhost:0")
require.NoError(t, err)
Expand Down
47 changes: 11 additions & 36 deletions pkg/query-service/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
)

type ServerOptions struct {
Config signoz.Config
PromConfigPath string
SkipTopLvlOpsPath string
HTTPHostPort string
Expand Down Expand Up @@ -271,7 +272,11 @@ func (s *Server) createPrivateServer(api *APIHandler) (*http.Server, error) {

r := NewRouter()

r.Use(setTimeoutMiddleware)
r.Use(middleware.NewTimeout(zap.L(),
s.serverOptions.Config.APIServer.Timeout.ExcludedRoutes,
s.serverOptions.Config.APIServer.Timeout.Default,
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(s.analyticsMiddleware)
r.Use(middleware.NewLogging(zap.L()).Wrap)

Expand All @@ -297,7 +302,11 @@ func (s *Server) createPublicServer(api *APIHandler, web web.Web) (*http.Server,

r := NewRouter()

r.Use(setTimeoutMiddleware)
r.Use(middleware.NewTimeout(zap.L(),
s.serverOptions.Config.APIServer.Timeout.ExcludedRoutes,
s.serverOptions.Config.APIServer.Timeout.Default,
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(s.analyticsMiddleware)
r.Use(middleware.NewLogging(zap.L()).Wrap)
r.Use(LogCommentEnricher)
Expand Down Expand Up @@ -580,40 +589,6 @@ func (s *Server) analyticsMiddleware(next http.Handler) http.Handler {
})
}

// TODO(remove): Implemented at pkg/http/middleware/timeout.go
func getRouteContextTimeout(overrideTimeout string) time.Duration {
var timeout time.Duration
var err error
if overrideTimeout != "" {
timeout, err = time.ParseDuration(overrideTimeout + "s")
if err != nil {
timeout = constants.ContextTimeout
}
if timeout > constants.ContextTimeoutMaxAllowed {
timeout = constants.ContextTimeoutMaxAllowed
}
return timeout
}
return constants.ContextTimeout
}

// TODO(remove): Implemented at pkg/http/middleware/timeout.go
func setTimeoutMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var cancel context.CancelFunc
// check if route is not excluded
url := r.URL.Path
if _, ok := constants.TimeoutExcludedRoutes[url]; !ok {
ctx, cancel = context.WithTimeout(r.Context(), getRouteContextTimeout(r.Header.Get("timeout")))
defer cancel()
}

r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}

// initListeners initialises listeners of the server
func (s *Server) initListeners() error {
// listen on public port
Expand Down
42 changes: 0 additions & 42 deletions pkg/query-service/app/server_test.go

This file was deleted.

27 changes: 0 additions & 27 deletions pkg/query-service/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,26 +153,6 @@ var DEFAULT_FEATURE_SET = model.FeatureSet{
},
}

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()

func GetContextTimeoutMaxAllowed() time.Duration {
contextTimeoutStr := GetOrDefaultEnv("CONTEXT_TIMEOUT_MAX_ALLOWED", "600")
contextTimeoutDuration, err := time.ParseDuration(contextTimeoutStr + "s")
if err != nil {
return time.Minute
}
return contextTimeoutDuration
}

func GetEvalDelay() time.Duration {
evalDelayStr := GetOrDefaultEnv("RULES_EVAL_DELAY", "2m")
evalDelayDuration, err := time.ParseDuration(evalDelayStr)
Expand All @@ -182,8 +162,6 @@ func GetEvalDelay() time.Duration {
return evalDelayDuration
}

var ContextTimeoutMaxAllowed = GetContextTimeoutMaxAllowed()

const (
TraceID = "traceID"
ServiceName = "serviceName"
Expand Down Expand Up @@ -255,11 +233,6 @@ const (
SIGNOZ_TOP_LEVEL_OPERATIONS_TABLENAME = "distributed_top_level_operations"
)

var TimeoutExcludedRoutes = map[string]bool{
"/api/v1/logs/tail": true,
"/api/v3/logs/livetail": true,
}

// alert related constants
const (
// AlertHelpPage is used in case default alert repo url is not set
Expand Down
14 changes: 0 additions & 14 deletions pkg/query-service/constants/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package constants
import (
"os"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
)
Expand All @@ -20,16 +19,3 @@ 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))
})
})
}
Loading

0 comments on commit 2d99c29

Please sign in to comment.