-
Notifications
You must be signed in to change notification settings - Fork 69
/
engine_loader_hooks.go
155 lines (122 loc) · 4.03 KB
/
engine_loader_hooks.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package core
import (
"context"
"errors"
"fmt"
"github.com/wundergraph/cosmo/router/pkg/metric"
rotel "github.com/wundergraph/cosmo/router/pkg/otel"
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/resolve"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
"slices"
"strings"
)
var (
_ resolve.LoaderHooks = (*EngineLoaderHooks)(nil)
)
const EngineLoaderHooksScopeName = "wundergraph/cosmo/router/engine/loader"
const EngineLoaderHooksScopeVersion = "0.0.1"
// EngineLoaderHooks implements resolve.LoaderHooks
// It is used to trace and measure the performance of the engine loader
type EngineLoaderHooks struct {
tracer trace.Tracer
metricStore metric.Store
}
func NewEngineRequestHooks(metricStore metric.Store) resolve.LoaderHooks {
return &EngineLoaderHooks{
tracer: otel.GetTracerProvider().Tracer(
EngineLoaderHooksScopeName,
trace.WithInstrumentationVersion(EngineLoaderHooksScopeVersion),
),
metricStore: metricStore,
}
}
func (f *EngineLoaderHooks) OnLoad(ctx context.Context, dataSourceID string) context.Context {
if resolve.IsIntrospectionDataSource(dataSourceID) {
return ctx
}
reqContext := getRequestContext(ctx)
if reqContext == nil {
return ctx
}
ctx, span := f.tracer.Start(ctx, "Engine - Fetch")
subgraph := reqContext.SubgraphByID(dataSourceID)
if subgraph != nil {
span.SetAttributes(rotel.WgSubgraphName.String(subgraph.Name))
}
span.SetAttributes(
rotel.WgSubgraphID.String(dataSourceID),
)
return ctx
}
func (f *EngineLoaderHooks) OnFinished(ctx context.Context, statusCode int, dataSourceID string, err error) {
if resolve.IsIntrospectionDataSource(dataSourceID) {
return
}
reqContext := getRequestContext(ctx)
if reqContext == nil {
return
}
span := trace.SpanFromContext(ctx)
defer span.End()
activeSubgraph := reqContext.SubgraphByID(dataSourceID)
baseAttributes := []attribute.KeyValue{
// Subgraph response status code
semconv.HTTPStatusCode(statusCode),
rotel.WgComponentName.String("engine-loader"),
rotel.WgSubgraphID.String(activeSubgraph.Id),
rotel.WgSubgraphName.String(activeSubgraph.Name),
}
// Ensure common attributes are set
baseAttributes = append(baseAttributes, setAttributesFromOperationContext(reqContext.operation)...)
if err != nil {
// Set error status. This is the fetch error from the engine
// Downstream errors are extracted from the subgraph response
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
var errorCodesAttr []string
var subgraphError *resolve.SubgraphError
if errors.As(err, &subgraphError) {
// Extract downstream errors
if len(subgraphError.DownstreamErrors) > 0 {
for i, downstreamError := range subgraphError.DownstreamErrors {
var errorCode string
if downstreamError.Extensions != nil {
if ok := downstreamError.Extensions["code"]; ok != nil {
if code, ok := downstreamError.Extensions["code"].(string); ok {
errorCode = code
}
}
}
if errorCode != "" {
errorCodesAttr = append(errorCodesAttr, errorCode)
span.AddEvent(fmt.Sprintf("Downstream error %d", i+1),
trace.WithAttributes(
rotel.WgSubgraphErrorExtendedCode.String(errorCode),
rotel.WgSubgraphErrorMessage.String(downstreamError.Message),
),
)
}
}
}
}
// Reduce cardinality of error codes
slices.Sort(errorCodesAttr)
if len(errorCodesAttr) > 0 {
// Create individual metrics for each error code
for _, code := range errorCodesAttr {
f.metricStore.MeasureRequestError(ctx,
// Add only the error code as an attribute
append(baseAttributes, rotel.WgSubgraphErrorExtendedCode.String(code))...,
)
}
// Add this after the metrics have been created
// The list might be used for post-processing
baseAttributes = append(baseAttributes, rotel.WgSubgraphErrorExtendedCode.String(strings.Join(errorCodesAttr, ",")))
}
}
span.SetAttributes(baseAttributes...)
}