-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
logz_test.go
153 lines (114 loc) · 4.17 KB
/
logz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package logz_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"context"
"github.com/glassonion1/logz"
"github.com/glassonion1/logz/internal/config"
"github.com/glassonion1/logz/internal/logger"
"github.com/glassonion1/logz/internal/spancontext"
"github.com/glassonion1/logz/middleware"
"github.com/glassonion1/logz/testhelper"
"github.com/google/go-cmp/cmp"
)
func TestLogzSpanContext(t *testing.T) {
logz.InitTracer()
t.Run("Tests logz integration", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.Handle("/test1", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logz.Infof(ctx, "write %s log", "info")
sc := spancontext.Extract(ctx)
if sc.TraceID == "00000000000000000000000000000000" {
t.Error("trace id is zero value")
}
if sc.SpanID == "0000000000000000" {
t.Error("span id is zero value")
}
}))
mux.Handle("/test2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logz.Infof(ctx, "write %s log", "info")
sc := spancontext.Extract(ctx)
if sc.TraceID == "00000000000000000000000000000000" {
t.Error("trace id is zero value")
}
if sc.SpanID == "0000000000000000" {
t.Error("span id is zero value")
}
// nested function
func(ctx context.Context) {
logz.Infof(ctx, "write %s nested log", "info")
child := spancontext.Extract(ctx)
if child.TraceID != sc.TraceID {
t.Error("trace and child trace id are not equal")
}
if child.SpanID != sc.SpanID {
t.Error("span and child span id are not equal")
}
}(ctx)
}))
mid := middleware.NetHTTP("test/component")(mux)
req1 := httptest.NewRequest(http.MethodGet, "/test1", nil)
rec1 := httptest.NewRecorder()
mid.ServeHTTP(rec1, req1)
req2 := httptest.NewRequest(http.MethodGet, "/test2", nil)
rec2 := httptest.NewRecorder()
mid.ServeHTTP(rec2, req2)
})
}
func TestLogzRemoteParent(t *testing.T) {
logz.InitTracer()
t.Run("Tests logz integration with remote parent", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.Handle("/test1", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logz.Infof(ctx, "write %s log", "info")
sc := spancontext.Extract(ctx)
if diff := cmp.Diff(sc.TraceID, "a0d3eee13de6a4bbcf291eb444b94f28"); diff != "" {
t.Errorf("remote and current trace id are missmatch: %v", diff)
}
if sc.SpanID == "0000000000000000" {
t.Error("span id is zero value")
}
}))
mid := middleware.NetHTTP("test/component")(mux)
req1 := httptest.NewRequest(http.MethodGet, "/test1", nil)
// Simulates managed cloud service like App Engine or Cloud Run, that sets HTTP header of X-Cloud-Trace-Context
req1.Header.Set("X-Cloud-Trace-Context", "a0d3eee13de6a4bbcf291eb444b94f28/1;o=1")
rec1 := httptest.NewRecorder()
mid.ServeHTTP(rec1, req1)
})
}
func TestLogzWriteLog(t *testing.T) {
logz.InitTracer()
config.ProjectID = "test"
logger.NowFunc = func() time.Time {
return time.Date(2020, 12, 31, 23, 59, 59, 999999999, time.UTC)
}
t.Run("Tests logz integration", func(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.Handle("/test1", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
sc := spancontext.Extract(ctx)
got := testhelper.ExtractApplicationLogOut(t, ctx, func(ctx context.Context) {
logz.Infof(ctx, "writes %s log", "info")
})
expected := `{"severity":"INFO","message":"writes info log","time":"2020-12-31T23:59:59.999999999Z","logging.googleapis.com/sourceLocation":{"file":"logz_test.go","line":"134","function":"github.com/glassonion1/logz_test.TestLogzWriteLog.func2.1.1"},"logging.googleapis.com/trace":"projects/test/traces/%s","logging.googleapis.com/spanId":"%s","logging.googleapis.com/trace_sampled":true}`
expected = fmt.Sprintf(expected, sc.TraceID, sc.SpanID)
if diff := cmp.Diff(got, expected); diff != "" {
t.Errorf("failed log info test: %v", diff)
}
}))
mid := middleware.NetHTTP("test/component")(mux)
req1 := httptest.NewRequest(http.MethodGet, "/test1", nil)
rec1 := httptest.NewRecorder()
mid.ServeHTTP(rec1, req1)
})
}