-
Notifications
You must be signed in to change notification settings - Fork 59
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
feat: metrics for services and checks #519
Open
IronCore864
wants to merge
27
commits into
canonical:master
Choose a base branch
from
IronCore864:poc-custom-metrics-lib
base: master
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.
+730
−55
Open
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f76470c
poc: a metrics module for pebble
IronCore864 6d8ee59
chore: undo unnecessary change
IronCore864 b4abc9a
chore: undo unnecessary change
IronCore864 a274276
chore: undo unnecessary change
IronCore864 a2c07e6
chore: metrics identity basic auth poc
IronCore864 4ebb633
chore: a poc for metrics with labels
IronCore864 7468b95
poc: remove adding identities using env vars according to comment in …
IronCore864 790a8f9
chore: update tests for the metrics lib poc
IronCore864 272005b
chore: refactor identities and access according to spec review
IronCore864 5be3e96
feat: use sha512 to verify password
IronCore864 a6c374d
feat: move the metrics api to /v1/metrics
IronCore864 1bd54cb
chore: remove Username from apiBasicIdentity
IronCore864 98ea11e
chore: revert changes on user state
IronCore864 68c18b7
Merge branch 'master' into poc-custom-metrics-lib
IronCore864 7fc255e
chore: fix failed identity tests
IronCore864 31a0617
test: unit tests for basic identity
IronCore864 a57f041
chore: rework the metrics for services
IronCore864 b7a442f
chore: add metrics for checks, not done
IronCore864 8ebebb8
chore: refactor metrics, add open telemetry writer
IronCore864 363eaf0
Merge branch 'master' into poc-custom-metrics-lib
IronCore864 a1db1a6
chore: refactor according to review, fix check counter reset issue
IronCore864 047cc42
Merge branch 'master' into poc-custom-metrics-lib
IronCore864 c527344
chore: add a test for check metrics
IronCore864 5476299
test: add tests for open telemetry writer
IronCore864 9e5b65a
test: service metrics
IronCore864 d678766
chore: update tests
IronCore864 c99fa53
chore: fix linting
IronCore864 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 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 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 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 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 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 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 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,53 @@ | ||
// Copyright (c) 2024 Canonical Ltd | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 3 as | ||
// published by the Free Software Foundation. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package daemon | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
|
||
"github.com/canonical/pebble/internals/overlord/checkstate" | ||
"github.com/canonical/pebble/internals/overlord/servstate" | ||
) | ||
|
||
func v1GetMetrics(c *Command, r *http.Request, _ *UserState) Response { | ||
return metricsResponse{ | ||
svcMgr: overlordServiceManager(c.d.overlord), | ||
chkMgr: overlordCheckManager(c.d.overlord), | ||
} | ||
} | ||
|
||
// metricsResponse is a Response implementation to serve the metrics in the OpenMetrics format. | ||
type metricsResponse struct { | ||
svcMgr *servstate.ServiceManager | ||
chkMgr *checkstate.CheckManager | ||
} | ||
|
||
func (r metricsResponse) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
var buffer bytes.Buffer | ||
|
||
err := r.svcMgr.Metrics(&buffer) | ||
if err != nil { | ||
IronCore864 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
err = r.chkMgr.Metrics(&buffer) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
IronCore864 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
w.Write([]byte(buffer.String())) | ||
IronCore864 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains 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 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,50 @@ | ||
// Copyright (c) 2025 Canonical Ltd | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License version 3 as | ||
// published by the Free Software Foundation. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
// Metric represents a single metric. | ||
type Metric struct { | ||
Name string | ||
Value interface{} | ||
IronCore864 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
LabelPairs []string | ||
} | ||
|
||
// WriteTo writes the metric in OpenMetrics format. | ||
func (m *Metric) WriteTo(w io.Writer) (n int64, err error) { | ||
labelStr := "" | ||
if len(m.LabelPairs) > 0 { | ||
sort.Strings(m.LabelPairs) | ||
labelStr = "{" + strings.Join(m.LabelPairs, ",") + "}" | ||
IronCore864 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var written int | ||
switch v := m.Value.(type) { | ||
case int64: | ||
written, err = fmt.Fprintf(w, "%s%s %d\n", m.Name, labelStr, v) | ||
case float64: | ||
written, err = fmt.Fprintf(w, "%s%s %.2f\n", m.Name, labelStr, v) // Format float appropriately | ||
default: | ||
written, err = fmt.Fprintf(w, "%s%s %v\n", m.Name, labelStr, m.Value) | ||
} | ||
|
||
return int64(written), err | ||
} |
This file contains 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.
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.
Is this package maintained? Are we confident about introducing it in every charm / pebble deployment out there?
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 hasn't been recently changed, but the part of it we're using is small a straight-forward, and the algorithm being implemented is stable and well-documented (SHA-crypt). So I would say it's "stable" rather than "unmaintained". I'm actually contemplating vendoring this (the core part of it is only ~100 LoC). But open to other ideas/concerns too.
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.
We've done some diligence on it, see #563 (comment) for example.