/
context.go
120 lines (98 loc) · 3.81 KB
/
context.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package apmfasthttp // import "go.elastic.co/apm/module/apmfasthttp/v2"
import (
"context"
"net/http"
"strings"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
"go.elastic.co/apm/module/apmhttp/v2"
"go.elastic.co/apm/v2"
)
const txKey = "apmfasthttp_transaction"
func init() {
origTransactionFromContext := apm.OverrideTransactionFromContext
apm.OverrideTransactionFromContext = func(ctx context.Context) *apm.Transaction {
if tx, ok := ctx.Value(txKey).(*txCloser); ok {
return tx.tx
}
return origTransactionFromContext(ctx)
}
origBodyCapturerFromContext := apm.OverrideBodyCapturerFromContext
apm.OverrideBodyCapturerFromContext = func(ctx context.Context) *apm.BodyCapturer {
if tx, ok := ctx.Value(txKey).(*txCloser); ok {
return tx.bc
}
return origBodyCapturerFromContext(ctx)
}
}
func setRequestContext(ctx *fasthttp.RequestCtx, tracer *apm.Tracer, tx *apm.Transaction) (*apm.BodyCapturer, error) {
req := new(http.Request)
if err := fasthttpadaptor.ConvertRequest(ctx, req, true); err != nil {
return nil, err
}
bc := tracer.CaptureHTTPRequestBody(req)
tx.Context.SetHTTPRequest(req)
tx.Context.SetHTTPRequestBody(bc)
return bc, nil
}
func setResponseContext(ctx *fasthttp.RequestCtx, tx *apm.Transaction, bc *apm.BodyCapturer) {
statusCode := ctx.Response.Header.StatusCode()
tx.Result = apmhttp.StatusCodeResult(statusCode)
if !tx.Sampled() {
return
}
headers := make(http.Header)
ctx.Response.Header.VisitAll(func(k, v []byte) {
sk := string(k)
sv := string(v)
headers.Set(sk, sv)
})
tx.Context.SetHTTPResponseHeaders(headers)
tx.Context.SetHTTPStatusCode(statusCode)
}
// StartTransactionWithBody returns a new Transaction with name,
// created with tracer, and taking trace context from ctx.
//
// If the transaction is not ignored, the request and the request body
// capturer will be returned with the transaction added to its context.
func StartTransactionWithBody(
ctx *fasthttp.RequestCtx, tracer *apm.Tracer, name string,
) (*apm.Transaction, *apm.BodyCapturer, error) {
traceContext, ok := getRequestTraceparent(ctx, apmhttp.W3CTraceparentHeader)
if !ok {
traceContext, ok = getRequestTraceparent(ctx, apmhttp.ElasticTraceparentHeader)
}
if ok {
tracestateHeader := string(ctx.Request.Header.Peek(apmhttp.TracestateHeader))
traceContext.State, _ = apmhttp.ParseTracestateHeader(strings.Split(tracestateHeader, ",")...)
}
tx := tracer.StartTransactionOptions(name, "request", apm.TransactionOptions{TraceContext: traceContext})
bc, err := setRequestContext(ctx, tracer, tx)
if err != nil {
tx.End()
return nil, nil, err
}
// NOTE: the fasthttp documentation states that references to RequestCtx
// must not be held after returning from RequestHandler. This is due to
// RequestCtx being pooled, and released after the RequestHandler returns.
// However, it is safe to reference and call RequestCtx in a closer, as the
// closer is invoked before releasing the RequestCtx back to the pool.
ctx.SetUserValue(txKey, newTxCloser(ctx, tx, bc))
return tx, bc, nil
}