Restructure modules (#1572)
* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
This commit is contained in:
157
base/metrics/api.go
Normal file
157
base/metrics/api.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/safing/portmaster/base/api"
|
||||
"github.com/safing/portmaster/base/config"
|
||||
"github.com/safing/portmaster/base/log"
|
||||
"github.com/safing/portmaster/service/mgr"
|
||||
)
|
||||
|
||||
func registerAPI() error {
|
||||
api.RegisterHandler("/metrics", &metricsAPI{})
|
||||
|
||||
if err := api.RegisterEndpoint(api.Endpoint{
|
||||
Name: "Export Registered Metrics",
|
||||
Description: "List all registered metrics with their metadata.",
|
||||
Path: "metrics/list",
|
||||
Read: api.Dynamic,
|
||||
StructFunc: func(ar *api.Request) (any, error) {
|
||||
return ExportMetrics(ar.AuthToken.Read), nil
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := api.RegisterEndpoint(api.Endpoint{
|
||||
Name: "Export Metric Values",
|
||||
Description: "List all exportable metric values.",
|
||||
Path: "metrics/values",
|
||||
Read: api.Dynamic,
|
||||
Parameters: []api.Parameter{{
|
||||
Method: http.MethodGet,
|
||||
Field: "internal-only",
|
||||
Description: "Specify to only return metrics with an alternative internal ID.",
|
||||
}},
|
||||
StructFunc: func(ar *api.Request) (any, error) {
|
||||
return ExportValues(
|
||||
ar.AuthToken.Read,
|
||||
ar.Request.URL.Query().Has("internal-only"),
|
||||
), nil
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type metricsAPI struct{}
|
||||
|
||||
func (m *metricsAPI) ReadPermission(*http.Request) api.Permission { return api.Dynamic }
|
||||
|
||||
func (m *metricsAPI) WritePermission(*http.Request) api.Permission { return api.NotSupported }
|
||||
|
||||
func (m *metricsAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// Get API Request for permission and query.
|
||||
ar := api.GetAPIRequest(r)
|
||||
if ar == nil {
|
||||
http.Error(w, "Missing API Request.", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Get expertise level from query.
|
||||
expertiseLevel := config.ExpertiseLevelDeveloper
|
||||
switch ar.Request.URL.Query().Get("level") {
|
||||
case config.ExpertiseLevelNameUser:
|
||||
expertiseLevel = config.ExpertiseLevelUser
|
||||
case config.ExpertiseLevelNameExpert:
|
||||
expertiseLevel = config.ExpertiseLevelExpert
|
||||
case config.ExpertiseLevelNameDeveloper:
|
||||
expertiseLevel = config.ExpertiseLevelDeveloper
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain; version=0.0.4; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
WriteMetrics(w, ar.AuthToken.Read, expertiseLevel)
|
||||
}
|
||||
|
||||
// WriteMetrics writes all metrics that match the given permission and
|
||||
// expertiseLevel to the given writer.
|
||||
func WriteMetrics(w io.Writer, permission api.Permission, expertiseLevel config.ExpertiseLevel) {
|
||||
registryLock.RLock()
|
||||
defer registryLock.RUnlock()
|
||||
|
||||
// Write all matching metrics.
|
||||
for _, metric := range registry {
|
||||
if permission >= metric.Opts().Permission &&
|
||||
expertiseLevel >= metric.Opts().ExpertiseLevel {
|
||||
metric.WritePrometheus(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeMetricsTo(ctx context.Context, url string) error {
|
||||
// First, collect metrics into buffer.
|
||||
buf := &bytes.Buffer{}
|
||||
WriteMetrics(buf, api.PermitSelf, config.ExpertiseLevelDeveloper)
|
||||
|
||||
// Check if there is something to send.
|
||||
if buf.Len() == 0 {
|
||||
log.Debugf("metrics: not pushing metrics, nothing to send")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create request
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
// Send.
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
// Check return status.
|
||||
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get and return error.
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf(
|
||||
"got %s while writing metrics to %s: %s",
|
||||
resp.Status,
|
||||
url,
|
||||
body,
|
||||
)
|
||||
}
|
||||
|
||||
func metricsWriter(ctx *mgr.WorkerCtx) error {
|
||||
pushURL := pushOption()
|
||||
module.metricTicker = mgr.NewSleepyTicker(1*time.Minute, 0)
|
||||
defer module.metricTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-module.metricTicker.Wait():
|
||||
err := writeMetricsTo(ctx.Ctx(), pushURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user