generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
observability.go
63 lines (55 loc) · 1.73 KB
/
observability.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
package controller
import (
"context"
"time"
"github.com/alecthomas/types"
"github.com/TBD54566975/ftl/backend/common/log"
"github.com/TBD54566975/ftl/backend/common/model"
"github.com/TBD54566975/ftl/backend/controller/dal"
"github.com/TBD54566975/ftl/backend/schema"
ftlv1 "github.com/TBD54566975/ftl/protos/xyz/block/ftl/v1"
)
type Call struct {
deploymentName model.DeploymentName
requestName model.RequestName
startTime time.Time
destVerb *schema.VerbRef
callers []*schema.VerbRef
request *ftlv1.CallRequest
response types.Option[*ftlv1.CallResponse]
callError types.Option[error]
}
func (s *Service) recordCall(ctx context.Context, call *Call) {
logger := log.FromContext(ctx)
var sourceVerb types.Option[schema.VerbRef]
if len(call.callers) > 0 {
sourceVerb = types.Some(*call.callers[0])
}
var errorStr types.Option[string]
var stack types.Option[string]
var responseBody []byte
if callError, ok := call.callError.Get(); ok {
errorStr = types.Some(callError.Error())
} else if response, ok := call.response.Get(); ok {
responseBody = response.GetBody()
if callError := response.GetError(); callError != nil {
errorStr = types.Some(callError.Message)
stack = types.Ptr(callError.Stack)
}
}
err := s.dal.InsertCallEvent(ctx, &dal.CallEvent{
Time: call.startTime,
DeploymentName: call.deploymentName,
RequestName: types.Some(call.requestName),
Duration: time.Since(call.startTime),
SourceVerb: sourceVerb,
DestVerb: *call.destVerb,
Request: call.request.GetBody(),
Response: responseBody,
Error: errorStr,
Stack: stack,
})
if err != nil {
logger.Errorf(err, "failed to record call")
}
}