2021-06-28 13:20:23 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
2021-10-30 23:19:03 +00:00
|
|
|
"context"
|
2022-11-25 21:24:34 +00:00
|
|
|
"fmt"
|
2021-10-30 23:19:03 +00:00
|
|
|
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2021-06-28 13:20:23 +00:00
|
|
|
"go.opencensus.io/stats"
|
|
|
|
"go.opencensus.io/stats/view"
|
|
|
|
"go.opencensus.io/tag"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-06-28 13:20:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-11-25 21:24:34 +00:00
|
|
|
WakuVersion = stats.Int64("waku_version", "", stats.UnitDimensionless)
|
2021-10-16 21:50:49 +00:00
|
|
|
Messages = stats.Int64("node_messages", "Number of messages received", stats.UnitDimensionless)
|
|
|
|
Peers = stats.Int64("peers", "Number of connected peers", stats.UnitDimensionless)
|
|
|
|
Dials = stats.Int64("dials", "Number of peer dials", stats.UnitDimensionless)
|
2021-06-28 13:20:23 +00:00
|
|
|
StoreMessages = stats.Int64("store_messages", "Number of historical messages", stats.UnitDimensionless)
|
|
|
|
FilterSubscriptions = stats.Int64("filter_subscriptions", "Number of filter subscriptions", stats.UnitDimensionless)
|
2021-10-30 23:19:03 +00:00
|
|
|
StoreErrors = stats.Int64("errors", "Number of errors in store protocol", stats.UnitDimensionless)
|
2022-11-25 20:54:11 +00:00
|
|
|
StoreQueries = stats.Int64("store_queries", "Number of store queries", stats.UnitDimensionless)
|
2021-10-30 23:19:03 +00:00
|
|
|
LightpushErrors = stats.Int64("errors", "Number of errors in lightpush protocol", stats.UnitDimensionless)
|
2022-10-23 13:13:43 +00:00
|
|
|
PeerExchangeError = stats.Int64("errors", "Number of errors in peer exchange protocol", stats.UnitDimensionless)
|
2021-06-28 13:20:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-11-25 21:24:34 +00:00
|
|
|
KeyType, _ = tag.NewKey("type")
|
|
|
|
ErrorType, _ = tag.NewKey("error_type")
|
|
|
|
GitVersion, _ = tag.NewKey("git_version")
|
2021-06-28 13:20:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-10-16 21:50:49 +00:00
|
|
|
PeersView = &view.View{
|
|
|
|
Name: "gowaku_connected_peers",
|
|
|
|
Measure: Peers,
|
|
|
|
Description: "Number of connected peers",
|
|
|
|
Aggregation: view.Sum(),
|
|
|
|
}
|
|
|
|
DialsView = &view.View{
|
|
|
|
Name: "gowaku_peers_dials",
|
|
|
|
Measure: Dials,
|
|
|
|
Description: "Number of peer dials",
|
|
|
|
Aggregation: view.Count(),
|
|
|
|
}
|
|
|
|
MessageView = &view.View{
|
|
|
|
Name: "gowaku_node_messages",
|
2021-06-28 13:20:23 +00:00
|
|
|
Measure: Messages,
|
2021-10-16 21:50:49 +00:00
|
|
|
Description: "The number of the messages received",
|
2021-06-28 13:20:23 +00:00
|
|
|
Aggregation: view.Count(),
|
|
|
|
}
|
2022-11-25 20:54:11 +00:00
|
|
|
StoreQueriesView = &view.View{
|
|
|
|
Name: "gowaku_store_queries",
|
|
|
|
Measure: StoreQueries,
|
|
|
|
Description: "The number of the store queries received",
|
|
|
|
Aggregation: view.Count(),
|
|
|
|
}
|
2021-10-16 21:50:49 +00:00
|
|
|
StoreMessagesView = &view.View{
|
|
|
|
Name: "gowaku_store_messages",
|
2021-06-28 13:20:23 +00:00
|
|
|
Measure: StoreMessages,
|
|
|
|
Description: "The distribution of the store protocol messages",
|
|
|
|
Aggregation: view.LastValue(),
|
2021-10-30 23:19:03 +00:00
|
|
|
TagKeys: []tag.Key{KeyType},
|
2021-06-28 13:20:23 +00:00
|
|
|
}
|
|
|
|
FilterSubscriptionsView = &view.View{
|
2021-10-16 21:50:49 +00:00
|
|
|
Name: "gowaku_filter_subscriptions",
|
2021-06-28 13:20:23 +00:00
|
|
|
Measure: FilterSubscriptions,
|
|
|
|
Description: "The number of content filter subscriptions",
|
|
|
|
Aggregation: view.LastValue(),
|
|
|
|
}
|
|
|
|
StoreErrorTypesView = &view.View{
|
2021-10-16 21:50:49 +00:00
|
|
|
Name: "gowaku_store_errors",
|
2021-10-30 23:19:03 +00:00
|
|
|
Measure: StoreErrors,
|
2021-06-28 13:20:23 +00:00
|
|
|
Description: "The distribution of the store protocol errors",
|
|
|
|
Aggregation: view.Count(),
|
2022-07-23 12:14:49 +00:00
|
|
|
TagKeys: []tag.Key{ErrorType},
|
2021-06-28 13:20:23 +00:00
|
|
|
}
|
2021-10-30 23:19:03 +00:00
|
|
|
LightpushErrorTypesView = &view.View{
|
|
|
|
Name: "gowaku_lightpush_errors",
|
|
|
|
Measure: LightpushErrors,
|
|
|
|
Description: "The distribution of the lightpush protocol errors",
|
|
|
|
Aggregation: view.Count(),
|
2022-06-13 18:36:04 +00:00
|
|
|
TagKeys: []tag.Key{ErrorType},
|
2021-10-30 23:19:03 +00:00
|
|
|
}
|
2022-11-25 21:24:34 +00:00
|
|
|
VersionView = &view.View{
|
|
|
|
Name: "gowaku_version",
|
|
|
|
Measure: WakuVersion,
|
|
|
|
Description: "The gowaku version",
|
|
|
|
Aggregation: view.LastValue(),
|
|
|
|
TagKeys: []tag.Key{GitVersion},
|
|
|
|
}
|
2021-06-28 13:20:23 +00:00
|
|
|
)
|
2021-10-30 23:19:03 +00:00
|
|
|
|
2022-12-17 17:15:55 +00:00
|
|
|
func recordWithTags(ctx context.Context, tagKey tag.Key, tagType string, ms stats.Measurement) {
|
|
|
|
if err := stats.RecordWithTags(ctx, []tag.Mutator{tag.Insert(tagKey, tagType)}, ms); err != nil {
|
2022-01-18 18:17:06 +00:00
|
|
|
utils.Logger().Error("failed to record with tags", zap.Error(err))
|
2021-10-30 23:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 17:15:55 +00:00
|
|
|
func RecordLightpushError(ctx context.Context, tagType string) {
|
|
|
|
recordWithTags(ctx, ErrorType, tagType, LightpushErrors.M(1))
|
|
|
|
}
|
|
|
|
|
2022-10-23 13:13:43 +00:00
|
|
|
func RecordPeerExchangeError(ctx context.Context, tagType string) {
|
2022-12-17 17:15:55 +00:00
|
|
|
recordWithTags(ctx, ErrorType, tagType, PeerExchangeError.M(1))
|
2022-10-23 13:13:43 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 23:19:03 +00:00
|
|
|
func RecordMessage(ctx context.Context, tagType string, len int) {
|
|
|
|
if err := stats.RecordWithTags(ctx, []tag.Mutator{tag.Insert(KeyType, tagType)}, StoreMessages.M(int64(len))); err != nil {
|
2022-01-18 18:17:06 +00:00
|
|
|
utils.Logger().Error("failed to record with tags", zap.Error(err))
|
2021-10-30 23:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:54:11 +00:00
|
|
|
func RecordStoreQuery(ctx context.Context) {
|
|
|
|
stats.Record(ctx, StoreQueries.M(1))
|
|
|
|
}
|
|
|
|
|
2021-10-30 23:19:03 +00:00
|
|
|
func RecordStoreError(ctx context.Context, tagType string) {
|
2022-12-17 17:15:55 +00:00
|
|
|
recordWithTags(ctx, ErrorType, tagType, StoreErrors.M(1))
|
2021-10-30 23:19:03 +00:00
|
|
|
}
|
2022-11-25 21:24:34 +00:00
|
|
|
|
|
|
|
func RecordVersion(ctx context.Context, version string, commit string) {
|
|
|
|
v := fmt.Sprintf("%s-%s", version, commit)
|
2022-12-17 17:15:55 +00:00
|
|
|
recordWithTags(ctx, GitVersion, v, WakuVersion.M(1))
|
2022-11-25 21:24:34 +00:00
|
|
|
}
|