-
Notifications
You must be signed in to change notification settings - Fork 15
[dbsql] [ffapi] Metrics for Go and DB Stats #205
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright © 2026 Kaleido, Inc. | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| // 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 dbsql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
|
|
||
| "github.com/hyperledger/firefly-common/pkg/metric" | ||
| "github.com/prometheus/client_golang/prometheus/collectors" | ||
| ) | ||
|
|
||
| type metricsOptions struct { | ||
| dbName string | ||
| } | ||
|
|
||
| func (opts *metricsOptions) apply(options ...MetricsOption) { | ||
| for _, option := range options { | ||
| option(opts) | ||
| } | ||
| } | ||
|
|
||
| type MetricsOption func(opts *metricsOptions) | ||
|
|
||
| // WithDBName sets the name of the database label for the metrics collector. | ||
| func WithDBName(dbName string) MetricsOption { | ||
| return func(opts *metricsOptions) { | ||
| opts.dbName = dbName | ||
| } | ||
| } | ||
|
|
||
| // EnableDBMetrics registers a prometheus collector for the given database connection. | ||
| func EnableDBMetrics(_ context.Context, metricsRegistry metric.MetricsRegistry, db *sql.DB, options ...MetricsOption) { | ||
| opts := &metricsOptions{} | ||
| opts.apply(options...) | ||
|
|
||
| if opts.dbName == "" { | ||
| opts.dbName = "default" | ||
| } | ||
|
|
||
| metricsRegistry.MustRegisterCollector(collectors.NewDBStatsCollector(db, opts.dbName)) | ||
|
Contributor
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. Info on stats https://pkg.go.dev/database/sql#DBStats |
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||
| // Copyright © 2025 Kaleido, Inc. | ||||||
| // Copyright © 2026 Kaleido, Inc. | ||||||
| // | ||||||
| // SPDX-License-Identifier: Apache-2.0 | ||||||
| // | ||||||
|
|
@@ -26,6 +26,7 @@ import ( | |||||
| "time" | ||||||
|
|
||||||
| "github.com/prometheus/client_golang/prometheus" | ||||||
| "github.com/prometheus/client_golang/prometheus/collectors" | ||||||
| "github.com/prometheus/client_golang/prometheus/promhttp" | ||||||
|
|
||||||
| "github.com/gorilla/mux" | ||||||
|
|
@@ -65,6 +66,7 @@ type apiServer[T any] struct { | |||||
| alwaysPaginate bool | ||||||
| handleYAML bool | ||||||
| monitoringEnabled bool | ||||||
| goProcessMetricsEnabled bool | ||||||
|
Contributor
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. This is whole pkg is already Golang
Suggested change
|
||||||
| metricsPath string | ||||||
| livenessPath string | ||||||
| loggingPath string | ||||||
|
|
@@ -116,15 +118,17 @@ func NewAPIServer[T any](ctx context.Context, options APIServerOptions[T]) APISe | |||||
| } | ||||||
|
|
||||||
| as := &apiServer[T]{ | ||||||
| defaultFilterLimit: options.APIConfig.GetUint64(ConfAPIDefaultFilterLimit), | ||||||
| maxFilterLimit: options.APIConfig.GetUint64(ConfAPIMaxFilterLimit), | ||||||
| maxFilterSkip: options.APIConfig.GetUint64(ConfAPIMaxFilterSkip), | ||||||
| requestTimeout: options.APIConfig.GetDuration(ConfAPIRequestTimeout), | ||||||
| requestMaxTimeout: options.APIConfig.GetDuration(ConfAPIRequestMaxTimeout), | ||||||
| monitoringEnabled: options.MonitoringConfig.GetBool(ConfMonitoringServerEnabled), | ||||||
| metricsPath: options.MonitoringConfig.GetString(ConfMonitoringServerMetricsPath), | ||||||
| livenessPath: options.MonitoringConfig.GetString(ConfMonitoringServerLivenessPath), | ||||||
| loggingPath: options.MonitoringConfig.GetString(ConfMonitoringServerLoggingPath), | ||||||
| defaultFilterLimit: options.APIConfig.GetUint64(ConfAPIDefaultFilterLimit), | ||||||
| maxFilterLimit: options.APIConfig.GetUint64(ConfAPIMaxFilterLimit), | ||||||
| maxFilterSkip: options.APIConfig.GetUint64(ConfAPIMaxFilterSkip), | ||||||
| requestTimeout: options.APIConfig.GetDuration(ConfAPIRequestTimeout), | ||||||
| requestMaxTimeout: options.APIConfig.GetDuration(ConfAPIRequestMaxTimeout), | ||||||
| monitoringEnabled: options.MonitoringConfig.GetBool(ConfMonitoringServerEnabled), | ||||||
| goProcessMetricsEnabled: options.MonitoringConfig.GetBool(ConfMonitoringGoProcessMetricsEnabled), | ||||||
| metricsPath: options.MonitoringConfig.GetString(ConfMonitoringServerMetricsPath), | ||||||
| livenessPath: options.MonitoringConfig.GetString(ConfMonitoringServerLivenessPath), | ||||||
| loggingPath: options.MonitoringConfig.GetString(ConfMonitoringServerLoggingPath), | ||||||
|
|
||||||
| alwaysPaginate: options.APIConfig.GetBool(ConfAPIAlwaysPaginate), | ||||||
| handleYAML: options.HandleYAML, | ||||||
| apiDynamicPublicURLHeader: options.APIConfig.GetString(ConfAPIDynamicPublicURLHeader), | ||||||
|
|
@@ -306,6 +310,11 @@ func (as *apiServer[T]) createMuxRouter(ctx context.Context) (*mux.Router, error | |||||
| } | ||||||
|
|
||||||
| if as.monitoringEnabled { | ||||||
| if as.goProcessMetricsEnabled { | ||||||
| as.MetricsRegistry.MustRegisterCollector(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) | ||||||
| as.MetricsRegistry.MustRegisterCollector(collectors.NewGoCollector()) | ||||||
| } | ||||||
| as.MetricsRegistry.MustRegisterCollector(collectors.NewBuildInfoCollector()) | ||||||
|
Contributor
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. Important comment on this collector: |
||||||
| h, _ := as.MetricsRegistry.GetHTTPMetricsInstrumentationsMiddlewareForSubsystem(ctx, as.metricsSubsystemName()) | ||||||
| r.Use(h) | ||||||
| } | ||||||
|
|
||||||
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.
Confused on this one, it feels like it should be mandatory to supply a dbName