Skip to content

Commit

Permalink
Merge pull request #702 from luraproject/endpoint_info_on_backend_logs
Browse files Browse the repository at this point in the history
Add parent endpoint info to Backend
  • Loading branch information
taik0 authored Jan 11, 2024
2 parents 1196698 + 73e4438 commit b6784e5
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ type Backend struct {
HeadersToPass []string `mapstructure:"input_headers"`
// QueryStringsToPass has the list of query string params to be sent to the backend
QueryStringsToPass []string `mapstructure:"input_query_strings"`

// ParentEndpoint is to be filled by the parent endpoint with its pattern enpoint
// so logs and other instrumentation can output better info (thus, it is not loaded
// with `mapstructure` or `json` tags).
ParentEndpoint string `json:"-" mapstructure:"-"`
}

// Plugin contains the config required by the plugin module
Expand Down Expand Up @@ -497,6 +502,8 @@ func (s *ServiceConfig) initEndpoints() error {
e.ExtraConfig.sanitize()

for j, b := range e.Backend {
// we "tell" the backend which is his parent endpoint
b.ParentEndpoint = e.Endpoint
if err := s.initBackendDefaults(i, j); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions proxy/concurrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
// NewConcurrentMiddlewareWithLogger creates a proxy middleware that enables sending several requests concurrently
func NewConcurrentMiddlewareWithLogger(logger logging.Logger, remote *config.Backend) Middleware {
if remote.ConcurrentCalls == 1 {
logger.Fatal("too few concurrent calls: NewConcurrentMiddleware expects more than 1 concurrent call, got %d", remote.ConcurrentCalls)
logger.Fatal("too few concurrent calls for %s -> %s: NewConcurrentMiddleware expects more than 1 concurrent call, got %d", remote.ParentEndpoint, remote.URLPattern, remote.ConcurrentCalls)
return nil
}
serviceTimeout := time.Duration(75*remote.Timeout.Nanoseconds()/100) * time.Nanosecond

return func(next ...Proxy) Proxy {
if len(next) > 1 {
logger.Fatal("too many proxies for this proxy middleware: NewConcurrentMiddleware only accepts 1 proxy, got %d", len(next))
logger.Fatal("too many proxies for this %s -> %s proxy middleware: NewConcurrentMiddleware only accepts 1 proxy, got %d", remote.ParentEndpoint, remote.URLPattern, len(next))
return nil
}

Expand Down
7 changes: 4 additions & 3 deletions proxy/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewGraphQLMiddleware(logger logging.Logger, remote *config.Backend) Middlew
if err != nil {
if err != graphql.ErrNoConfigFound {
logger.Warning(
fmt.Sprintf("[BACKEND: %s][GraphQL] %s", remote.URLPattern, err.Error()))
fmt.Sprintf("[BACKEND: %s -> %s][GraphQL] %s", remote.ParentEndpoint, remote.URLPattern, err.Error()))
}
return emptyMiddlewareFallback(logger)
}
Expand Down Expand Up @@ -68,13 +68,14 @@ func NewGraphQLMiddleware(logger logging.Logger, remote *config.Backend) Middlew

return func(next ...Proxy) Proxy {
if len(next) > 1 {
logger.Fatal("too many proxies for this proxy middleware: NewGraphQLMiddleware only accepts 1 proxy, got %d", len(next))
logger.Fatal("too many proxies for this %s -> %s proxy middleware: NewGraphQLMiddleware only accepts 1 proxy, got %d", remote.ParentEndpoint, remote.URLPattern, len(next))
return nil
}

logger.Debug(
fmt.Sprintf(
"[BACKEND: %s][GraphQL] Operation: %s, Method: %s",
"[BACKEND: %s -> %s][GraphQL] Operation: %s, Method: %s",
remote.ParentEndpoint,
remote.URLPattern,
opt.Type,
opt.Method,
Expand Down
2 changes: 1 addition & 1 deletion proxy/headers_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewFilterHeadersMiddleware(logger logging.Logger, remote *config.Backend) M

return func(next ...Proxy) Proxy {
if len(next) > 1 {
logger.Fatal("too many proxies for this proxy middleware: NewFilterHeadersMiddleware only accepts 1 proxy, got %d", len(next))
logger.Fatal("too many proxies for this %s -> %s proxy middleware: NewFilterHeadersMiddleware only accepts 1 proxy, got %d", remote.ParentEndpoint, remote.URLPattern, len(next))
return nil
}
nextProxy := next[0]
Expand Down
2 changes: 1 addition & 1 deletion proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func NewRequestBuilderMiddlewareWithLogger(logger logging.Logger, remote *config
func newRequestBuilderMiddleware(l logging.Logger, remote *config.Backend) Middleware {
return func(next ...Proxy) Proxy {
if len(next) > 1 {
l.Fatal("too many proxies for this proxy middleware: newRequestBuilderMiddleware only accepts 1 proxy, got %d", len(next))
l.Fatal("too many proxies for this %s -> %s proxy middleware: newRequestBuilderMiddleware only accepts 1 proxy, got %d", remote.ParentEndpoint, remote.URLPattern, len(next))
return nil
}
return func(ctx context.Context, request *Request) (*Response, error) {
Expand Down
3 changes: 2 additions & 1 deletion proxy/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func NewBackendPluginMiddleware(logger logging.Logger, remote *config.Backend) M
return emptyMiddlewareFallback(logger)
}

return newPluginMiddleware(logger, "BACKEND", remote.URLPattern, cfg)
return newPluginMiddleware(logger, "BACKEND",
fmt.Sprintf("%s -> %s", remote.ParentEndpoint, remote.URLPattern), cfg)
}

func newPluginMiddleware(logger logging.Logger, tag, pattern string, cfg map[string]interface{}) Middleware {
Expand Down
2 changes: 1 addition & 1 deletion proxy/query_strings_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func NewFilterQueryStringsMiddleware(logger logging.Logger, remote *config.Backe

return func(next ...Proxy) Proxy {
if len(next) > 1 {
logger.Fatal("too many proxies for this proxy middleware: NewFilterQueryStringsMiddleware only accepts 1 proxy, got %d", len(next))
logger.Fatal("too many proxies for this %s -> %s proxy middleware: NewFilterQueryStringsMiddleware only accepts 1 proxy, got %d", remote.ParentEndpoint, remote.URLPattern, len(next))
return nil
}
nextProxy := next[0]
Expand Down
3 changes: 2 additions & 1 deletion transport/http/client/plugin/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package plugin

import (
"context"
"fmt"
"net/http"
"net/http/httptest"

Expand All @@ -27,7 +28,7 @@ func HTTPRequestExecutorWithContext(
next func(*config.Backend) client.HTTPRequestExecutor,
) func(*config.Backend) client.HTTPRequestExecutor {
return func(cfg *config.Backend) client.HTTPRequestExecutor {
logPrefix := "[BACKEND: " + cfg.URLPattern + "]"
logPrefix := fmt.Sprintf("[BACKEND: %s -> %s]", cfg.ParentEndpoint, cfg.URLPattern)
v, ok := cfg.ExtraConfig[Namespace]
if !ok {
return next(cfg)
Expand Down

0 comments on commit b6784e5

Please sign in to comment.