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/optional"
"github.com/TBD54566975/ftl/backend/controller/dal"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/backend/schema"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/model"
)
type Call struct {
deploymentKey model.DeploymentKey
requestName model.RequestName
startTime time.Time
destVerb *schema.Ref
callers []*schema.Ref
request *ftlv1.CallRequest
response optional.Option[*ftlv1.CallResponse]
callError optional.Option[error]
}
func (s *Service) recordCall(ctx context.Context, call *Call) {
logger := log.FromContext(ctx)
var sourceVerb optional.Option[schema.Ref]
if len(call.callers) > 0 {
sourceVerb = optional.Some(*call.callers[0])
}
var errorStr optional.Option[string]
var stack optional.Option[string]
var responseBody []byte
if callError, ok := call.callError.Get(); ok {
errorStr = optional.Some(callError.Error())
} else if response, ok := call.response.Get(); ok {
responseBody = response.GetBody()
if callError := response.GetError(); callError != nil {
errorStr = optional.Some(callError.Message)
stack = optional.Ptr(callError.Stack)
}
}
err := s.dal.InsertCallEvent(ctx, &dal.CallEvent{
Time: call.startTime,
DeploymentKey: call.deploymentKey,
RequestName: optional.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")
}
}