-
Notifications
You must be signed in to change notification settings - Fork 70
feat: add prometheus endpoint #1008
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
pauliyobo
wants to merge
8
commits into
xataio:main
Choose a base branch
from
pauliyobo:issue903
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
8 commits
Select commit
Hold shift + click to select a range
1009cf3
Initial implementation for configurable prometheus exporter
pauliyobo ef22e0e
gate prometheus exporter behind instrumentation config
pauliyobo 69a7746
add tests
pauliyobo 386234b
fix potential panic caused by nil dereference
pauliyobo 2cca19b
update documentation
pauliyobo ce65618
run go mod tidy
pauliyobo eeb8894
Merge branch 'main' into issue903
kvch 1bb15c9
fix health.NewServer invocations in new tests
pauliyobo 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
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 |
|---|---|---|
|
|
@@ -12,12 +12,15 @@ | |
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||
| "github.com/xataio/pgstream/internal/json" | ||
| loglib "github.com/xataio/pgstream/pkg/log" | ||
| "github.com/xataio/pgstream/pkg/otel" | ||
| ) | ||
|
|
||
| const ( | ||
| DefaultAddress = "localhost:9910" | ||
| DefaultAddress = "localhost:9910" | ||
| DefaultPrometheusEndpoint = "/metrics" | ||
|
|
||
| readinessCheckTimeout = 2 * time.Second | ||
| readHeaderTimeout = 5 * time.Second | ||
|
|
@@ -38,6 +41,7 @@ | |
| phaseProvider func() string | ||
| listener net.Listener | ||
| httpServer *http.Server | ||
| metricsConfig *otel.MetricsConfig | ||
| } | ||
|
|
||
| var errNotListening = errors.New("health server not initialised: call Listen first") | ||
|
|
@@ -46,14 +50,15 @@ | |
|
|
||
| // NewServer builds a health server from the given config. The server is not | ||
| // started until Start() is called. | ||
| func NewServer(cfg Config, opts ...Option) *Server { | ||
| func NewServer(cfg Config, metricsCfg *otel.MetricsConfig, opts ...Option) *Server { | ||
| address := cfg.Address | ||
| if address == "" { | ||
| address = DefaultAddress | ||
| } | ||
| s := &Server{ | ||
| logger: loglib.NewNoopLogger(), | ||
| address: address, | ||
| logger: loglib.NewNoopLogger(), | ||
| address: address, | ||
| metricsConfig: metricsCfg, | ||
| } | ||
| for _, opt := range opts { | ||
| opt(s) | ||
|
|
@@ -114,6 +119,14 @@ | |
| mux.HandleFunc("/ready", s.handleReady) | ||
| mux.HandleFunc("/status", s.handleStatus) | ||
|
|
||
| if s.isPrometheusEnabled() { | ||
| endpoint := DefaultPrometheusEndpoint | ||
| if promEndpoint := s.metricsConfig.Prometheus.Endpoint; promEndpoint != "" { | ||
|
Collaborator
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. Please validate the endpoint format. |
||
| endpoint = promEndpoint | ||
| } | ||
| mux.HandleFunc(endpoint, s.handlePrometheus) | ||
| } | ||
|
|
||
| s.listener = ln | ||
| s.httpServer = &http.Server{ | ||
| Handler: mux, | ||
|
|
@@ -200,3 +213,16 @@ | |
| s.logger.Warn(err, "health server: writing response") | ||
| } | ||
| } | ||
|
|
||
| func (s *Server) isPrometheusEnabled() bool { | ||
| return s.metricsConfig != nil && s.metricsConfig.Prometheus != nil && s.metricsConfig.Prometheus.Enabled | ||
|
|
||
| } | ||
|
|
||
| func (s *Server) handlePrometheus(w http.ResponseWriter, req *http.Request) { | ||
| if s.isPrometheusEnabled() { | ||
| promhttp.Handler().ServeHTTP(w, req) | ||
| } else { | ||
| s.writeJSON(w, http.StatusNotFound, map[string]any{}) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
It seems to me that if the health server is not enabled, but prometheus is enabled, the endpoint is not going to work.
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.
Yeah, that's correct. This is because of the dependency the metrics endpoint has on the health server. this was also documented. What would be preferred in this case?