-
Notifications
You must be signed in to change notification settings - Fork 27
allow configuration of the metrics server opts #240
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
+103
−2
Merged
Changes from 1 commit
Commits
Show all changes
2 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
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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import ( | |
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| grpcprometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" | ||
| "github.com/prometheus/client_golang/prometheus" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
|
|
@@ -303,6 +304,90 @@ func TestMetricsServer_WithDefaultRegistryAndDefaultPort(t *testing.T) { | |
| // Wait for server to start | ||
| time.Sleep(3 * time.Second) | ||
|
|
||
| t.Run("MetricsServerTest On DefaultPort With DefaultRegistry", func(t *testing.T) { | ||
| // Test gRPC connection | ||
| conn, err := grpc.NewClient(fmt.Sprintf("localhost:%d", grpcPort), | ||
| grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
| if err != nil { | ||
| t.Fatalf("Failed to connect: %v", err) | ||
| } | ||
| defer conn.Close() | ||
|
|
||
| client := v1.NewFunctionRunnerServiceClient(conn) | ||
|
|
||
| // Make the request | ||
| req := &v1.RunFunctionRequest{ | ||
| Meta: &v1.RequestMeta{Tag: "default-metrics-test"}, | ||
| } | ||
|
|
||
| _, err = client.RunFunction(context.Background(), req) | ||
| if err != nil { | ||
| t.Errorf("Request failed: %v", err) | ||
| } | ||
|
|
||
| // Wait for metrics to be collected | ||
| time.Sleep(2 * time.Second) | ||
|
|
||
| // Verify metrics endpoint is accessible | ||
| metricsURL := fmt.Sprintf("http://localhost:%d/metrics", metricsPort) | ||
| httpReq, err := http.NewRequestWithContext(context.Background(), http.MethodGet, metricsURL, nil) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create request: %v", err) | ||
| } | ||
| resp, err := http.DefaultClient.Do(httpReq) | ||
| if err != nil { | ||
| t.Fatalf("Failed to get metrics: %v", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| t.Fatalf("Failed to read metrics: %v", err) | ||
| } | ||
|
|
||
| metricsContent := string(body) | ||
|
|
||
| // Verify metrics are present | ||
| if !strings.Contains(metricsContent, "# HELP") { | ||
| t.Error("Expected Prometheus format") | ||
| } | ||
|
|
||
| // Verify gRPC metrics are present | ||
| if !strings.Contains(metricsContent, "grpc_server_started_total") { | ||
| t.Error("Expected grpc_server_started_total metric to be present") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // TestMetricsServer_WithCustomMetricsServerOpts verifies that metrics server uses custom metrics server opts. | ||
| func TestMetricsServer_WithCustomMetricsServerOpts(t *testing.T) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we'll probably want to refactor these tests at some point to reduce the duplication, but it's fine for now 🤓 |
||
| // Create mock server | ||
| mockServer := &MockFunctionServer{ | ||
| rsp: &v1.RunFunctionResponse{ | ||
| Meta: &v1.ResponseMeta{Tag: "default-metrics-test"}, | ||
| }, | ||
| } | ||
|
|
||
| // Get ports | ||
| grpcPort := getAvailablePort(t) | ||
| // Should use default metrics port 8080 | ||
| metricsPort := 8080 | ||
|
|
||
| serverDone := make(chan error, 1) | ||
| go func() { | ||
| err := Serve(mockServer, | ||
| Listen("tcp", fmt.Sprintf(":%d", grpcPort)), | ||
| Insecure(true), | ||
| WithMetricsServerOpts( | ||
| grpcprometheus.WithServerHandlingTimeHistogram(), | ||
| ), | ||
| ) | ||
| serverDone <- err | ||
| }() | ||
|
|
||
| // Wait for server to start | ||
| time.Sleep(3 * time.Second) | ||
|
|
||
| t.Run("MetricsServerTest On DefaultPort With DefaultRegisrty", func(t *testing.T) { | ||
| // Test gRPC connection | ||
| conn, err := grpc.NewClient(fmt.Sprintf("localhost:%d", grpcPort), | ||
|
|
@@ -355,6 +440,11 @@ func TestMetricsServer_WithDefaultRegistryAndDefaultPort(t *testing.T) { | |
| if !strings.Contains(metricsContent, "grpc_server_started_total") { | ||
| t.Error("Expected grpc_server_started_total metric to be present") | ||
| } | ||
|
|
||
| // Verify gRPC Histogram metrics are present | ||
| if !strings.Contains(metricsContent, "grpc_server_handling_seconds_bucket") { | ||
| t.Error("Expected grpc_server_handling_seconds_bucket metric to be present") | ||
| } | ||
| }) | ||
| } | ||
|
|
||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default port test is flaky (port 8080 contention) and relies on sleeps.
Two concerns:
Recommend either (a) preflight-check 8080 and skip if busy, plus readiness polling, or (b) use a random free port via WithMetricsServer while keeping a single default-port test elsewhere.
Minimal changes to improve stability:
Nit: s/DefaultRegisrty/DefaultRegistry in the subtest name.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern exists before this PR, so I wouldn't force us to change it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbw976, understood! You're right that this is pre-existing behavior, and it makes sense not to expand the scope of this PR to address it.
If you'd like, I can open a follow-up issue to track improving the test stability (handling port contention and replacing fixed sleeps with readiness polling) for future work. Just let me know!
🧠 Learnings used