Skip to content

Commit

Permalink
Add an octx package (#246)
Browse files Browse the repository at this point in the history
- It houses context keys used by multiple ong packages.
  • Loading branch information
komuw authored Mar 6, 2023
1 parent bc5bc10 commit 1e13131
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Most recent version is listed first.

## v0.0.44
- Organise imports: https://github.com/komuw/ong/pull/245
- Create an internal/octx that houses context keys used by multiple ong packages: https://github.com/komuw/ong/pull/246

## v0.0.43
- Add precision to ratelimiting: https://github.com/komuw/ong/pull/239
Expand Down
3 changes: 2 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import (
"syscall"
"time"

"github.com/komuw/ong/internal/octx"
"github.com/komuw/ong/log"

"golang.org/x/exp/slog"
)

const (
logIDHeader = string(log.CtxKey)
logIDHeader = string(octx.LogCtxKey)
errPrefix = "ong/client:"
)

Expand Down
1 change: 1 addition & 0 deletions internal/clientip/client_ip.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package clientip provides(in a best effort manner) client IP addresses.
package clientip

import (
Expand Down
8 changes: 8 additions & 0 deletions internal/octx/octx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Package octx houses context keys used by multiple ong packages.
package octx

type logContextKeyType string

// LogCtxKey is the name of the context key used to store the logID.
// It is used primarily by `ong/log`, `ong/client` and `ong/middleware` packages
const LogCtxKey = logContextKeyType("Ong-logID")
10 changes: 3 additions & 7 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ import (

"github.com/komuw/ong/errors"
"github.com/komuw/ong/id"
"github.com/komuw/ong/internal/octx"

"golang.org/x/exp/slog"
)

type logContextKeyType string

const (
// CtxKey is the name of the context key used to store the logID.
CtxKey = logContextKeyType("Ong-logID")

// ImmediateLevel is the severity which if a log event has, it is logged immediately without buffering.
LevelImmediate = slog.Level(-6142973)

Expand All @@ -36,7 +32,7 @@ func GetId(ctx context.Context) string {
// It returns the logID and true if the id came from ctx else false
func getId(ctx context.Context) (string, bool) {
if ctx != nil {
if vCtx := ctx.Value(CtxKey); vCtx != nil {
if vCtx := ctx.Value(octx.LogCtxKey); vCtx != nil {
if s, ok := vCtx.(string); ok {
return s, true
}
Expand Down Expand Up @@ -144,7 +140,7 @@ func (h handler) Handle(ctx context.Context, r slog.Record) error {
// if ctx did not contain a logId, do not use the generated one.
id = id2
}
ctx = context.WithValue(ctx, CtxKey, id)
ctx = context.WithValue(ctx, octx.LogCtxKey, id)

newAttrs := []slog.Attr{
{Key: "logID", Value: slog.StringValue(id)},
Expand Down
5 changes: 3 additions & 2 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

ongErrors "github.com/komuw/ong/errors"
"github.com/komuw/ong/internal/octx"

"github.com/akshayjshah/attest"
"go.uber.org/goleak"
Expand Down Expand Up @@ -280,7 +281,7 @@ func TestLogger(t *testing.T) {
attest.Subsequence(t, w.String(), msg)

newId := "NEW-id-adh4e92427dajd"
ctx = context.WithValue(ctx, CtxKey, newId)
ctx = context.WithValue(ctx, octx.LogCtxKey, newId)
l.ErrorCtx(ctx, "hey2", errors.New("badTingOne"))
attest.Subsequence(t, w.String(), newId)
})
Expand All @@ -306,7 +307,7 @@ func TestLogger(t *testing.T) {
stdLogger := slog.NewLogLogger(l.Handler(), LevelImmediate)
stdLogger.Println(msg)
attest.Subsequence(t, w.String(), msg)
attest.Subsequence(t, w.String(), "log/log_test.go:307")
attest.Subsequence(t, w.String(), "log/log_test.go:308")
attest.True(t, LevelImmediate < 0) // otherwise it will trigger `log.handler` to flush all logs, which we dont want.
}
})
Expand Down
5 changes: 3 additions & 2 deletions middleware/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"time"

"github.com/komuw/ong/cookie"
"github.com/komuw/ong/internal/octx"
"github.com/komuw/ong/log"
)

const logIDKey = string(log.CtxKey)
const logIDKey = string(octx.LogCtxKey)

// trace is a middleware that adds logID to request and response.
func trace(wrappedHandler http.HandlerFunc, domain string) http.HandlerFunc {
Expand All @@ -21,7 +22,7 @@ func trace(wrappedHandler http.HandlerFunc, domain string) http.HandlerFunc {
ctx = context.WithValue(
ctx,
// using this custom key is important, instead of using `logIDKey`
log.CtxKey,
octx.LogCtxKey,
logID,
)
r = r.WithContext(ctx)
Expand Down
6 changes: 3 additions & 3 deletions middleware/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/komuw/ong/id"
"github.com/komuw/ong/log"
"github.com/komuw/ong/internal/octx"

"github.com/akshayjshah/attest"
)
Expand Down Expand Up @@ -261,7 +261,7 @@ func TestGetLogId(t *testing.T) {

{
expected := "expected-two"
ctx := context.WithValue(context.Background(), log.CtxKey, expected)
ctx := context.WithValue(context.Background(), octx.LogCtxKey, expected)
req := httptest.NewRequest(http.MethodHead, "/someUri", nil)
req = req.WithContext(ctx)
id := getLogId(req)
Expand All @@ -280,7 +280,7 @@ func TestGetLogId(t *testing.T) {
req.Header.Add(logIDKey, expected)

req = req.WithContext(
context.WithValue(context.Background(), log.CtxKey, "context-logID"),
context.WithValue(context.Background(), octx.LogCtxKey, "context-logID"),
)

id := getLogId(req)
Expand Down

0 comments on commit 1e13131

Please sign in to comment.