Skip to content

Commit 65f25f0

Browse files
committed
fix: removed SetContext, modified to RequestLoggingWithFunc (for WebHook)
1 parent 7feabcf commit 65f25f0

File tree

1 file changed

+5
-31
lines changed

1 file changed

+5
-31
lines changed

middleware.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,17 @@ func RequestLoggingWithEcho(config *Config) echo.MiddlewareFunc {
5757
}
5858
}
5959

60-
// SetContext for CloudFunctions
61-
func SetContext(config *Config, w http.ResponseWriter, r *http.Request) (http.ResponseWriter, *http.Request) {
62-
before := time.Now()
63-
64-
traceId := getTraceId(r)
65-
if traceId == "" {
66-
// there is no span yet, so create one
67-
var ctx context.Context
68-
traceId, ctx = generateTraceId(r)
69-
r = r.WithContext(ctx)
70-
}
71-
72-
traces := fmt.Sprintf("projects/%s/traces/%s", config.ProjectId, traceId)
73-
74-
contextLogger := &ContextLogger{
75-
out: config.ContextLogOut,
76-
Trace: traces,
77-
Severity: config.Severity,
78-
AdditionalData: config.AdditionalData,
79-
loggedSeverity: make([]Severity, 0, 10),
80-
Skip: config.Skip,
81-
}
82-
ctx := context.WithValue(r.Context(), contextLoggerKey, contextLogger)
83-
r = r.WithContext(ctx)
60+
// RequestLoggingWithFunc for WebHook
61+
func RequestLoggingWithFunc(config *Config, w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
62+
reserve := NewReserve(config, r)
8463

8564
wrw := &wrappedResponseWriter{ResponseWriter: w}
8665
defer func() {
8766
// logging
88-
elapsed := time.Since(before)
89-
maxSeverity := contextLogger.maxSeverity()
90-
err := writeRequestLog(r, config, wrw.status, wrw.responseSize, elapsed, traces, maxSeverity)
91-
if err != nil {
92-
_, _ = fmt.Fprintln(os.Stderr, err.Error())
93-
}
67+
reserve.LastHandling(wrw)
9468
}()
9569

96-
return wrw, r
70+
next.ServeHTTP(w, reserve.request)
9771
}
9872

9973
type Reserve struct {

0 commit comments

Comments
 (0)