-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api,sdk): Generate call stack for sdk error and preserve stack t…
…race (#3310)
- Loading branch information
Showing
21 changed files
with
475 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ovh/cds/cli" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
adminErrorsCmd = cli.Command{ | ||
Name: "errors", | ||
Short: "Manage CDS errors", | ||
} | ||
|
||
adminErrors = cli.NewCommand(adminErrorsCmd, nil, | ||
[]*cobra.Command{ | ||
cli.NewCommand(adminErrorsGetCmd, adminErrorsGetFunc, nil), | ||
}) | ||
) | ||
|
||
var adminErrorsGetCmd = cli.Command{ | ||
Name: "get", | ||
Short: "Get CDS error", | ||
Args: []cli.Arg{ | ||
{Name: "uuid"}, | ||
}, | ||
} | ||
|
||
func adminErrorsGetFunc(v cli.Values) error { | ||
res, err := client.MonErrorsGet(v.GetString("uuid")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Message: %s\n", res.Message) | ||
if res.StackTrace != "" { | ||
fmt.Printf("Stack trace:\n%s", res.StackTrace) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,76 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
|
||
"github.com/ovh/cds/sdk" | ||
"github.com/ovh/cds/engine/service" | ||
) | ||
|
||
type graylogResponse struct { | ||
TotalResult int `json:"total_results"` | ||
Messages []struct { | ||
Message map[string]interface{} `json:"message"` | ||
} `json:"messages"` | ||
} | ||
|
||
func (api *API) getErrorHandler() service.Handler { | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
vars := mux.Vars(r) | ||
uuid := vars["uuid"] | ||
|
||
if api.Config.Graylog.URL == "" || api.Config.Graylog.AccessToken == "" { | ||
return sdk.ErrNotImplemented | ||
} | ||
|
||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/search/universal/absolute", api.Config.Graylog.URL), nil) | ||
if err != nil { | ||
return sdk.WrapError(err, "invalid given Graylog url") | ||
} | ||
|
||
q := req.URL.Query() | ||
q.Add("query", fmt.Sprintf("error_uuid:%s", uuid)) | ||
q.Add("from", "1970-01-01 00:00:00.000") | ||
q.Add("to", time.Now().Format("2006-01-02 15:04:05")) | ||
q.Add("limit", "1") | ||
req.URL.RawQuery = q.Encode() | ||
|
||
req.SetBasicAuth(api.Config.Graylog.AccessToken, "token") | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return sdk.WrapError(err, "cannot send query to Graylog") | ||
} | ||
|
||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return sdk.WrapError(err, "cannot read response from Graylog") | ||
} | ||
|
||
var res graylogResponse | ||
if err := json.Unmarshal(body, &res); err != nil { | ||
return sdk.WrapError(err, "cannot unmarshal response from Graylog") | ||
} | ||
|
||
if res.TotalResult < 1 { | ||
return sdk.ErrNotFound | ||
} | ||
|
||
e := sdk.Error{ | ||
UUID: res.Messages[0].Message["error_uuid"].(string), | ||
Message: res.Messages[0].Message["message"].(string), | ||
} | ||
if st, ok := res.Messages[0].Message["stack_trace"]; ok { | ||
e.StackTrace = st.(string) | ||
} | ||
|
||
return service.WriteJSON(w, e, http.StatusOK) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.