mirror of https://github.com/libp2p/go-libp2p.git
sukun
4 months ago
committed by
GitHub
6 changed files with 742 additions and 16 deletions
@ -0,0 +1,506 @@ |
|||
{ |
|||
"annotations": { |
|||
"list": [ |
|||
{ |
|||
"builtIn": 1, |
|||
"datasource": { |
|||
"type": "grafana", |
|||
"uid": "-- Grafana --" |
|||
}, |
|||
"enable": true, |
|||
"hide": true, |
|||
"iconColor": "rgba(0, 211, 255, 1)", |
|||
"name": "Annotations & Alerts", |
|||
"target": { |
|||
"limit": 100, |
|||
"matchAny": false, |
|||
"tags": [], |
|||
"type": "dashboard" |
|||
}, |
|||
"type": "dashboard" |
|||
} |
|||
] |
|||
}, |
|||
"editable": true, |
|||
"fiscalYearStartMonth": 0, |
|||
"graphTooltip": 0, |
|||
"id": 4, |
|||
"links": [], |
|||
"panels": [ |
|||
{ |
|||
"datasource": { |
|||
"type": "prometheus", |
|||
"uid": "${data_source}" |
|||
}, |
|||
"fieldConfig": { |
|||
"defaults": { |
|||
"color": { |
|||
"mode": "palette-classic", |
|||
"seriesBy": "last" |
|||
}, |
|||
"custom": { |
|||
"axisBorderShow": false, |
|||
"axisCenteredZero": false, |
|||
"axisColorMode": "text", |
|||
"axisLabel": "", |
|||
"axisPlacement": "auto", |
|||
"barAlignment": 0, |
|||
"drawStyle": "line", |
|||
"fillOpacity": 0, |
|||
"gradientMode": "none", |
|||
"hideFrom": { |
|||
"legend": false, |
|||
"tooltip": false, |
|||
"viz": false |
|||
}, |
|||
"insertNulls": false, |
|||
"lineInterpolation": "linear", |
|||
"lineWidth": 1, |
|||
"pointSize": 5, |
|||
"scaleDistribution": { |
|||
"type": "linear" |
|||
}, |
|||
"showPoints": "auto", |
|||
"spanNulls": false, |
|||
"stacking": { |
|||
"group": "A", |
|||
"mode": "none" |
|||
}, |
|||
"thresholdsStyle": { |
|||
"mode": "off" |
|||
} |
|||
}, |
|||
"mappings": [], |
|||
"thresholds": { |
|||
"mode": "absolute", |
|||
"steps": [ |
|||
{ |
|||
"color": "green", |
|||
"value": null |
|||
}, |
|||
{ |
|||
"color": "red", |
|||
"value": 80 |
|||
} |
|||
] |
|||
} |
|||
}, |
|||
"overrides": [ |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "OK" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "green", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
}, |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "E_DIAL_REFUSED" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "purple", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
} |
|||
] |
|||
}, |
|||
"gridPos": { |
|||
"h": 8, |
|||
"w": 12, |
|||
"x": 0, |
|||
"y": 0 |
|||
}, |
|||
"id": 1, |
|||
"options": { |
|||
"legend": { |
|||
"calcs": [], |
|||
"displayMode": "list", |
|||
"placement": "bottom", |
|||
"showLegend": true |
|||
}, |
|||
"tooltip": { |
|||
"maxHeight": 600, |
|||
"mode": "single", |
|||
"sort": "none" |
|||
} |
|||
}, |
|||
"targets": [ |
|||
{ |
|||
"datasource": { |
|||
"type": "prometheus", |
|||
"uid": "${data_source}" |
|||
}, |
|||
"editorMode": "code", |
|||
"expr": "sum by (response_status) (increase(libp2p_autonatv2_requests_completed_total{instance=~\"$instance\", server_error=\"nil\"}[$__rate_interval]))\n", |
|||
"instant": false, |
|||
"legendFormat": "__auto", |
|||
"range": true, |
|||
"refId": "A" |
|||
} |
|||
], |
|||
"title": "Dial Request by Response Status", |
|||
"type": "timeseries" |
|||
}, |
|||
{ |
|||
"datasource": { |
|||
"type": "prometheus", |
|||
"uid": "${data_source}" |
|||
}, |
|||
"fieldConfig": { |
|||
"defaults": { |
|||
"color": { |
|||
"mode": "palette-classic", |
|||
"seriesBy": "last" |
|||
}, |
|||
"custom": { |
|||
"axisBorderShow": false, |
|||
"axisCenteredZero": false, |
|||
"axisColorMode": "text", |
|||
"axisLabel": "", |
|||
"axisPlacement": "auto", |
|||
"barAlignment": 0, |
|||
"drawStyle": "line", |
|||
"fillOpacity": 0, |
|||
"gradientMode": "none", |
|||
"hideFrom": { |
|||
"legend": false, |
|||
"tooltip": false, |
|||
"viz": false |
|||
}, |
|||
"insertNulls": false, |
|||
"lineInterpolation": "linear", |
|||
"lineWidth": 1, |
|||
"pointSize": 5, |
|||
"scaleDistribution": { |
|||
"type": "linear" |
|||
}, |
|||
"showPoints": "auto", |
|||
"spanNulls": false, |
|||
"stacking": { |
|||
"group": "A", |
|||
"mode": "none" |
|||
}, |
|||
"thresholdsStyle": { |
|||
"mode": "off" |
|||
} |
|||
}, |
|||
"mappings": [], |
|||
"thresholds": { |
|||
"mode": "absolute", |
|||
"steps": [ |
|||
{ |
|||
"color": "green", |
|||
"value": null |
|||
}, |
|||
{ |
|||
"color": "red", |
|||
"value": 80 |
|||
} |
|||
] |
|||
} |
|||
}, |
|||
"overrides": [ |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "OK" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "green", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
}, |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "E_DIAL_REFUSED" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "purple", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
}, |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "E_DIAL_ERROR" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "purple", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
} |
|||
] |
|||
}, |
|||
"gridPos": { |
|||
"h": 8, |
|||
"w": 12, |
|||
"x": 12, |
|||
"y": 0 |
|||
}, |
|||
"id": 2, |
|||
"options": { |
|||
"legend": { |
|||
"calcs": [], |
|||
"displayMode": "list", |
|||
"placement": "bottom", |
|||
"showLegend": true |
|||
}, |
|||
"tooltip": { |
|||
"maxHeight": 600, |
|||
"mode": "single", |
|||
"sort": "none" |
|||
} |
|||
}, |
|||
"targets": [ |
|||
{ |
|||
"datasource": { |
|||
"type": "prometheus", |
|||
"uid": "${data_source}" |
|||
}, |
|||
"editorMode": "code", |
|||
"expr": "sum by (ip_or_dns_version, transport, dial_status) (increase(libp2p_autonatv2_requests_completed_total{instance=~\"$instance\", server_error=\"nil\", response_status=\"OK\"}[$__rate_interval]))\n", |
|||
"instant": false, |
|||
"legendFormat": "__auto", |
|||
"range": true, |
|||
"refId": "A" |
|||
} |
|||
], |
|||
"title": "Dial Request by Dial Status", |
|||
"type": "timeseries" |
|||
}, |
|||
{ |
|||
"datasource": { |
|||
"type": "prometheus", |
|||
"uid": "${data_source}" |
|||
}, |
|||
"fieldConfig": { |
|||
"defaults": { |
|||
"color": { |
|||
"mode": "palette-classic", |
|||
"seriesBy": "last" |
|||
}, |
|||
"custom": { |
|||
"axisBorderShow": false, |
|||
"axisCenteredZero": false, |
|||
"axisColorMode": "text", |
|||
"axisLabel": "", |
|||
"axisPlacement": "auto", |
|||
"barAlignment": 0, |
|||
"drawStyle": "line", |
|||
"fillOpacity": 0, |
|||
"gradientMode": "none", |
|||
"hideFrom": { |
|||
"legend": false, |
|||
"tooltip": false, |
|||
"viz": false |
|||
}, |
|||
"insertNulls": false, |
|||
"lineInterpolation": "linear", |
|||
"lineWidth": 1, |
|||
"pointSize": 5, |
|||
"scaleDistribution": { |
|||
"type": "linear" |
|||
}, |
|||
"showPoints": "auto", |
|||
"spanNulls": false, |
|||
"stacking": { |
|||
"group": "A", |
|||
"mode": "none" |
|||
}, |
|||
"thresholdsStyle": { |
|||
"mode": "off" |
|||
} |
|||
}, |
|||
"mappings": [], |
|||
"thresholds": { |
|||
"mode": "absolute", |
|||
"steps": [ |
|||
{ |
|||
"color": "green", |
|||
"value": null |
|||
}, |
|||
{ |
|||
"color": "red", |
|||
"value": 80 |
|||
} |
|||
] |
|||
} |
|||
}, |
|||
"overrides": [ |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "OK" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "green", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
}, |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "E_DIAL_REFUSED" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "purple", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
}, |
|||
{ |
|||
"matcher": { |
|||
"id": "byName", |
|||
"options": "E_DIAL_ERROR" |
|||
}, |
|||
"properties": [ |
|||
{ |
|||
"id": "color", |
|||
"value": { |
|||
"fixedColor": "purple", |
|||
"mode": "fixed" |
|||
} |
|||
} |
|||
] |
|||
} |
|||
] |
|||
}, |
|||
"gridPos": { |
|||
"h": 8, |
|||
"w": 12, |
|||
"x": 5, |
|||
"y": 8 |
|||
}, |
|||
"id": 3, |
|||
"options": { |
|||
"legend": { |
|||
"calcs": [], |
|||
"displayMode": "list", |
|||
"placement": "bottom", |
|||
"showLegend": true |
|||
}, |
|||
"tooltip": { |
|||
"maxHeight": 600, |
|||
"mode": "single", |
|||
"sort": "none" |
|||
} |
|||
}, |
|||
"targets": [ |
|||
{ |
|||
"datasource": { |
|||
"type": "prometheus", |
|||
"uid": "${data_source}" |
|||
}, |
|||
"editorMode": "code", |
|||
"expr": "sum by (server_error) (increase(libp2p_autonatv2_requests_completed_total{instance=~\"$instance\", server_error!=\"nil\"}[$__rate_interval]))\n", |
|||
"instant": false, |
|||
"legendFormat": "__auto", |
|||
"range": true, |
|||
"refId": "A" |
|||
} |
|||
], |
|||
"title": "Dial Request Errors", |
|||
"type": "timeseries" |
|||
} |
|||
], |
|||
"refresh": "", |
|||
"schemaVersion": 39, |
|||
"tags": [], |
|||
"templating": { |
|||
"list": [ |
|||
{ |
|||
"allValue": "", |
|||
"current": { |
|||
"selected": true, |
|||
"text": [ |
|||
"All" |
|||
], |
|||
"value": [ |
|||
"$__all" |
|||
] |
|||
}, |
|||
"definition": "label_values(up,instance)", |
|||
"hide": 0, |
|||
"includeAll": true, |
|||
"label": "instance", |
|||
"multi": true, |
|||
"name": "instance", |
|||
"options": [], |
|||
"query": { |
|||
"qryType": 1, |
|||
"query": "label_values(up,instance)", |
|||
"refId": "PrometheusVariableQueryEditor-VariableQuery" |
|||
}, |
|||
"refresh": 1, |
|||
"regex": "", |
|||
"skipUrlSync": false, |
|||
"sort": 0, |
|||
"type": "query" |
|||
}, |
|||
{ |
|||
"hide": 0, |
|||
"includeAll": false, |
|||
"label": "", |
|||
"multi": false, |
|||
"name": "data_source", |
|||
"options": [], |
|||
"query": "prometheus", |
|||
"queryValue": "", |
|||
"refresh": 1, |
|||
"regex": "", |
|||
"skipUrlSync": false, |
|||
"type": "datasource" |
|||
} |
|||
] |
|||
}, |
|||
"time": { |
|||
"from": "now-1h", |
|||
"to": "now" |
|||
}, |
|||
"timeRangeUpdatedDuringEditOrView": false, |
|||
"timepicker": {}, |
|||
"timezone": "browser", |
|||
"title": "go-libp2p autoNATv2", |
|||
"uid": "cdpusyp3xtfcwa", |
|||
"version": 1, |
|||
"weekStart": "" |
|||
} |
@ -0,0 +1,97 @@ |
|||
package autonatv2 |
|||
|
|||
import ( |
|||
"github.com/libp2p/go-libp2p/p2p/metricshelper" |
|||
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pb" |
|||
ma "github.com/multiformats/go-multiaddr" |
|||
"github.com/prometheus/client_golang/prometheus" |
|||
) |
|||
|
|||
type MetricsTracer interface { |
|||
CompletedRequest(EventDialRequestCompleted) |
|||
} |
|||
|
|||
const metricNamespace = "libp2p_autonatv2" |
|||
|
|||
var ( |
|||
requestsCompleted = prometheus.NewCounterVec( |
|||
prometheus.CounterOpts{ |
|||
Namespace: metricNamespace, |
|||
Name: "requests_completed_total", |
|||
Help: "Requests Completed", |
|||
}, |
|||
[]string{"server_error", "response_status", "dial_status", "dial_data_required", "ip_or_dns_version", "transport"}, |
|||
) |
|||
) |
|||
|
|||
type metricsTracer struct { |
|||
} |
|||
|
|||
func NewMetricsTracer(reg prometheus.Registerer) MetricsTracer { |
|||
metricshelper.RegisterCollectors(reg, requestsCompleted) |
|||
return &metricsTracer{} |
|||
} |
|||
|
|||
func (m *metricsTracer) CompletedRequest(e EventDialRequestCompleted) { |
|||
labels := metricshelper.GetStringSlice() |
|||
defer metricshelper.PutStringSlice(labels) |
|||
|
|||
errStr := getErrString(e.Error) |
|||
|
|||
dialData := "false" |
|||
if e.DialDataRequired { |
|||
dialData = "true" |
|||
} |
|||
|
|||
var ip, transport string |
|||
if e.DialedAddr != nil { |
|||
ip = getIPOrDNSVersion(e.DialedAddr) |
|||
transport = metricshelper.GetTransport(e.DialedAddr) |
|||
} |
|||
|
|||
*labels = append(*labels, |
|||
errStr, |
|||
pb.DialResponse_ResponseStatus_name[int32(e.ResponseStatus)], |
|||
pb.DialStatus_name[int32(e.DialStatus)], |
|||
dialData, |
|||
ip, |
|||
transport, |
|||
) |
|||
requestsCompleted.WithLabelValues(*labels...).Inc() |
|||
} |
|||
|
|||
func getIPOrDNSVersion(a ma.Multiaddr) string { |
|||
if a == nil { |
|||
return "" |
|||
} |
|||
res := "unknown" |
|||
ma.ForEach(a, func(c ma.Component) bool { |
|||
switch c.Protocol().Code { |
|||
case ma.P_IP4: |
|||
res = "ip4" |
|||
case ma.P_IP6: |
|||
res = "ip6" |
|||
case ma.P_DNS, ma.P_DNSADDR: |
|||
res = "dns" |
|||
case ma.P_DNS4: |
|||
res = "dns4" |
|||
case ma.P_DNS6: |
|||
res = "dns6" |
|||
} |
|||
return false |
|||
}) |
|||
return res |
|||
} |
|||
|
|||
func getErrString(e error) string { |
|||
var errStr string |
|||
switch e { |
|||
case nil: |
|||
errStr = "nil" |
|||
case errBadRequest, errDialDataRefused, errResourceLimitExceeded: |
|||
errStr = e.Error() |
|||
default: |
|||
errStr = "other" |
|||
} |
|||
return errStr |
|||
} |
@ -0,0 +1,51 @@ |
|||
package autonatv2 |
|||
|
|||
import ( |
|||
"errors" |
|||
"math/rand" |
|||
"testing" |
|||
|
|||
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pb" |
|||
ma "github.com/multiformats/go-multiaddr" |
|||
"github.com/prometheus/client_golang/prometheus" |
|||
) |
|||
|
|||
func TestMetricsNoAllocNoCover(t *testing.T) { |
|||
mt := NewMetricsTracer(prometheus.DefaultRegisterer) |
|||
respStatuses := []pb.DialResponse_ResponseStatus{ |
|||
pb.DialResponse_E_DIAL_REFUSED, |
|||
pb.DialResponse_OK, |
|||
} |
|||
dialStatuses := []pb.DialStatus{ |
|||
pb.DialStatus_OK, |
|||
pb.DialStatus_E_DIAL_BACK_ERROR, |
|||
} |
|||
errs := []error{ |
|||
nil, |
|||
errBadRequest, |
|||
errDialDataRefused, |
|||
errors.New("write failed"), |
|||
} |
|||
addrs := []ma.Multiaddr{ |
|||
nil, |
|||
ma.StringCast("/ip4/1.2.3.4/udp/1/quic-v1"), |
|||
ma.StringCast("/ip4/1.1.1.1/tcp/1/"), |
|||
} |
|||
tests := map[string]func(){ |
|||
"CompletedRequest": func() { |
|||
mt.CompletedRequest(EventDialRequestCompleted{ |
|||
Error: errs[rand.Intn(len(errs))], |
|||
ResponseStatus: respStatuses[rand.Intn(len(respStatuses))], |
|||
DialStatus: dialStatuses[rand.Intn(len(dialStatuses))], |
|||
DialDataRequired: rand.Intn(2) == 1, |
|||
DialedAddr: addrs[rand.Intn(len(addrs))], |
|||
}) |
|||
}, |
|||
} |
|||
for method, f := range tests { |
|||
allocs := testing.AllocsPerRun(10000, f) |
|||
if allocs > 0 { |
|||
t.Fatalf("%s alloc test failed expected 0 received %0.2f", method, allocs) |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue