-
Notifications
You must be signed in to change notification settings - Fork 33
feat: router Prometheus /metrics endpoint (JEP-0013 Phase 2) #933
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
Open
RoddieKieley
wants to merge
6
commits into
jumpstarter-dev:main
Choose a base branch
from
RoddieKieley:jep-0013-phase2-router-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c778c08
feat: router Prometheus /metrics endpoint (JEP-0013 Phase 2)
RoddieKieley 216b287
fix: evaluate and address coderabbitai feedback.
RoddieKieley 3cbf07f
fix: evaluate and address human feedback.
RoddieKieley a442040
fix: reject undocumented jumpstarter_* router metrics.
RoddieKieley ec9bf58
fix: pass tlsSecretHash into createRouterDeployment bind test.
RoddieKieley e9f3036
fix: build the full router package in docker-build-ci.
RoddieKieley 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
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| Copyright 2026. The Jumpstarter Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
|
RoddieKieley marked this conversation as resolved.
|
||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| ) | ||
|
|
||
| // startMetricsServer starts an HTTP server exposing GET /metrics. | ||
| // addr "0" or empty disables the server and returns ("", nil, nil). | ||
| // addr ending with ":0" binds an ephemeral port; the returned listen address | ||
| // is host:port suitable for http.Get. | ||
| // The returned shutdown func gracefully stops the server (nil when disabled). | ||
| func startMetricsServer(addr string) (string, func(context.Context) error, error) { | ||
| if addr == "" || addr == "0" { | ||
| return "", nil, nil | ||
| } | ||
|
|
||
| ln, err := net.Listen("tcp", addr) | ||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| mux := http.NewServeMux() | ||
| mux.Handle("/metrics", promhttp.Handler()) | ||
|
|
||
| srv := &http.Server{ | ||
| Handler: mux, | ||
| ReadHeaderTimeout: 10 * time.Second, | ||
| ReadTimeout: 30 * time.Second, | ||
| WriteTimeout: 30 * time.Second, | ||
| // IdleTimeout is generous so Prometheus scrape keepalives survive | ||
| // typical scrape intervals without churning connections. | ||
| IdleTimeout: 5 * time.Minute, | ||
| } | ||
| go func() { | ||
| if err := srv.Serve(ln); err != nil && err != http.ErrServerClosed { | ||
| ctrl.Log.WithName("metrics").Error(err, "metrics server stopped unexpectedly") | ||
| } | ||
| }() | ||
|
RoddieKieley marked this conversation as resolved.
|
||
|
|
||
| return ln.Addr().String(), srv.Shutdown, 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,161 @@ | ||
| /* | ||
| Copyright 2026. The Jumpstarter Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| dto "github.com/prometheus/client_model/go" | ||
| "github.com/prometheus/common/expfmt" | ||
| ) | ||
|
|
||
| func TestMetricsEndpointServesPrometheusText(t *testing.T) { | ||
| addr, shutdown, err := startMetricsServer("127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatalf("startMetricsServer: %v", err) | ||
| } | ||
| if addr == "" { | ||
| t.Fatal("expected non-empty listen address") | ||
| } | ||
| if shutdown == nil { | ||
| t.Fatal("expected non-nil shutdown func when server is enabled") | ||
| } | ||
| t.Cleanup(func() { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
| defer cancel() | ||
| _ = shutdown(ctx) | ||
| }) | ||
|
|
||
| client := &http.Client{Timeout: 2 * time.Second} | ||
| var resp *http.Response | ||
| var lastErr error | ||
| for i := 0; i < 20; i++ { | ||
| resp, lastErr = client.Get("http://" + addr + "/metrics") | ||
| if lastErr == nil { | ||
| break | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
| } | ||
| if lastErr != nil { | ||
| t.Fatalf("GET /metrics: %v", lastErr) | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| t.Fatalf("status = %d, want 200", resp.StatusCode) | ||
| } | ||
| ct := resp.Header.Get("Content-Type") | ||
| if !strings.Contains(ct, "text/plain") && !strings.Contains(ct, "openmetrics") { | ||
| t.Fatalf("unexpected Content-Type %q", ct) | ||
| } | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| t.Fatalf("read body: %v", err) | ||
| } | ||
|
|
||
| // Validate Prometheus exposition contract (not substring heuristics). | ||
| var parser expfmt.TextParser | ||
| families, err := parser.TextToMetricFamilies(strings.NewReader(string(body))) | ||
| if err != nil { | ||
| t.Fatalf("parse Prometheus exposition: %v\nbody:\n%s", err, body) | ||
| } | ||
| if len(families) == 0 { | ||
| t.Fatal("expected at least one metric family from default promhttp handler") | ||
| } | ||
|
|
||
| // Default Go process metrics should appear; reject undocumented jumpstarter_* series. | ||
| hasGo, hasProcess := false, false | ||
| for name := range families { | ||
| if strings.HasPrefix(name, "jumpstarter_") { | ||
| t.Fatalf("unexpected undocumented metric family %q; got families: %v", name, familyNames(families)) | ||
| } | ||
| switch { | ||
| case strings.HasPrefix(name, "go_"): | ||
| hasGo = true | ||
| case strings.HasPrefix(name, "process_"): | ||
| hasProcess = true | ||
| } | ||
| } | ||
| if !hasGo { | ||
| t.Fatalf("expected go_* metric family, got families: %v", familyNames(families)) | ||
| } | ||
| if !hasProcess { | ||
| t.Fatalf("expected process_* metric family, got families: %v", familyNames(families)) | ||
| } | ||
| } | ||
|
|
||
| func familyNames(families map[string]*dto.MetricFamily) []string { | ||
| names := make([]string, 0, len(families)) | ||
| for name := range families { | ||
| names = append(names, name) | ||
| } | ||
| return names | ||
| } | ||
|
|
||
| func TestMetricsServerDisabledWhenAddrZero(t *testing.T) { | ||
| addr, shutdown, err := startMetricsServer("0") | ||
| if err != nil { | ||
| t.Fatalf("startMetricsServer(0): %v", err) | ||
| } | ||
| if addr != "" { | ||
| t.Fatalf("expected empty addr when disabled, got %q", addr) | ||
| } | ||
| if shutdown != nil { | ||
| t.Fatal("expected nil shutdown func when server is disabled") | ||
| } | ||
| } | ||
|
|
||
| func TestMetricsServerShutdown(t *testing.T) { | ||
| addr, shutdown, err := startMetricsServer("127.0.0.1:0") | ||
| if err != nil { | ||
| t.Fatalf("startMetricsServer: %v", err) | ||
| } | ||
| if shutdown == nil { | ||
| t.Fatal("expected non-nil shutdown func") | ||
| } | ||
|
|
||
| client := &http.Client{Timeout: 2 * time.Second} | ||
| var lastErr error | ||
| for i := 0; i < 20; i++ { | ||
| var resp *http.Response | ||
| resp, lastErr = client.Get("http://" + addr + "/metrics") | ||
| if lastErr == nil { | ||
| _ = resp.Body.Close() | ||
| break | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
| } | ||
| if lastErr != nil { | ||
| t.Fatalf("GET /metrics before shutdown: %v", lastErr) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
| defer cancel() | ||
| if err := shutdown(ctx); err != nil { | ||
| t.Fatalf("shutdown: %v", err) | ||
| } | ||
|
|
||
| _, err = client.Get("http://" + addr + "/metrics") | ||
| if err == nil { | ||
| t.Fatal("expected GET /metrics to fail after shutdown") | ||
| } | ||
| } |
|
RoddieKieley marked this conversation as resolved.
|
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
69 changes: 69 additions & 0 deletions
69
controller/deploy/operator/internal/controller/jumpstarter/router_metrics_bind_test.go
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,69 @@ | ||
| /* | ||
| Copyright 2026. The Jumpstarter Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package jumpstarter | ||
|
|
||
| import ( | ||
| operatorv1alpha1 "github.com/jumpstarter-dev/jumpstarter/controller/deploy/operator/api/v1alpha1" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| var _ = Describe("createRouterDeployment metrics bind", func() { | ||
| var r *JumpstarterReconciler | ||
| var js *operatorv1alpha1.Jumpstarter | ||
|
|
||
| BeforeEach(func() { | ||
| r = &JumpstarterReconciler{} | ||
| js = &operatorv1alpha1.Jumpstarter{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "jumpstarter", | ||
| Namespace: "jumpstarter-lab", | ||
| }, | ||
| Spec: operatorv1alpha1.JumpstarterSpec{ | ||
| Routers: operatorv1alpha1.RoutersConfig{ | ||
| Image: "example.com/router:test", | ||
| ImagePullPolicy: corev1.PullIfNotPresent, | ||
| Replicas: 1, | ||
| }, | ||
| }, | ||
| } | ||
| }) | ||
|
|
||
| It("exposes metrics-bind-address=:8080 and metrics port 8080", func() { | ||
| dep := r.createRouterDeployment(js, 0, "") | ||
| Expect(dep).NotTo(BeNil()) | ||
| Expect(dep.Spec.Template.Spec.Containers).NotTo(BeEmpty()) | ||
|
|
||
| c := dep.Spec.Template.Spec.Containers[0] | ||
| Expect(c.Args).To(Or( | ||
| ContainElement("-metrics-bind-address=:8080"), | ||
| ContainElement("--metrics-bind-address=:8080"), | ||
| )) | ||
|
|
||
| var metricsPort *corev1.ContainerPort | ||
| for i := range c.Ports { | ||
| if c.Ports[i].Name == "metrics" { | ||
| metricsPort = &c.Ports[i] | ||
| break | ||
| } | ||
| } | ||
| Expect(metricsPort).NotTo(BeNil(), "expected container port named metrics") | ||
| Expect(metricsPort.ContainerPort).To(Equal(int32(8080))) | ||
| }) | ||
| }) |
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.