Skip to content

Commit 9f38fd1

Browse files
authored
Merge pull request #212 from fastly/cee-dub/request-meta
Use FastlyMeta for RequestID and SandboxID
2 parents b38efa4 + 5124f51 commit 9f38fd1

File tree

9 files changed

+66
-53
lines changed

9 files changed

+66
-53
lines changed

_examples/reusable-sessions/fastly.toml renamed to _examples/reusable-sandbox/fastly.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ authors = ["[email protected]"]
55
description = ""
66
language = "go"
77
manifest_version = 2
8-
name = "reusable-sessions"
8+
name = "reusable-sandbox"

_examples/reusable-sessions/main.go renamed to _examples/reusable-sandbox/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ package main
55
import (
66
"context"
77
"fmt"
8-
"os"
98
"time"
109

1110
"github.com/fastly/compute-sdk-go/fsthttp"
1211
)
1312

1413
func main() {
15-
var requests int
14+
var requestCount int
1615
fsthttp.ServeMany(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
17-
requests++
18-
fmt.Fprintf(w, "Request %v, Hello, %s (%q, %q)!\n", requests, r.RemoteAddr, os.Getenv("FASTLY_TRACE_ID"), r.RequestID)
16+
requestCount++
17+
meta, err := r.FastlyMeta()
18+
if err != nil {
19+
fsthttp.Error(w, err.Error(), fsthttp.StatusInternalServerError)
20+
return
21+
}
22+
fmt.Fprintf(w, "Request %v, Hello, %s (sandbox: %q, request: %q)!\n", requestCount, r.RemoteAddr, meta.SandboxID, meta.RequestID)
1923
}, &fsthttp.ServeManyOptions{
2024
NextTimeout: 1 * time.Second,
2125
MaxRequests: 100,

compute/compute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
// of the program.
1717
//
1818
// Because compute guests can run on a variety of different platforms,
19-
// you should not necessarily expect these values to converge across
20-
// different sessions. Instead, we strongly recommend using this value
19+
// you should not necessarily expect these raw values to converge across
20+
// different executions. Instead, we strongly recommend using this value
2121
// to look at the relative cost of various operations in your code base,
2222
// by taking the time before and after a particular operation and then
2323
// dividing this by the total amount of vCPU time your program takes.

end_to_end_tests/serve-many/main.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package main
55
import (
66
"context"
77
"fmt"
8-
"os"
98
"time"
109

1110
"github.com/fastly/compute-sdk-go/fsthttp"
@@ -19,17 +18,22 @@ func main() {
1918
}
2019

2120
handler := func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
22-
if r.Header.Get("Close-Session") == "1" {
21+
if r.Header.Get("Fresh-Sandbox") == "1" {
2322
opts.Continue = func() bool {
2423
return false
2524
}
2625
}
2726

28-
sessionID, requestID := os.Getenv("FASTLY_TRACE_ID"), r.RequestID
29-
fmt.Printf("Session ID: %s, Request ID: %s\n", sessionID, requestID)
27+
meta, err := r.FastlyMeta()
28+
if err != nil {
29+
fsthttp.Error(w, err.Error(), fsthttp.StatusInternalServerError)
30+
return
31+
}
32+
sandboxID, requestID := meta.SandboxID, meta.RequestID
33+
fmt.Printf("Sandbox ID: %s, Request ID: %s\n", sandboxID, requestID)
3034

3135
w.Header().Set("Content-Type", "text/plain")
32-
w.Header().Set("Session-ID", sessionID)
36+
w.Header().Set("Sandbox-ID", sandboxID)
3337
w.Header().Set("Request-ID", requestID)
3438
w.Write([]byte("OK"))
3539
}

end_to_end_tests/serve-many/main_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/fastly/compute-sdk-go/fsthttp"
1010
)
1111

12-
func TestSessionReuse(t *testing.T) {
13-
// First request. Session ID and request ID should match.
12+
func TestSandboxReuse(t *testing.T) {
13+
// First request. Sandbox ID and request ID should match.
1414
req, err := fsthttp.NewRequest("GET", "http://anyplace.horse", nil)
1515
if err != nil {
1616
t.Fatal(err)
@@ -21,44 +21,44 @@ func TestSessionReuse(t *testing.T) {
2121
}
2222
defer resp.Body.Close()
2323

24-
sessionID, requestID := resp.Header.Get("Session-ID"), resp.Header.Get("Request-ID")
24+
sandboxID, requestID := resp.Header.Get("Sandbox-ID"), resp.Header.Get("Request-ID")
2525

26-
if sessionID == "" || requestID == "" {
27-
t.Fatalf("Session-ID and/or Request-ID are empty: %s, %s", sessionID, requestID)
26+
if sandboxID == "" || requestID == "" {
27+
t.Fatalf("Sandbox-ID and/or Request-ID are empty: %s, %s", sandboxID, requestID)
2828
}
29-
if sessionID != requestID {
30-
t.Errorf("sessionID = %s, requestID = %s; expected them to match", sessionID, requestID)
29+
if sandboxID != requestID {
30+
t.Errorf("sandboxID = %s, requestID = %s; expected them to match", sandboxID, requestID)
3131
}
32-
prevSessionID := sessionID
32+
prevSandboxID := sandboxID
3333

34-
// Second request. This should reuse the session, so the session ID
35-
// should match the previous session ID and the request ID should
34+
// Second request. This should reuse the sandbox, so the sandbox ID
35+
// should match the previous sandbox ID and the request ID should
3636
// not match.
3737
//
3838
// We also set a header to tell the server to not allow any more
39-
// requests on this session.
39+
// requests on this sandbox.
4040
req, err = fsthttp.NewRequest("GET", "http://anyplace.horse", nil)
4141
if err != nil {
4242
t.Fatal(err)
4343
}
44-
req.Header.Set("Close-Session", "1")
44+
req.Header.Set("Fresh-Sandbox", "1")
4545
resp, err = req.Send(context.Background(), "self")
4646
if err != nil {
4747
t.Fatal(err)
4848
}
4949
defer resp.Body.Close()
5050

51-
sessionID, requestID = resp.Header.Get("Session-ID"), resp.Header.Get("Request-ID")
51+
sandboxID, requestID = resp.Header.Get("Sandbox-ID"), resp.Header.Get("Request-ID")
5252

53-
if sessionID != prevSessionID {
54-
t.Errorf("sessionID = %s, previous sessionID = %s; expected them to match", sessionID, sessionID)
53+
if sandboxID != prevSandboxID {
54+
t.Errorf("sandboxID = %s, previous sandboxID = %s; expected them to match", sandboxID, sandboxID)
5555
}
56-
if sessionID == requestID {
57-
t.Errorf("sessionID = %s, requestID = %s; expected them to differ", sessionID, requestID)
56+
if sandboxID == requestID {
57+
t.Errorf("sandboxID = %s, requestID = %s; expected them to differ", sandboxID, requestID)
5858
}
59-
prevSessionID = sessionID
59+
prevSandboxID = sandboxID
6060

61-
// Third request, we should have a new session ID and it should match the request ID
61+
// Third request, we should have a new sandbox ID and it should match the request ID
6262
req, err = fsthttp.NewRequest("GET", "http://anyplace.horse", nil)
6363
if err != nil {
6464
t.Fatal(err)
@@ -69,12 +69,12 @@ func TestSessionReuse(t *testing.T) {
6969
}
7070
defer resp.Body.Close()
7171

72-
sessionID, requestID = resp.Header.Get("Session-ID"), resp.Header.Get("Request-ID")
72+
sandboxID, requestID = resp.Header.Get("Sandbox-ID"), resp.Header.Get("Request-ID")
7373

74-
if sessionID == prevSessionID {
75-
t.Errorf("sessionID = %s, previous sessionID = %s; expected them to differ", sessionID, sessionID)
74+
if sandboxID == prevSandboxID {
75+
t.Errorf("sandboxID = %s, previous sandboxID = %s; expected them to differ", sandboxID, sandboxID)
7676
}
77-
if sessionID != requestID {
78-
t.Errorf("sessionID = %s, requestID = %s; expected them to match", sessionID, requestID)
77+
if sandboxID != requestID {
78+
t.Errorf("sandboxID = %s, requestID = %s; expected them to match", sandboxID, requestID)
7979
}
8080
}

fsthttp/backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,10 @@ func (b *BackendOptions) ClientCertificate(certificate string, key secretstore.S
329329

330330
// PoolConnections allows users to turn connection pooling on or off for the
331331
// backend. Pooling allows the Compute platform to reuse connections across
332-
// multiple sessions, resulting in lower resource use at the server (because it
333-
// does not need to reperform the TCP handhsake and TLS authentication when the
332+
// multiple executions, resulting in lower resource use at the server (because it
333+
// does not need to repeat the TCP handhsake and TLS authentication when the
334334
// connection is reused). The default is to pool connections. Set this to false
335-
// to create a new connection to the backend for every incoming session.
335+
// to create a new connection to the backend for every incoming request.
336336
func (b *BackendOptions) PoolConnections(poolingOn bool) *BackendOptions {
337337
b.abiOpts.PoolConnections(poolingOn)
338338
return b

fsthttp/request.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"net/url"
13+
"os"
1314
"strings"
1415
"sync"
1516
"time"
@@ -113,9 +114,6 @@ type Request struct {
113114
// discarded.
114115
ManualFramingMode bool
115116

116-
// RequestID is the current Fastly request ID
117-
RequestID string
118-
119117
// ImageOptimizerOptions control the image optimizer request.
120118
ImageOptimizerOptions *imageopto.Options
121119

@@ -176,11 +174,6 @@ func newClientRequest(abiReq *fastly.HTTPRequest, abiReqBody *fastly.HTTPBody) (
176174
return nil, fmt.Errorf("get protocol version: %w", err)
177175
}
178176

179-
reqID, err := abiReq.DownstreamRequestID()
180-
if err != nil {
181-
return nil, fmt.Errorf("get request id: %w", err)
182-
}
183-
184177
header := NewHeader()
185178
keys := abiReq.GetHeaderNames()
186179
for keys.Next() {
@@ -252,7 +245,6 @@ func newClientRequest(abiReq *fastly.HTTPRequest, abiReqBody *fastly.HTTPBody) (
252245
RemoteAddr: remoteAddr.String(),
253246
ServerAddr: serverAddr.String(),
254247
TLSInfo: tlsInfo,
255-
RequestID: reqID,
256248
downstream: reqAbi{req: abiReq, body: abiReqBody},
257249
}, nil
258250
}
@@ -371,8 +363,15 @@ func (req *Request) FastlyMeta() (*FastlyMeta, error) {
371363
}
372364

373365
var err error
374-
375366
var fastlyMeta FastlyMeta
367+
368+
fastlyMeta.SandboxID = os.Getenv("FASTLY_TRACE_ID")
369+
370+
fastlyMeta.RequestID, err = req.downstream.req.DownstreamRequestID()
371+
if err != nil {
372+
return nil, fmt.Errorf("get request ID: %w", err)
373+
}
374+
376375
fastlyMeta.H2, err = req.downstream.req.DownstreamH2Fingerprint()
377376
if err = ignoreNoneError(err); err != nil {
378377
return nil, fmt.Errorf("get H2 fingerprint: %w", err)
@@ -1076,6 +1075,12 @@ type TLSClientCertificateInfo struct {
10761075

10771076
// FastlyMeta holds various Fastly-specific metadata for a request.
10781077
type FastlyMeta struct {
1078+
// SandboxID is the unique identifier for the sandbox handling the request.
1079+
SandboxID string
1080+
1081+
// RequestID is the unique identifier for the request.
1082+
RequestID string
1083+
10791084
// H2 is the HTTP/2 fingerprint of a client request if available
10801085
H2 []byte
10811086

internal/abi/fastly/compute_runtime_guest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import (
2222
func fastlyGetVCPUMs(prim.Pointer[prim.U64]) FastlyStatus
2323

2424
// Return the number of milliseconds spent on the CPU for the current
25-
// session.
25+
// sandbox.
2626
//
2727
// Because compute guests can run on a variety of different platforms,
28-
// you should not necessarily expect these values to converge across
29-
// different sessions. Instead, we strongly recommend using this value
28+
// you should not necessarily expect these raw values to converge across
29+
// different executions. Instead, we strongly recommend using this value
3030
// to look at the relative cost of various operations in your code base,
3131
// by taking the time before and after a particular operation and then
3232
// dividing this by the total amount of vCPU time your program takes.

secretstore/secretstore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (s *Secret) Handle() *fastly.Secret {
115115
// This function is provided as a way to use data that should be secret,
116116
// but is being obtained by some other means than a Fastly Secret Store.
117117
// Secret values created this way are plaintext only, and are not shared
118-
// with other sessions. This should only be used in situations in which
118+
// via a Store. This should only be used in situations in which
119119
// an API requires a [Secret], but you cannot (for whatever reason) use
120120
// a [Store] to store them.
121121
//

0 commit comments

Comments
 (0)