-
Notifications
You must be signed in to change notification settings - Fork 253
feat_: add ability to process prometheus metrics in telemetry client #5782
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: develop
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,139 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
prom_model "github.com/prometheus/client_model/go" | ||
) | ||
|
||
type MetricType int | ||
|
||
const ( | ||
_ MetricType = iota | ||
CounterType | ||
GaugeType | ||
) | ||
|
||
type TelemetryRecord struct { | ||
NodeName string `json:"nodeName"` | ||
PeerID string `json:"peerId"` | ||
StatusVersion string `json:"statusVersion"` | ||
DeviceType string `json:"deviceType"` | ||
} | ||
|
||
type ProcessTelemetryRequest func(ctx context.Context, data interface{}) | ||
|
||
type MetricPayload struct { | ||
Name string | ||
Value []*prom_model.Metric | ||
} | ||
|
||
type Metric struct { | ||
typ MetricType | ||
labels map[string]string | ||
} | ||
|
||
type PrometheusMetrics struct { | ||
metrics map[string]Metric | ||
process ProcessTelemetryRequest | ||
telemetryRecord TelemetryRecord | ||
} | ||
|
||
func NewPrometheusMetrics(process ProcessTelemetryRequest, tc TelemetryRecord) *PrometheusMetrics { | ||
return &PrometheusMetrics{ | ||
metrics: make(map[string]Metric), | ||
process: process, | ||
telemetryRecord: tc, | ||
} | ||
} | ||
|
||
func (pm *PrometheusMetrics) Register(name string, typ MetricType, labels prometheus.Labels) { | ||
pm.metrics[name] = Metric{typ, labels} | ||
} | ||
|
||
func (pm *PrometheusMetrics) Snapshot() { | ||
gatherer := prometheus.DefaultGatherer | ||
metrics, err := gatherer.Gather() | ||
if err != nil { | ||
log.Fatalf("Failed to gather metrics: %v", err) | ||
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. Not sure if we should use |
||
} | ||
|
||
for _, mf := range metrics { | ||
metric, ok := pm.metrics[*mf.Name] | ||
if !ok { | ||
continue | ||
} | ||
|
||
metricFamilyValue := mf.GetMetric() | ||
|
||
if len(metricFamilyValue) == 0 { | ||
continue | ||
} | ||
|
||
metricValue := []*prom_model.Metric{} | ||
|
||
if metric.labels != nil { //filter out metrics based on labels | ||
for _, m := range mf.GetMetric() { | ||
|
||
matchCnt := len(metric.labels) | ||
|
||
for name, value := range metric.labels { | ||
for _, label := range m.GetLabel() { | ||
if name == *label.Name && value == *label.Value { | ||
matchCnt-- | ||
} | ||
} | ||
} | ||
|
||
if matchCnt > 0 { | ||
continue | ||
} | ||
|
||
metricValue = append(metricValue, m) | ||
|
||
} | ||
} else { | ||
metricValue = metricFamilyValue | ||
} | ||
|
||
if len(metricValue) == 0 { | ||
continue | ||
} | ||
|
||
p := MetricPayload{Name: *mf.Name, Value: metricValue} | ||
|
||
pm.ToTelemetryRequest(p) | ||
} | ||
|
||
} | ||
|
||
func (pm *PrometheusMetrics) ToTelemetryRequest(p MetricPayload) error { | ||
postBody := map[string]interface{}{ | ||
"value": p.Value, | ||
"name": p.Name, | ||
"nodeName": pm.telemetryRecord.NodeName, | ||
"deviceType": pm.telemetryRecord.DeviceType, | ||
"peerId": pm.telemetryRecord.PeerID, | ||
"statusVersion": pm.telemetryRecord.StatusVersion, | ||
"timestamp": time.Now().Unix(), | ||
} | ||
|
||
telemtryData, err := json.Marshal(postBody) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rawData := json.RawMessage(telemtryData) | ||
|
||
wrap := PrometheusMetricWrapper{ | ||
Typ: "PrometheusMetric", | ||
Data: &rawData, | ||
} | ||
|
||
pm.process(context.Background(), wrap) | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,10 +27,10 @@ var ( | |
Name: "waku2_envelopes_received_total", | ||
Help: "Number of envelopes received.", | ||
}) | ||
EnvelopesValidatedCounter = prom.NewCounter(prom.CounterOpts{ | ||
EnvelopesValidatedCounter = prom.NewCounterVec(prom.CounterOpts{ | ||
Name: "waku2_envelopes_validated_total", | ||
Help: "Number of envelopes processed successfully.", | ||
}) | ||
}, []string{"pubsubTopic", "type"}) | ||
EnvelopesRejectedCounter = prom.NewCounterVec(prom.CounterOpts{ | ||
Name: "waku2_envelopes_rejected_total", | ||
Help: "Number of envelopes rejected.", | ||
|
@@ -52,7 +52,7 @@ var ( | |
|
||
func init() { | ||
prom.MustRegister(EnvelopesReceivedCounter) | ||
prom.MustRegister(EnvelopesRejectedCounter) | ||
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. Was this metric supposed to be removed? |
||
prom.MustRegister(EnvelopesValidatedCounter) | ||
prom.MustRegister(EnvelopesCacheFailedCounter) | ||
prom.MustRegister(EnvelopesCachedCounter) | ||
prom.MustRegister(EnvelopesSizeMeter) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ import ( | |
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/peerstore" | ||
"github.com/multiformats/go-multiaddr" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"go.uber.org/zap" | ||
|
||
|
@@ -274,6 +275,7 @@ func New(nodeKey *ecdsa.PrivateKey, fleet string, cfg *Config, logger *zap.Logge | |
node.WithLogLevel(logger.Level()), | ||
node.WithClusterID(cfg.ClusterID), | ||
node.WithMaxMsgSize(1024 * 1024), | ||
node.WithPrometheusRegisterer(prometheus.DefaultRegisterer), | ||
} | ||
|
||
if cfg.EnableDiscV5 { | ||
|
@@ -1105,10 +1107,11 @@ func (w *Waku) Start() error { | |
w.logger) | ||
|
||
w.missingMsgVerifier.Start(w.ctx) | ||
w.logger.Info("Started missing message verifier") | ||
|
||
w.wg.Add(1) | ||
go func() { | ||
w.wg.Done() | ||
defer w.wg.Done() | ||
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. not sure how this wait group is intended to behave cc @richard-ramos 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. Adding the defer is correct. This is a bug i introduced when doing the refactoring |
||
for { | ||
select { | ||
case <-w.ctx.Done(): | ||
|
@@ -1118,6 +1121,7 @@ func (w *Waku) Start() error { | |
if err != nil { | ||
w.logger.Error("OnNewEnvelopes error", zap.Error(err)) | ||
} | ||
w.logger.Info("Got a missing message!") | ||
} | ||
} | ||
}() | ||
|
@@ -1347,7 +1351,7 @@ func (w *Waku) OnNewEnvelopes(envelope *protocol.Envelope, msgType common.Messag | |
trouble = true | ||
} | ||
|
||
common.EnvelopesValidatedCounter.Inc() | ||
common.EnvelopesValidatedCounter.With(prometheus.Labels{"pubsubTopic": envelope.PubsubTopic(), "type": msgType}).Inc() | ||
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. Note that we need the meaning of |
||
|
||
if trouble { | ||
return errors.New("received invalid envelope") | ||
|
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.
should probably remove