-
Notifications
You must be signed in to change notification settings - Fork 233
feat: expose query operation timings #2491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
StarpTech
merged 4 commits into
wundergraph:main
from
AlenaSviridenko:asviridenko-expose-operation-timings
Feb 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package custom_operation_timings | ||
|
|
||
| import ( | ||
| "net/http" | ||
|
|
||
| "github.com/wundergraph/cosmo/router/core" | ||
| ) | ||
|
|
||
| const myModuleID = "operationTimingsModule" | ||
|
|
||
| // OperationTimingsModule is a simple module that reads and logs the operation timings | ||
| type OperationTimingsModule struct { | ||
| ResultsChan chan core.OperationTimings | ||
| } | ||
|
|
||
| func (m *OperationTimingsModule) Middleware(ctx core.RequestContext, next http.Handler) { | ||
| timings := ctx.Operation().Timings() | ||
|
|
||
| if m.ResultsChan != nil { | ||
| select { | ||
| case m.ResultsChan <- timings: | ||
| default: | ||
| // drop if nobody is listening to avoid blocking the request path | ||
| } | ||
| } | ||
|
|
||
| // Call the next handler in the chain or return early by calling w.Write() | ||
| next.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) | ||
| } | ||
|
|
||
| func (m *OperationTimingsModule) Module() core.ModuleInfo { | ||
| return core.ModuleInfo{ | ||
| // This is the ID of your module, it must be unique | ||
| ID: myModuleID, | ||
| // The priority of your module, lower numbers are executed first | ||
| Priority: 1, | ||
| New: func() core.Module { | ||
| return &OperationTimingsModule{ | ||
| ResultsChan: make(chan core.OperationTimings, 1), | ||
| } | ||
| }, | ||
|
AlenaSviridenko marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| // Interface guard | ||
| var ( | ||
| _ core.RouterMiddlewareHandler = (*OperationTimingsModule)(nil) | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| package module_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| operationTimingsModule "github.com/wundergraph/cosmo/router-tests/modules/custom-operation-timings" | ||
| "github.com/wundergraph/cosmo/router-tests/testenv" | ||
| "github.com/wundergraph/cosmo/router/cmd/custom/module" | ||
| "github.com/wundergraph/cosmo/router/core" | ||
| "github.com/wundergraph/cosmo/router/pkg/config" | ||
| ) | ||
|
|
||
| func TestCustomModuleOperationTimings(t *testing.T) { | ||
| t.Run("gets the correct timings for a simple query", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| resultsChan := make(chan core.OperationTimings, 1) | ||
|
|
||
| cfg := config.Config{ | ||
| Graph: config.Graph{}, | ||
| Modules: map[string]any{ | ||
| "myModule": module.MyModule{ | ||
| Value: 1, | ||
| }, | ||
| "operationTimingsModule": operationTimingsModule.OperationTimingsModule{ | ||
| ResultsChan: resultsChan, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| RouterOptions: []core.Option{ | ||
| core.WithModulesConfig(cfg.Modules), | ||
| core.WithCustomModules(&module.MyModule{}, &operationTimingsModule.OperationTimingsModule{}), | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res, err := xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ | ||
| Query: `query MyQuery { employee(id: 1) { id currentMood } }`, | ||
| OperationName: json.RawMessage(`"MyQuery"`), | ||
| }) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 200, res.Response.StatusCode) | ||
|
|
||
| testenv.AwaitChannelWithT(t, 10*time.Second, resultsChan, func(t *testing.T, timings core.OperationTimings) { | ||
| // Verify that timings are populated (they should be non-zero for a real query) | ||
| // We don't check exact values as they depend on the machine, but we verify they are set | ||
| assert.GreaterOrEqual(t, timings.ParsingTime, time.Duration(0), "ParsingTime should be non-negative") | ||
| assert.GreaterOrEqual(t, timings.ValidationTime, time.Duration(0), "ValidationTime should be non-negative") | ||
| assert.GreaterOrEqual(t, timings.PlanningTime, time.Duration(0), "PlanningTime should be non-negative") | ||
| assert.GreaterOrEqual(t, timings.NormalizationTime, time.Duration(0), "NormalizationTime should be non-negative") | ||
|
|
||
| // At least one timing should be non-zero to verify timings are actually being captured | ||
| totalTime := timings.ParsingTime + timings.ValidationTime + timings.PlanningTime + timings.NormalizationTime | ||
| assert.Greater(t, totalTime, time.Duration(0), "At least one timing should be non-zero") | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| t.Run("gets the correct timings for a complex query", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| resultsChan := make(chan core.OperationTimings, 1) | ||
|
|
||
| cfg := config.Config{ | ||
| Graph: config.Graph{}, | ||
| Modules: map[string]any{ | ||
| "myModule": module.MyModule{ | ||
| Value: 1, | ||
| }, | ||
| "operationTimingsModule": operationTimingsModule.OperationTimingsModule{ | ||
| ResultsChan: resultsChan, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| testenv.Run(t, &testenv.Config{ | ||
| RouterOptions: []core.Option{ | ||
| core.WithModulesConfig(cfg.Modules), | ||
| core.WithCustomModules(&module.MyModule{}, &operationTimingsModule.OperationTimingsModule{}), | ||
| }, | ||
| }, func(t *testing.T, xEnv *testenv.Environment) { | ||
| res, err := xEnv.MakeGraphQLRequest(testenv.GraphQLRequest{ | ||
| Query: ` | ||
| query Employees { | ||
| employees { | ||
| id | ||
| tag | ||
| notes | ||
| updatedAt | ||
| currentMood | ||
| derivedMood | ||
| isAvailable | ||
| products | ||
| details { | ||
| forename | ||
| surname | ||
| middlename | ||
| hasChildren | ||
| maritalStatus | ||
| nationality | ||
| } | ||
| role { | ||
| departments | ||
| title | ||
| } | ||
| } | ||
| }`, | ||
| OperationName: json.RawMessage(`"Employees"`), | ||
| }) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, 200, res.Response.StatusCode) | ||
|
|
||
| testenv.AwaitChannelWithT(t, 10*time.Second, resultsChan, func(t *testing.T, timings core.OperationTimings) { | ||
| // Verify that timings are populated | ||
| assert.GreaterOrEqual(t, timings.ParsingTime, time.Duration(0), "ParsingTime should be non-negative") | ||
| assert.GreaterOrEqual(t, timings.ValidationTime, time.Duration(0), "ValidationTime should be non-negative") | ||
| assert.GreaterOrEqual(t, timings.PlanningTime, time.Duration(0), "PlanningTime should be non-negative") | ||
| assert.GreaterOrEqual(t, timings.NormalizationTime, time.Duration(0), "NormalizationTime should be non-negative") | ||
|
|
||
| // At least one timing should be non-zero to verify timings are actually being captured | ||
| totalTime := timings.ParsingTime + timings.ValidationTime + timings.PlanningTime + timings.NormalizationTime | ||
| assert.Greater(t, totalTime, time.Duration(0), "At least one timing should be non-zero") | ||
| }) | ||
| }) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.