mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
added change for 'fn_id' to propagate through to logs (#1351)
* edited logging variable format to fit same standard across all fn related projects * changing naming convention of param urls to contain lowercase_underscores and removing camelCase to lowercase_underscored normalizing func & refactoring duplicated variables
This commit is contained in:
29
api/const.go
29
api/const.go
@@ -1,24 +1,21 @@
|
||||
package api
|
||||
|
||||
const (
|
||||
// AppName is the app name context key
|
||||
// AppName is the app name context key & url path parameter
|
||||
AppName string = "app_name"
|
||||
// AppID is the app id context key
|
||||
// AppID is the app id context key & url path parameter
|
||||
AppID string = "app_id"
|
||||
// FnID is the fn id context key & url path parameter
|
||||
|
||||
// ParamAppID is the url path parameter for app id
|
||||
ParamAppID string = "appID"
|
||||
// ParamAppName is the url path parameter for app name
|
||||
ParamAppName string = "appName"
|
||||
// ParamTriggerID is the url path parameter for trigger id
|
||||
ParamTriggerID string = "triggerID"
|
||||
// ParamCallID is the url path parameter for call id
|
||||
ParamCallID string = "callID"
|
||||
// ParamFnID is the url path parameter for fn id
|
||||
ParamFnID string = "fnID"
|
||||
// ParamTriggerSource is the triggers source parameter
|
||||
ParamTriggerSource string = "triggerSource"
|
||||
// TriggerID is the url path parameter for trigger id
|
||||
TriggerID string = "trigger_id"
|
||||
// CallID is the url path parameter for call id
|
||||
CallID string = "call_id"
|
||||
// FnID is the url path parameter for fn id
|
||||
FnID string = "fn_id"
|
||||
// TriggerSource is the triggers source parameter
|
||||
TriggerSource string = "trigger_source"
|
||||
|
||||
//ParamTriggerType is the trigger type parameter - only used in hybrid API
|
||||
ParamTriggerType string = "triggerType"
|
||||
//TriggerType is the trigger type parameter - only used in hybrid API
|
||||
TriggerType string = "trigger_type"
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func (s *Server) handleAppDelete(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
err := s.datastore.RemoveApp(ctx, c.Param(api.ParamAppID))
|
||||
err := s.datastore.RemoveApp(ctx, c.Param(api.AppID))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func (s *Server) handleAppGet(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
appId := c.Param(api.ParamAppID)
|
||||
appId := c.Param(api.AppID)
|
||||
app, err := s.datastore.GetAppByID(ctx, appId)
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
|
||||
@@ -23,7 +23,7 @@ func (s *Server) handleAppUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
id := c.Param(api.ParamAppID)
|
||||
id := c.Param(api.AppID)
|
||||
|
||||
if app.ID == "" {
|
||||
app.ID = id
|
||||
|
||||
@@ -11,20 +11,20 @@ import (
|
||||
func (s *Server) handleCallGet(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
fnID := c.Param(api.ParamFnID)
|
||||
fnID := c.Param(api.FnID)
|
||||
|
||||
if fnID == "" {
|
||||
handleErrorResponse(c, models.ErrFnsMissingID)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := s.datastore.GetFnByID(ctx, c.Param(api.ParamFnID))
|
||||
_, err := s.datastore.GetFnByID(ctx, c.Param(api.FnID))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
callID := c.Param(api.ParamCallID)
|
||||
callID := c.Param(api.CallID)
|
||||
if callID == "" {
|
||||
handleErrorResponse(c, models.ErrDatastoreEmptyCallID)
|
||||
}
|
||||
|
||||
@@ -15,14 +15,14 @@ func (s *Server) handleCallList(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
var err error
|
||||
|
||||
fnID := c.Param(api.ParamFnID)
|
||||
fnID := c.Param(api.FnID)
|
||||
|
||||
if fnID == "" {
|
||||
handleErrorResponse(c, models.ErrFnsMissingID)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.datastore.GetFnByID(ctx, c.Param(api.ParamFnID))
|
||||
_, err = s.datastore.GetFnByID(ctx, c.Param(api.FnID))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
|
||||
@@ -36,8 +36,8 @@ func writeJSON(c *gin.Context, callID string, logReader io.Reader) {
|
||||
func (s *Server) handleCallLogGet(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
fnID := c.Param(api.ParamFnID)
|
||||
callID := c.Param(api.ParamCallID)
|
||||
fnID := c.Param(api.FnID)
|
||||
callID := c.Param(api.CallID)
|
||||
|
||||
logReader, err := s.logstore.GetLog(ctx, fnID, callID)
|
||||
if err != nil {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func (s *Server) handleFnDelete(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
fnID := c.Param(api.ParamFnID)
|
||||
fnID := c.Param(api.FnID)
|
||||
|
||||
err := s.datastore.RemoveFn(ctx, fnID)
|
||||
if err != nil {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
func (s *Server) handleFnGet(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
f, err := s.datastore.GetFnByID(ctx, c.Param(api.ParamFnID))
|
||||
f, err := s.datastore.GetFnByID(ctx, c.Param(api.FnID))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
|
||||
@@ -21,7 +21,7 @@ func (s *Server) handleFnUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
pathFnID := c.Param(api.ParamFnID)
|
||||
pathFnID := c.Param(api.FnID)
|
||||
|
||||
if fn.ID == "" {
|
||||
fn.ID = pathFnID
|
||||
|
||||
@@ -78,9 +78,9 @@ func traceWrap(c *gin.Context) {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
ctx, err := tag.New(c.Request.Context(),
|
||||
tag.Insert(appKey, c.Param(api.ParamAppName)),
|
||||
tag.Insert(appIDKey, c.Param(api.ParamAppID)),
|
||||
tag.Insert(fnKey, c.Param(api.ParamFnID)),
|
||||
tag.Insert(appKey, c.Param(api.AppName)),
|
||||
tag.Insert(appIDKey, c.Param(api.AppID)),
|
||||
tag.Insert(fnKey, c.Param(api.FnID)),
|
||||
)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
@@ -189,18 +189,18 @@ func panicWrap(c *gin.Context) {
|
||||
func loggerWrap(c *gin.Context) {
|
||||
ctx, _ := common.LoggerWithFields(c.Request.Context(), extractFields(c))
|
||||
|
||||
if appName := c.Param(api.ParamAppName); appName != "" {
|
||||
if appName := c.Param(api.AppName); appName != "" {
|
||||
c.Set(api.AppName, appName)
|
||||
ctx = ContextWithApp(ctx, appName)
|
||||
}
|
||||
|
||||
if appID := c.Param(api.ParamAppID); appID != "" {
|
||||
c.Set(api.ParamAppID, appID)
|
||||
if appID := c.Param(api.AppID); appID != "" {
|
||||
c.Set(api.AppID, appID)
|
||||
ctx = ContextWithAppID(ctx, appID)
|
||||
}
|
||||
|
||||
if fnID := c.Param(api.ParamFnID); fnID != "" {
|
||||
c.Set(api.ParamFnID, fnID)
|
||||
if fnID := c.Param(api.FnID); fnID != "" {
|
||||
c.Set(api.FnID, fnID)
|
||||
ctx = ContextWithFnID(ctx, fnID)
|
||||
}
|
||||
|
||||
@@ -211,24 +211,24 @@ func loggerWrap(c *gin.Context) {
|
||||
type ctxFnIDKey string
|
||||
|
||||
func ContextWithFnID(ctx context.Context, fnID string) context.Context {
|
||||
return context.WithValue(ctx, ctxFnIDKey(api.ParamFnID), fnID)
|
||||
return context.WithValue(ctx, ctxFnIDKey(api.FnID), fnID)
|
||||
}
|
||||
|
||||
// FnIDFromContext returns the app from a context, if set.
|
||||
func FnIDFromContext(ctx context.Context) string {
|
||||
r, _ := ctx.Value(ctxFnIDKey(api.ParamFnID)).(string)
|
||||
r, _ := ctx.Value(ctxFnIDKey(api.FnID)).(string)
|
||||
return r
|
||||
}
|
||||
|
||||
type ctxAppIDKey string
|
||||
|
||||
func ContextWithAppID(ctx context.Context, appID string) context.Context {
|
||||
return context.WithValue(ctx, ctxAppIDKey(api.ParamAppID), appID)
|
||||
return context.WithValue(ctx, ctxAppIDKey(api.AppID), appID)
|
||||
}
|
||||
|
||||
// AppIDFromContext returns the app from a context, if set.
|
||||
func AppIDFromContext(ctx context.Context) string {
|
||||
r, _ := ctx.Value(ctxAppIDKey(api.ParamAppID)).(string)
|
||||
r, _ := ctx.Value(ctxAppIDKey(api.AppID)).(string)
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -269,7 +269,7 @@ func (s *Server) checkAppPresenceByName() gin.HandlerFunc {
|
||||
|
||||
func setAppIDInCtx(c *gin.Context) {
|
||||
// add appName to context
|
||||
appID := c.Param(api.ParamAppID)
|
||||
appID := c.Param(api.AppID)
|
||||
|
||||
if appID != "" {
|
||||
c.Set(api.AppID, appID)
|
||||
@@ -279,7 +279,7 @@ func setAppIDInCtx(c *gin.Context) {
|
||||
}
|
||||
|
||||
func appIDCheck(c *gin.Context) {
|
||||
appID := c.GetString(api.ParamAppID)
|
||||
appID := c.GetString(api.AppID)
|
||||
if appID == "" {
|
||||
handleErrorResponse(c, models.ErrAppsMissingID)
|
||||
c.Abort()
|
||||
|
||||
@@ -193,12 +193,12 @@ func (s *Server) handleRunnerGetTriggerBySource(c *gin.Context) {
|
||||
|
||||
appId := c.MustGet(api.AppID).(string)
|
||||
|
||||
triggerType := c.Param(api.ParamTriggerType)
|
||||
triggerType := c.Param(api.TriggerType)
|
||||
if triggerType == "" {
|
||||
handleErrorResponse(c, errors.New("no trigger type in request"))
|
||||
return
|
||||
}
|
||||
triggerSource := strings.TrimPrefix(c.Param(api.ParamTriggerSource), "/")
|
||||
triggerSource := strings.TrimPrefix(c.Param(api.TriggerSource), "/")
|
||||
|
||||
trigger, err := s.datastore.GetTriggerBySource(ctx, appId, triggerType, triggerSource)
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ func (s *syncResponseWriter) Status() int { return s.status }
|
||||
|
||||
// handleFnInvokeCall executes the function, for router handlers
|
||||
func (s *Server) handleFnInvokeCall(c *gin.Context) {
|
||||
fnID := c.Param(api.ParamFnID)
|
||||
fnID := c.Param(api.FnID)
|
||||
ctx, _ := common.LoggerWithFields(c.Request.Context(), logrus.Fields{"fn_id": fnID})
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
err := s.handleFnInvokeCall2(c)
|
||||
@@ -56,7 +56,7 @@ func (s *Server) handleFnInvokeCall(c *gin.Context) {
|
||||
// handleTriggerHTTPFunctionCall2 executes the function and returns an error
|
||||
// Requires the following in the context:
|
||||
func (s *Server) handleFnInvokeCall2(c *gin.Context) error {
|
||||
fn, err := s.lbReadAccess.GetFnByID(c, c.Param(api.ParamFnID))
|
||||
fn, err := s.lbReadAccess.GetFnByID(c, c.Param(api.FnID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -25,12 +25,12 @@ func (s *Server) handleHTTPTriggerCall(c *gin.Context) {
|
||||
// Requires the following in the context:
|
||||
func (s *Server) handleTriggerHTTPFunctionCall2(c *gin.Context) error {
|
||||
ctx := c.Request.Context()
|
||||
p := c.Param(api.ParamTriggerSource)
|
||||
p := c.Param(api.TriggerSource)
|
||||
if p == "" {
|
||||
p = "/"
|
||||
}
|
||||
|
||||
appName := c.Param(api.ParamAppName)
|
||||
appName := c.Param(api.AppName)
|
||||
|
||||
appID, err := s.lbReadAccess.GetAppID(ctx, appName)
|
||||
if err != nil {
|
||||
|
||||
@@ -1112,31 +1112,31 @@ func (s *Server) bindHandlers(ctx context.Context) {
|
||||
{
|
||||
v2.GET("/apps", s.handleAppList)
|
||||
v2.POST("/apps", s.handleAppCreate)
|
||||
v2.GET("/apps/:appID", s.handleAppGet)
|
||||
v2.PUT("/apps/:appID", s.handleAppUpdate)
|
||||
v2.DELETE("/apps/:appID", s.handleAppDelete)
|
||||
v2.GET("/apps/:app_id", s.handleAppGet)
|
||||
v2.PUT("/apps/:app_id", s.handleAppUpdate)
|
||||
v2.DELETE("/apps/:app_id", s.handleAppDelete)
|
||||
|
||||
v2.GET("/fns", s.handleFnList)
|
||||
v2.POST("/fns", s.handleFnCreate)
|
||||
v2.GET("/fns/:fnID", s.handleFnGet)
|
||||
v2.PUT("/fns/:fnID", s.handleFnUpdate)
|
||||
v2.DELETE("/fns/:fnID", s.handleFnDelete)
|
||||
v2.GET("/fns/:fn_id", s.handleFnGet)
|
||||
v2.PUT("/fns/:fn_id", s.handleFnUpdate)
|
||||
v2.DELETE("/fns/:fn_id", s.handleFnDelete)
|
||||
|
||||
v2.GET("/triggers", s.handleTriggerList)
|
||||
v2.POST("/triggers", s.handleTriggerCreate)
|
||||
v2.GET("/triggers/:triggerID", s.handleTriggerGet)
|
||||
v2.PUT("/triggers/:triggerID", s.handleTriggerUpdate)
|
||||
v2.DELETE("/triggers/:triggerID", s.handleTriggerDelete)
|
||||
v2.GET("/triggers/:trigger_id", s.handleTriggerGet)
|
||||
v2.PUT("/triggers/:trigger_id", s.handleTriggerUpdate)
|
||||
v2.DELETE("/triggers/:trigger_id", s.handleTriggerDelete)
|
||||
}
|
||||
|
||||
if !s.noCallEndpoints {
|
||||
v2.GET("/fns/:fnID/calls", s.handleCallList)
|
||||
v2.GET("/fns/:fnID/calls/:callID", s.handleCallGet)
|
||||
v2.GET("/fns/:fnID/calls/:callID/log", s.handleCallLogGet)
|
||||
v2.GET("/fns/:fn_id/calls", s.handleCallList)
|
||||
v2.GET("/fns/:fn_id/calls/:call_id", s.handleCallGet)
|
||||
v2.GET("/fns/:fn_id/calls/:call_id/log", s.handleCallLogGet)
|
||||
} else {
|
||||
v2.GET("/fns/:fnID/calls", s.goneResponse)
|
||||
v2.GET("/fns/:fnID/calls/:callID", s.goneResponse)
|
||||
v2.GET("/fns/:fnID/calls/:callID/log", s.goneResponse)
|
||||
v2.GET("/fns/:fn_id/calls", s.goneResponse)
|
||||
v2.GET("/fns/:fn_id/calls/:call_id", s.goneResponse)
|
||||
v2.GET("/fns/:fn_id/calls/:call_id/log", s.goneResponse)
|
||||
}
|
||||
|
||||
if !s.noHybridAPI { // Hybrid API - this should only be enabled on API servers
|
||||
@@ -1148,11 +1148,11 @@ func (s *Server) bindHandlers(ctx context.Context) {
|
||||
runner.POST("/finish", s.handleRunnerFinish)
|
||||
|
||||
runnerAppAPI := runner.Group(
|
||||
"/apps/:appID")
|
||||
"/apps/:app_id")
|
||||
runnerAppAPI.Use(setAppIDInCtx)
|
||||
// Both of these are somewhat odd -
|
||||
// Deprecate, remove with routes
|
||||
runnerAppAPI.GET("/triggerBySource/:triggerType/*triggerSource", s.handleRunnerGetTriggerBySource)
|
||||
runnerAppAPI.GET("/triggerBySource/:trigger_type/*trigger_source", s.handleRunnerGetTriggerBySource)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1160,13 +1160,13 @@ func (s *Server) bindHandlers(ctx context.Context) {
|
||||
case ServerTypeFull, ServerTypeLB, ServerTypeRunner:
|
||||
if !s.noHTTTPTriggerEndpoint {
|
||||
lbTriggerGroup := engine.Group("/t")
|
||||
lbTriggerGroup.Any("/:appName", s.handleHTTPTriggerCall)
|
||||
lbTriggerGroup.Any("/:appName/*triggerSource", s.handleHTTPTriggerCall)
|
||||
lbTriggerGroup.Any("/:app_name", s.handleHTTPTriggerCall)
|
||||
lbTriggerGroup.Any("/:app_name/*trigger_source", s.handleHTTPTriggerCall)
|
||||
}
|
||||
|
||||
if !s.noFnInvokeEndpoint {
|
||||
lbFnInvokeGroup := engine.Group("/invoke")
|
||||
lbFnInvokeGroup.POST("/:fnID", s.handleFnInvokeCall)
|
||||
lbFnInvokeGroup.POST("/:fn_id", s.handleFnInvokeCall)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func (s *Server) handleTriggerDelete(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
err := s.datastore.RemoveTrigger(ctx, c.Param(api.ParamTriggerID))
|
||||
err := s.datastore.RemoveTrigger(ctx, c.Param(api.TriggerID))
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
return
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
func (s *Server) handleTriggerGet(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
|
||||
trigger, err := s.datastore.GetTriggerByID(ctx, c.Param(api.ParamTriggerID))
|
||||
trigger, err := s.datastore.GetTriggerByID(ctx, c.Param(api.TriggerID))
|
||||
|
||||
if err != nil {
|
||||
handleErrorResponse(c, err)
|
||||
|
||||
@@ -21,7 +21,7 @@ func (s *Server) handleTriggerUpdate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
pathTriggerID := c.Param(api.ParamTriggerID)
|
||||
pathTriggerID := c.Param(api.TriggerID)
|
||||
|
||||
if trigger.ID == "" {
|
||||
trigger.ID = pathTriggerID
|
||||
|
||||
Reference in New Issue
Block a user