[feature] Add a request ID and include it in logs (#1476)

This adds a lightweight form of tracing to GTS. Each incoming request is
assigned a Request ID which we then pass on and log in all our log
lines. Any function that gets called downstream from an HTTP handler
should now emit a requestID=value pair whenever it logs something.

Co-authored-by: kim <grufwub@gmail.com>
This commit is contained in:
Daenney 2023-02-17 12:02:29 +01:00 committed by GitHub
commit 68e6d08c76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
118 changed files with 813 additions and 591 deletions

View file

@ -57,7 +57,7 @@ func (f *formatter) FromPlainEmojiOnly(ctx context.Context, pmf gtsmodel.ParseMe
var htmlContentBytes bytes.Buffer
err := md.Convert([]byte(plain), &htmlContentBytes)
if err != nil {
log.Errorf("error formatting plaintext to HTML: %s", err)
log.Errorf(ctx, "error formatting plaintext to HTML: %s", err)
}
result.HTML = htmlContentBytes.String()
@ -65,7 +65,10 @@ func (f *formatter) FromPlainEmojiOnly(ctx context.Context, pmf gtsmodel.ParseMe
result.HTML = SanitizeHTML(result.HTML)
// shrink ray
result.HTML = minifyHTML(result.HTML)
result.HTML, err = m.String("text/html", result.HTML)
if err != nil {
log.Errorf(ctx, "error minifying HTML: %s", err)
}
return result
}

View file

@ -53,9 +53,11 @@ type emoji struct {
Segment text.Segment
}
var kindMention = ast.NewNodeKind("Mention")
var kindHashtag = ast.NewNodeKind("Hashtag")
var kindEmoji = ast.NewNodeKind("Emoji")
var (
kindMention = ast.NewNodeKind("Mention")
kindHashtag = ast.NewNodeKind("Hashtag")
kindEmoji = ast.NewNodeKind("Emoji")
)
func (n *mention) Kind() ast.NodeKind {
return kindMention
@ -106,14 +108,11 @@ func newEmoji(s text.Segment) *emoji {
}
// mentionParser and hashtagParser fulfil the goldmark parser.InlineParser interface.
type mentionParser struct {
}
type mentionParser struct{}
type hashtagParser struct {
}
type hashtagParser struct{}
type emojiParser struct {
}
type emojiParser struct{}
func (p *mentionParser) Trigger() []byte {
return []byte{'@'}
@ -239,7 +238,7 @@ func (r *customRenderer) renderMention(w mdutil.BufWriter, source []byte, node a
n, ok := node.(*mention) // this function is only registered for kindMention
if !ok {
log.Errorf("type assertion failed")
log.Panic(nil, "type assertion failed")
}
text := string(n.Segment.Value(source))
@ -247,7 +246,7 @@ func (r *customRenderer) renderMention(w mdutil.BufWriter, source []byte, node a
// we don't have much recourse if this fails
if _, err := w.WriteString(html); err != nil {
log.Errorf("error writing HTML: %s", err)
log.Errorf(nil, "error writing HTML: %s", err)
}
return ast.WalkSkipChildren, nil
}
@ -259,7 +258,7 @@ func (r *customRenderer) renderHashtag(w mdutil.BufWriter, source []byte, node a
n, ok := node.(*hashtag) // this function is only registered for kindHashtag
if !ok {
log.Errorf("type assertion failed")
log.Panic(nil, "type assertion failed")
}
text := string(n.Segment.Value(source))
@ -268,7 +267,7 @@ func (r *customRenderer) renderHashtag(w mdutil.BufWriter, source []byte, node a
_, err := w.WriteString(html)
// we don't have much recourse if this fails
if err != nil {
log.Errorf("error writing HTML: %s", err)
log.Errorf(nil, "error writing HTML: %s", err)
}
return ast.WalkSkipChildren, nil
}
@ -281,7 +280,7 @@ func (r *customRenderer) renderEmoji(w mdutil.BufWriter, source []byte, node ast
n, ok := node.(*emoji) // this function is only registered for kindEmoji
if !ok {
log.Errorf("type assertion failed")
log.Panic(nil, "type assertion failed")
}
text := string(n.Segment.Value(source))
shortcode := text[1 : len(text)-1]
@ -289,7 +288,7 @@ func (r *customRenderer) renderEmoji(w mdutil.BufWriter, source []byte, node ast
emoji, err := r.f.db.GetEmojiByShortcodeDomain(r.ctx, shortcode, "")
if err != nil {
if err != db.ErrNoEntries {
log.Errorf("error getting local emoji with shortcode %s: %s", shortcode, err)
log.Errorf(nil, "error getting local emoji with shortcode %s: %s", shortcode, err)
}
} else if *emoji.VisibleInPicker && !*emoji.Disabled {
listed := false
@ -306,7 +305,7 @@ func (r *customRenderer) renderEmoji(w mdutil.BufWriter, source []byte, node ast
// we don't have much recourse if this fails
if _, err := w.WriteString(text); err != nil {
log.Errorf("error writing HTML: %s", err)
log.Errorf(nil, "error writing HTML: %s", err)
}
return ast.WalkSkipChildren, nil
}

View file

@ -53,7 +53,7 @@ func (f *formatter) FromMarkdown(ctx context.Context, pmf gtsmodel.ParseMentionF
var htmlContentBytes bytes.Buffer
err := md.Convert([]byte(markdownText), &htmlContentBytes)
if err != nil {
log.Errorf("error formatting markdown to HTML: %s", err)
log.Errorf(ctx, "error formatting markdown to HTML: %s", err)
}
result.HTML = htmlContentBytes.String()
@ -61,7 +61,10 @@ func (f *formatter) FromMarkdown(ctx context.Context, pmf gtsmodel.ParseMentionF
result.HTML = SanitizeHTML(result.HTML)
// shrink ray
result.HTML = minifyHTML(result.HTML)
result.HTML, err = m.String("text/html", result.HTML)
if err != nil {
log.Errorf(ctx, "error minifying HTML: %s", err)
}
return result
}

View file

@ -19,27 +19,16 @@
package text
import (
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
)
var (
m *minify.M
)
func minifyHTML(content string) string {
if m == nil {
m = minify.New()
m.Add("text/html", &html.Minifier{
KeepEndTags: true,
KeepQuotes: true,
})
}
minified, err := m.String("text/html", content)
if err != nil {
log.Errorf("error minifying HTML: %s", err)
}
return minified
}
// m is the global minify instance.
var m = func() *minify.M {
m := minify.New()
m.Add("text/html", &html.Minifier{
KeepEndTags: true,
KeepQuotes: true,
})
return m
}()

View file

@ -60,7 +60,7 @@ func (f *formatter) FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc
var htmlContentBytes bytes.Buffer
err := md.Convert([]byte(plain), &htmlContentBytes)
if err != nil {
log.Errorf("error formatting plaintext to HTML: %s", err)
log.Errorf(ctx, "error formatting plaintext to HTML: %s", err)
}
result.HTML = htmlContentBytes.String()
@ -68,7 +68,10 @@ func (f *formatter) FromPlain(ctx context.Context, pmf gtsmodel.ParseMentionFunc
result.HTML = SanitizeHTML(result.HTML)
// shrink ray
result.HTML = minifyHTML(result.HTML)
result.HTML, err = m.String("text/html", result.HTML)
if err != nil {
log.Errorf(ctx, "error minifying HTML: %s", err)
}
return result
}

View file

@ -20,11 +20,12 @@ package text
import (
"errors"
"strings"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/util"
"golang.org/x/text/unicode/norm"
"strings"
)
const (
@ -39,13 +40,13 @@ const (
func (r *customRenderer) replaceMention(text string) string {
menchie, err := r.parseMention(r.ctx, text, r.accountID, r.statusID)
if err != nil {
log.Errorf("error parsing mention %s from status: %s", text, err)
log.Errorf(nil, "error parsing mention %s from status: %s", text, err)
return text
}
if r.statusID != "" {
if err := r.f.db.Put(r.ctx, menchie); err != nil {
log.Errorf("error putting mention in db: %s", err)
log.Errorf(nil, "error putting mention in db: %s", err)
return text
}
}
@ -66,7 +67,7 @@ func (r *customRenderer) replaceMention(text string) string {
if menchie.TargetAccount == nil {
a, err := r.f.db.GetAccountByID(r.ctx, menchie.TargetAccountID)
if err != nil {
log.Errorf("error getting account with id %s from the db: %s", menchie.TargetAccountID, err)
log.Errorf(nil, "error getting account with id %s from the db: %s", menchie.TargetAccountID, err)
return text
}
menchie.TargetAccount = a
@ -105,7 +106,7 @@ func (r *customRenderer) replaceHashtag(text string) string {
tag, err := r.f.db.TagStringToTag(r.ctx, normalized, r.accountID)
if err != nil {
log.Errorf("error generating hashtags from status: %s", err)
log.Errorf(nil, "error generating hashtags from status: %s", err)
return text
}
@ -121,7 +122,7 @@ func (r *customRenderer) replaceHashtag(text string) string {
err = r.f.db.Put(r.ctx, tag)
if err != nil {
if !errors.Is(err, db.ErrAlreadyExists) {
log.Errorf("error putting tags in db: %s", err)
log.Errorf(nil, "error putting tags in db: %s", err)
return text
}
}