Skip to content

Commit

Permalink
Use UUIDv4 for request id (#31)
Browse files Browse the repository at this point in the history
As we go with release, IDs are hard to trace:
- They are not unique, restart changes them.
- Collisions kill them
- We should be able to trace down which requests cause problems across
all Quesma.

For this purpose industry uses uuid, let's use UUIDv4.
  • Loading branch information
jakozaur authored May 3, 2024
1 parent 015bf53 commit ecdc96e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion quesma/quesma/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestNoAsciiTableName(t *testing.T) {
assert.Equal(t, fmt.Sprintf(`SELECT * FROM "%s" `, tableName), query.String())
}

var ctx = context.WithValue(context.TODO(), tracing.RequestIdCtxKey, "test")
var ctx = context.WithValue(context.TODO(), tracing.RequestIdCtxKey, tracing.GetRequestId())

const tableName = `logs-generic-default`

Expand Down
4 changes: 2 additions & 2 deletions quesma/quesma/ui/html_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,9 @@ func (qmc *QuesmaManagementConsole) generateReportForRequestId(requestId string)

buffer := newBufferWithHead()
if requestFound {
buffer.Write(generateSimpleTop("Report for request Id " + requestId))
buffer.Write(generateSimpleTop("Report for request UUID " + requestId))
} else {
buffer.Write(generateSimpleTop("Report not found for request Id " + requestId))
buffer.Write(generateSimpleTop("Report not found for request UUID " + requestId))
}

buffer.Html(`<main id="queries">`)
Expand Down
2 changes: 1 addition & 1 deletion quesma/quesma/ui/html_pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestHtmlPages(t *testing.T) {
xss := "<script>alert('xss')</script>"
xssBytes := []byte(xss)
id := "MagicId_123"
id := "b1c4a89e-4905-5e3c-b57f-dc92627d011e"
logChan := make(chan tracing.LogWithLevel, 5)
qmc := NewQuesmaManagementConsole(config.QuesmaConfiguration{}, nil, nil, logChan, telemetry.NewPhoneHomeEmptyAgent())
qmc.PushPrimaryInfo(&QueryDebugPrimarySource{Id: id, QueryResp: xssBytes})
Expand Down
8 changes: 4 additions & 4 deletions quesma/quesma/ui/html_panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func generateQueries(debugKeyValueSlice []DebugKeyValue, withLinks bool) []byte
if withLinks {
buffer.Html(`<a href="/request-Id/`).Text(v.Key).Html(`">`)
}
buffer.Html("<p>RequestID:").Text(v.Key).Html(" Path: ").Text(v.Value.Path).Html("</p>\n")
buffer.Html("<p>UUID:").Text(v.Key).Html(" Path: ").Text(v.Value.Path).Html("</p>\n")
buffer.Html(`<pre Id="query`).Text(v.Key).Html(`">`)
buffer.Text(string(v.Value.IncomingQueryBody))
buffer.Html("\n</pre>")
Expand All @@ -46,7 +46,7 @@ func generateQueries(debugKeyValueSlice []DebugKeyValue, withLinks bool) []byte
buffer.Html(`<a href="/request-Id/`).Text(v.Key).Html(`">`)
}
tookStr := fmt.Sprintf(" took %d ms", v.Value.PrimaryTook.Milliseconds())
buffer.Html("<p>ResponseID:").Text(v.Key).Text(tookStr).Html("</p>\n")
buffer.Html("<p>UUID:").Text(v.Key).Text(tookStr).Html("</p>\n")
buffer.Html(`<pre Id="response`).Text(v.Key).Html(`">`)
if len(v.Value.QueryResp) > 0 {
buffer.Text(string(v.Value.QueryResp))
Expand All @@ -69,7 +69,7 @@ func generateQueries(debugKeyValueSlice []DebugKeyValue, withLinks bool) []byte
buffer.Html(`<a href="/request-Id/`).Text(v.Key).Html(`">`)
}
tookStr := fmt.Sprintf(" took %d ms", v.Value.SecondaryTook.Milliseconds())
buffer.Html("<p>RequestID:").Text(v.Key).Text(tookStr).Html(errorBanner(v.Value)).Html("</p>\n")
buffer.Html("<p>UUID:").Text(v.Key).Text(tookStr).Html(errorBanner(v.Value)).Html("</p>\n")
buffer.Html(`<pre Id="second_query`).Text(v.Key).Html(`">`)
buffer.Text(sqlfmt.SqlPrettyPrint(v.Value.QueryBodyTranslated))
buffer.Html("\n</pre>")
Expand All @@ -87,7 +87,7 @@ func generateQueries(debugKeyValueSlice []DebugKeyValue, withLinks bool) []byte
if withLinks {
buffer.Html(`<a href="/request-Id/`).Text(v.Key).Html(`">`)
}
buffer.Html("<p>ResponseID:").Text(v.Key).Html(errorBanner(v.Value)).Html("</p>\n")
buffer.Html("<p>UUID:").Text(v.Key).Html(errorBanner(v.Value)).Html("</p>\n")
buffer.Html(`<pre Id="second_response`).Text(v.Key).Html(`">`)
if len(v.Value.QueryTranslatedResults) > 0 {
buffer.Text(string(v.Value.QueryTranslatedResults))
Expand Down
2 changes: 1 addition & 1 deletion quesma/quesma/ui/management_console.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
RequestStatisticIngest2Elasticsearch = "ingest2elasticsearch"
)

var requestIdRegex, _ = regexp.Compile(`request_id":"(\d+)"`)
var requestIdRegex, _ = regexp.Compile(logger.RID + `":"([0-9a-fA-F-]+)"`)

type QueryDebugPrimarySource struct {
Id string
Expand Down
7 changes: 2 additions & 5 deletions quesma/tracing/context.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package tracing

import (
"fmt"
"sync/atomic"
"github.com/google/uuid"
)

type ContextKey string
Expand All @@ -15,8 +14,6 @@ const (
TraceEndCtxKey ContextKey = "TraceEnd"
)

var lastRequestId atomic.Int64

func GetRequestId() string {
return fmt.Sprintf("%d", lastRequestId.Add(1))
return uuid.New().String()
}

0 comments on commit ecdc96e

Please sign in to comment.