Skip to content

Commit

Permalink
MM-43004: better logging (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
levb committed Apr 27, 2022
1 parent 9e0ed4b commit 95a20a4
Show file tree
Hide file tree
Showing 85 changed files with 1,199 additions and 659 deletions.
25 changes: 25 additions & 0 deletions apps/call.go
Expand Up @@ -5,6 +5,9 @@ package apps

import (
"encoding/json"
"fmt"

"github.com/mattermost/mattermost-plugin-apps/utils"
)

// Call defines a way to invoke an App's function. Calls are used to fetch App's
Expand Down Expand Up @@ -128,3 +131,25 @@ func (c *Call) PartialCopy() *Call {
}
return &clone
}

func (c Call) String() string {
s := c.Path
if c.Expand != nil {
s += fmt.Sprintf(", expand: %v", c.Expand.String())
}
if c.State != nil {
s += fmt.Sprintf(", state: %v", utils.LogDigest(c.State))
}
return s
}

func (c Call) Loggable() []interface{} {
props := []interface{}{"call_path", c.Path}
if c.Expand != nil {
props = append(props, "call_expand", c.Expand.String())
}
if c.State != nil {
props = append(props, "call_state", utils.LogDigest(c.State))
}
return props
}
25 changes: 25 additions & 0 deletions apps/call_request.go
Expand Up @@ -5,7 +5,10 @@ package apps

import (
"encoding/json"
"fmt"
"io"

"github.com/mattermost/mattermost-plugin-apps/utils"
)

// CallRequest envelops all requests sent to Apps.
Expand Down Expand Up @@ -138,3 +141,25 @@ func (creq *CallRequest) BoolValue(name string) bool {

return false
}

func (creq CallRequest) String() string {
s := fmt.Sprintf("call: %s, context: %s", creq.Call.String(), creq.Context.String())
if len(creq.Values) > 0 {
s += ", values: " + utils.LogDigest(creq.Values)
}
if creq.Query != "" {
s += ", query: " + creq.Query
}
return s
}

func (creq CallRequest) Loggable() []interface{} {
props := append([]interface{}{}, creq.Call, creq.Context)
if len(creq.Values) > 0 {
props = append(props, "values", utils.LogDigest(creq.Values))
}
if creq.Query != "" {
props = append(props, "query", creq.Query)
}
return props
}
82 changes: 79 additions & 3 deletions apps/call_response.go
Expand Up @@ -5,6 +5,8 @@ package apps

import (
"fmt"

"github.com/mattermost/mattermost-plugin-apps/utils"
)

type CallResponseType string
Expand Down Expand Up @@ -103,9 +105,83 @@ func NewLookupResponse(opts []SelectOption) CallResponse {
}

