Skip to content

Commit def1303

Browse files
committed
fix: refactoring
1 parent 3642b85 commit def1303

File tree

5 files changed

+74
-22
lines changed

5 files changed

+74
-22
lines changed

router-tests/telemetry/telemetry_test.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9140,7 +9140,51 @@ func TestTelemetryExpressions(t *testing.T) {
91409140
assert.ErrorContains(t, err, "failed to build base mux: line 1, column 17: type expr.SubgraphRequest has no field clientTrace2")
91419141
})
91429142

9143-
t.Run("verify expression is correctly evaluated", func(t *testing.T) {
9143+
t.Run("verify name and id expressions", func(t *testing.T) {
9144+
t.Parallel()
9145+
metricReader := metric.NewManualReader()
9146+
exporter := tracetest.NewInMemoryExporter(t)
9147+
9148+
testenv.Run(t, &testenv.Config{
9149+
TraceExporter: exporter,
9150+
MetricReader: metricReader,
9151+
SubgraphTracingOptions: &core.SubgraphTracingOptions{
9152+
ExpressionAttributes: []core.ExpressionAttribute{
9153+
{
9154+
Key: "sg_name",
9155+
Expression: "subgraph.name",
9156+
},
9157+
{
9158+
Key: "sg_id",
9159+
Expression: "subgraph.id",
9160+
},
9161+
},
9162+
},
9163+
}, func(t *testing.T, xEnv *testenv.Environment) {
9164+
xEnv.MakeGraphQLRequestOK(testenv.GraphQLRequest{
9165+
Query: `query employees { employees { id details { forename surname } notes } }`,
9166+
Header: map[string][]string{"service-name": {"service-name"}},
9167+
})
9168+
9169+
sn := exporter.GetSpans().Snapshots()
9170+
engineFetchSpan := sn[6]
9171+
require.Equal(t, "Engine - Fetch", engineFetchSpan.Name())
9172+
require.Equal(t, trace.SpanKindInternal, engineFetchSpan.SpanKind())
9173+
9174+
attributes := engineFetchSpan.Attributes()
9175+
exprAttributes := attributes[14:]
9176+
9177+
require.Len(t, exprAttributes, 2)
9178+
9179+
sgName := findAttr(exprAttributes, "sg_name")
9180+
require.Equal(t, "employees", sgName.Value.AsString())
9181+
9182+
sgId := findAttr(exprAttributes, "sg_id")
9183+
require.Equal(t, "0", sgId.Value.AsString())
9184+
})
9185+
})
9186+
9187+
t.Run("verify available clientTrace expression are correctly evaluated", func(t *testing.T) {
91449188
t.Parallel()
91459189
metricReader := metric.NewManualReader()
91469190
exporter := tracetest.NewInMemoryExporter(t)
@@ -9260,7 +9304,7 @@ func TestTelemetryExpressions(t *testing.T) {
92609304
ExpressionAttributes: []core.ExpressionAttribute{
92619305
{
92629306
Key: "tls_start",
9263-
Expression: "subgraph.operation.trace.tlsStart?.time",
9307+
Expression: "subgraph.request.clientTrace.tlsStart?.time",
92649308
},
92659309
},
92669310
},
@@ -9276,9 +9320,10 @@ func TestTelemetryExpressions(t *testing.T) {
92769320
require.Equal(t, trace.SpanKindInternal, engineFetchSpan.SpanKind())
92779321

92789322
attributes := engineFetchSpan.Attributes()
9279-
exprAttributes := attributes[0:]
92809323

9281-
attr := findAttr(exprAttributes, "tls_start")
9324+
require.Len(t, attributes[14:], 0)
9325+
9326+
attr := findAttr(attributes, "tls_start")
92829327
require.False(t, attr.Valid())
92839328
})
92849329
})
@@ -9295,7 +9340,7 @@ func TestTelemetryExpressions(t *testing.T) {
92959340
ExpressionAttributes: []core.ExpressionAttribute{
92969341
{
92979342
Key: "wrote_headers",
9298-
Expression: "subgraph.operation.trace.wroteHeaders?.time",
9343+
Expression: "subgraph.request.clientTrace.wroteHeaders?.time",
92999344
},
93009345
},
93019346
},

router/core/access_log_field_handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ func TestAccessLogsFieldHandler(t *testing.T) {
122122
)
123123

124124
expressionResponse := response[1]
125-
require.IsType(t, &ExprWrapError{}, expressionResponse.Interface)
125+
require.IsType(t, &expr.WrapError{}, expressionResponse.Interface)
126126
require.Equal(t, expressionResponseKey, expressionResponse.Key)
127-
require.Equal(t, &ExprWrapError{requestError}, expressionResponse.Interface)
127+
require.Equal(t, &expr.WrapError{requestError}, expressionResponse.Interface)
128128
})
129129

130130
}

router/pkg/config/fixtures/full.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ compliance:
8686
telemetry:
8787
# Common options
8888
service_name: "cosmo-router"
89+
subgraph:
90+
attributes:
91+
- key: "wg.tracing.custom.conn.subgraph.hostport"
92+
value_from:
93+
expression: "subgraph.request.clientTrace.connCreate.hostPort"
8994

9095
# If no exporter is specified it uses https://cosmo-otel.wundergraph.com for tracing and metrics
9196

@@ -115,10 +120,6 @@ telemetry:
115120
export_timeout: 30s
116121
path: "/v1/traces"
117122
headers: {}
118-
subgraph:
119-
attributes:
120-
- key: "wg.tracing.custom.conn.subgraph.hostport"
121-
expression: "subgraph.operation.trace.conn.create.hostPort"
122123

123124
# OpenTelemetry Metrics
124125
metrics:

router/pkg/config/testdata/config_defaults.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"Telemetry": {
99
"ServiceName": "cosmo-router",
1010
"Attributes": null,
11+
"Subgraph": {
12+
"Attributes": null
13+
},
1114
"ResourceAttributes": null,
1215
"Tracing": {
1316
"Enabled": true,
@@ -25,9 +28,6 @@
2528
"Enabled": false,
2629
"HeaderName": "x-wg-trace-id"
2730
},
28-
"Subgraph": {
29-
"Attributes": null
30-
},
3131
"ExportGraphQLVariables": false,
3232
"WithNewRoot": false
3333
},

router/pkg/config/testdata/config_full.json

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88
"Telemetry": {
99
"ServiceName": "cosmo-router",
1010
"Attributes": null,
11+
"Subgraph": {
12+
"Attributes": [
13+
{
14+
"Key": "wg.tracing.custom.conn.subgraph.hostport",
15+
"Default": "",
16+
"ValueFrom": {
17+
"RequestHeader": "",
18+
"ContextField": "",
19+
"ResponseHeader": "",
20+
"Expression": "subgraph.request.clientTrace.connCreate.hostPort"
21+
}
22+
}
23+
]
24+
},
1125
"ResourceAttributes": null,
1226
"Tracing": {
1327
"Enabled": true,
@@ -35,14 +49,6 @@
3549
"Enabled": false,
3650
"HeaderName": "x-wg-trace-id"
3751
},
38-
"Subgraph": {
39-
"Attributes": [
40-
{
41-
"Key": "wg.tracing.custom.conn.subgraph.hostport",
42-
"Expression": "subgraph.operation.trace.conn.create.hostPort"
43-
}
44-
]
45-
},
4652
"ExportGraphQLVariables": true,
4753
"WithNewRoot": false
4854
},

0 commit comments

Comments
 (0)