// Error makes CallResponse a valid error, for convenience
func (cr CallResponse) Error() string {
if cr.Type == CallResponseTypeError {
return cr.Text
func (cresp CallResponse) Error() string {
if cresp.Type == CallResponseTypeError {
return cresp.Text
}
return ""
}

func (cresp CallResponse) String() string {
switch cresp.Type {
case CallResponseTypeError:
return fmt.Sprint("Error: ", cresp.Text)

case "", CallResponseTypeOK:
switch {
case cresp.Text != "" && cresp.Data == nil:
return fmt.Sprint("OK: ", cresp.Text)
case cresp.Text != "" && cresp.Data != nil:
return fmt.Sprintf("OK: %s; + data type %T", cresp.Text, cresp.Data)
case cresp.Data != nil:
return fmt.Sprintf("OK: data type %T, value: %v", cresp.Data, cresp.Data)
default:
return "OK: (none)"
}

case CallResponseTypeForm:
return fmt.Sprintf("Form: %v", utils.ToJSON(cresp.Form))

case CallResponseTypeCall:
return fmt.Sprintf("Call: %v", cresp.Call)

case CallResponseTypeNavigate:
s := fmt.Sprintf("Navigate to: %q", cresp.NavigateToURL)
if cresp.UseExternalBrowser {
s += ", using external browser"
}
return s

default:
return fmt.Sprintf("?? unknown response type %q", cresp.Type)
}
}

func (cresp CallResponse) Loggable() []interface{} {
props := []interface{}{"response_type", string(cresp.Type)}

switch cresp.Type {
case CallResponseTypeError:
props = append(props, "error", cresp.Text)

case "", CallResponseTypeOK:
if cresp.Text != "" {
text := cresp.Text
if len(text) > 100 {
text = text[:100] + "...(truncated)"
}
props = append(props, "response_text", text)
}
if cresp.Data != nil {
props = append(props, "response_data", cresp.Data)
}

case CallResponseTypeForm:
if cresp.Form != nil {
props = append(props, "response_form", *cresp.Form)
}

case CallResponseTypeCall:
if cresp.Call != nil {
props = append(props, "response_call", cresp.Call.String())
}

case CallResponseTypeNavigate:
props = append(props, "response_url", cresp.NavigateToURL)
if cresp.UseExternalBrowser {
props = append(props, "use_external_browser", true)
}
}

return props
}
116 changes: 116 additions & 0 deletions apps/context.go
Expand Up @@ -4,7 +4,13 @@
package apps

import (
"fmt"
"sort"
"strings"

"github.com/mattermost/mattermost-server/v6/model"

"github.com/mattermost/mattermost-plugin-apps/utils"
)

// Context is included in CallRequest and provides App with information about
Expand Down Expand Up @@ -118,3 +124,113 @@ type OAuth2Context struct {

User interface{} `json:"user,omitempty"`
}

func (c Context) String() string {
display, _ := c.loggable()

keys := []string{}
for k := range display {
keys = append(keys, k)
}
sort.Strings(keys)

ss := []string{}
for _, k := range keys {
ss = append(ss, fmt.Sprintf("%s: %s", k, display[k]))
}
return strings.Join(ss, ", ")
}

func (c Context) Loggable() []interface{} {
_, props := c.loggable()
return props
}

func (c Context) loggable() (map[string]string, []interface{}) {
display := map[string]string{}
props := []interface{}{}
add := func(f, v string) {
if v != "" {
display[f] = v
props = append(props, f, v)
}
}

add("locale", c.Locale)
add("subject", string(c.Subject))
add("ua", c.UserAgentContext.UserAgent)
add("ua_loc", string(c.UserAgentContext.Location))
if !c.UserAgentContext.TrackAsSubmit {
add("is_not_submit", "true")
}

if c.ExpandedContext.ActingUser != nil {
display["acting_user"] = c.ExpandedContext.ActingUser.GetDisplayName(model.ShowNicknameFullName)
props = append(props, "acting_user_id", c.ExpandedContext.ActingUser.Id)
}
if c.ExpandedContext.ActingUserAccessToken != "" {
display["acting_user_access_token"] = utils.LastN(c.ExpandedContext.ActingUserAccessToken, 4)
props = append(props, "acting_user_access_token", utils.LastN(c.ExpandedContext.ActingUserAccessToken, 4))
}

if c.ExpandedContext.Channel != nil {
display["channel"] = c.ExpandedContext.Channel.Name
props = append(props, "channel_id", c.ExpandedContext.Channel.Id)
}
if c.ExpandedContext.Team != nil {
display["team"] = c.ExpandedContext.Team.Name
props = append(props, "team_id", c.ExpandedContext.Channel.Id)
}
if c.ExpandedContext.Post != nil {
display["post"] = utils.LastN(c.ExpandedContext.Post.Message, 32)
props = append(props, "post_id", c.ExpandedContext.Post.Id)
}
if c.ExpandedContext.RootPost != nil {
display["root_post"] = utils.LastN(c.ExpandedContext.RootPost.Message, 32)
props = append(props, "root_post_id", c.ExpandedContext.RootPost.Id)
}

if c.ExpandedContext.BotUserID != "" {
display["bot_user_id"] = c.ExpandedContext.BotUserID
props = append(props, "bot_user_id", c.ExpandedContext.BotUserID)
}
if c.ExpandedContext.BotAccessToken != "" {
display["bot_access_token"] = utils.LastN(c.ExpandedContext.BotAccessToken, 4)
props = append(props, "bot_access_token", utils.LastN(c.ExpandedContext.BotAccessToken, 4))
}
if c.ExpandedContext.ChannelMember != nil {
display["channel_member_channel_id"] = c.ExpandedContext.ChannelMember.ChannelId
display["channel_member_user_id"] = c.ExpandedContext.ChannelMember.UserId
props = append(props, "channel_member_channel_id", c.ExpandedContext.ChannelMember.ChannelId)
props = append(props, "channel_member_user_id", c.ExpandedContext.ChannelMember.UserId)
}
if c.ExpandedContext.TeamMember != nil {
display["team_member_team_id"] = c.ExpandedContext.TeamMember.TeamId
display["team_member_user_id"] = c.ExpandedContext.TeamMember.UserId
props = append(props, "team_member_team_id", c.ExpandedContext.TeamMember.TeamId)
props = append(props, "team_member_user_id", c.ExpandedContext.TeamMember.UserId)
}

if c.ExpandedContext.OAuth2.OAuth2App.RemoteRootURL != "" {
display["remote_url"] = c.ExpandedContext.OAuth2.OAuth2App.RemoteRootURL
props = append(props, "remote_url", c.ExpandedContext.OAuth2.OAuth2App.RemoteRootURL)
}
if c.ExpandedContext.OAuth2.OAuth2App.ClientID != "" {
display["remote_client_id"] = utils.LastN(c.ExpandedContext.OAuth2.OAuth2App.ClientID, 4)
props = append(props, "remote_client_id", utils.LastN(c.ExpandedContext.OAuth2.OAuth2App.ClientID, 4))
}
if c.ExpandedContext.OAuth2.OAuth2App.ClientSecret != "" {
display["remote_client_secret"] = utils.LastN(c.ExpandedContext.OAuth2.OAuth2App.ClientSecret, 4)
props = append(props, "remote_client_secret", utils.LastN(c.ExpandedContext.OAuth2.OAuth2App.ClientSecret, 4))
}
if c.ExpandedContext.OAuth2.OAuth2App.Data != nil {
display["app_data"] = "(private)"
props = append(props, "app_data", "(private)")
}
if c.ExpandedContext.OAuth2.User != nil {
display["user_data"] = "(private)"
props = append(props, "user_data", "(private)")
}

return display, props
}
18 changes: 18 additions & 0 deletions apps/expand.go
Expand Up @@ -3,6 +3,13 @@

package apps

import (
"sort"
"strings"

"github.com/mattermost/mattermost-plugin-apps/utils"
)

type ExpandLevel string

const (
Expand Down Expand Up @@ -81,3 +88,14 @@ type Expand struct {
// previously stored with appclient.StoreOAuthUser).
OAuth2User ExpandLevel `json:"oauth2_user,omitempty"`
}

func (e Expand) String() string {
m := map[string]string{}
utils.Remarshal(&m, e)
ss := []string{}
for k, v := range m {
ss = append(ss, k+":"+v)
}
sort.Strings(ss)
return strings.Join(ss, ",")
}

0 comments on commit 95a20a4

Please sign in to comment.