2021-06-28 09:20:23 -04:00
package metrics
import (
2021-10-30 19:19:03 -04:00
"context"
2022-11-25 17:24:34 -04:00
"fmt"
2023-04-19 12:09:03 -04:00
"time"
2021-10-30 19:19:03 -04:00
2022-11-09 15:53:01 -04:00
"github.com/waku-org/go-waku/waku/v2/utils"
2021-06-28 09:20:23 -04:00
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
2022-01-18 14:17:06 -04:00
"go.uber.org/zap"
2021-06-28 09:20:23 -04:00
)
var (
2023-04-19 12:09:03 -04:00
WakuVersion = stats . Int64 ( "waku_version" , "" , stats . UnitDimensionless )
Messages = stats . Int64 ( "node_messages" , "Number of messages received" , stats . UnitDimensionless )
2023-04-25 14:48:47 -04:00
MessageSize = stats . Int64 ( "waku_histogram_message_size" , "message size histogram in kB" , stats . UnitDimensionless )
Peers = stats . Int64 ( "peers" , "Number of connected peers" , stats . UnitDimensionless )
Dials = stats . Int64 ( "dials" , "Number of peer dials" , stats . UnitDimensionless )
2023-04-19 12:09:03 -04:00
LegacyFilterMessages = stats . Int64 ( "legacy_filter_messages" , "Number of legacy filter messages" , stats . UnitDimensionless )
LegacyFilterSubscribers = stats . Int64 ( "legacy_filter_subscribers" , "Number of legacy filter subscribers" , stats . UnitDimensionless )
LegacyFilterSubscriptions = stats . Int64 ( "legacy_filter_subscriptions" , "Number of legacy filter subscriptions" , stats . UnitDimensionless )
LegacyFilterErrors = stats . Int64 ( "legacy_filter_errors" , "Number of errors in legacy filter protocol" , stats . UnitDimensionless )
FilterMessages = stats . Int64 ( "filter_messages" , "Number of filter messages" , stats . UnitDimensionless )
FilterRequests = stats . Int64 ( "filter_requests" , "Number of filter requests" , stats . UnitDimensionless )
FilterSubscriptions = stats . Int64 ( "filter_subscriptions" , "Number of filter subscriptions" , stats . UnitDimensionless )
FilterErrors = stats . Int64 ( "filter_errors" , "Number of errors in filter protocol" , stats . UnitDimensionless )
FilterRequestDurationSeconds = stats . Int64 ( "filter_request_duration_seconds" , "Duration of Filter Subscribe Requests" , stats . UnitSeconds )
FilterHandleMessageDurationSeconds = stats . Int64 ( "filter_handle_msessageduration_seconds" , "Duration to Push Message to Filter Subscribers" , stats . UnitSeconds )
2023-04-19 16:54:33 -04:00
StoreErrors = stats . Int64 ( "errors" , "Number of errors in store protocol" , stats . UnitDimensionless )
StoreQueries = stats . Int64 ( "store_queries" , "Number of store queries" , stats . UnitDimensionless )
2023-04-27 09:44:58 -04:00
ArchiveMessages = stats . Int64 ( "waku_archive_messages" , "Number of historical messages" , stats . UnitDimensionless )
ArchiveErrors = stats . Int64 ( "waku_archive_errors" , "Number of errors in archive protocol" , stats . UnitDimensionless )
ArchiveInsertDurationSeconds = stats . Int64 ( "waku_archive_insert_duration_seconds" , "Message insertion duration" , stats . UnitSeconds )
ArchiveQueryDurationSeconds = stats . Int64 ( "waku_archive_query_duration_seconds" , "History query duration" , stats . UnitSeconds )
2023-04-19 12:09:03 -04:00
LightpushMessages = stats . Int64 ( "lightpush_messages" , "Number of messages sent via lightpush protocol" , stats . UnitDimensionless )
LightpushErrors = stats . Int64 ( "errors" , "Number of errors in lightpush protocol" , stats . UnitDimensionless )
PeerExchangeError = stats . Int64 ( "errors" , "Number of errors in peer exchange protocol" , stats . UnitDimensionless )
2023-04-19 16:54:33 -04:00
DnsDiscoveryNodes = stats . Int64 ( "dnsdisc_nodes" , "Number of discovered nodes in dns discovert" , stats . UnitDimensionless )
DnsDiscoveryErrors = stats . Int64 ( "dnsdisc_errors" , "Number of errors in dns discovery" , stats . UnitDimensionless )
DiscV5Errors = stats . Int64 ( "discv5_errors" , "Number of errors in discv5" , stats . UnitDimensionless )
2021-06-28 09:20:23 -04:00
)
var (
2022-11-25 17:24:34 -04:00
KeyType , _ = tag . NewKey ( "type" )
ErrorType , _ = tag . NewKey ( "error_type" )
GitVersion , _ = tag . NewKey ( "git_version" )
2021-06-28 09:20:23 -04:00
)
var (
2021-10-16 17:50:49 -04:00
PeersView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_connected_peers" ,
2021-10-16 17:50:49 -04:00
Measure : Peers ,
Description : "Number of connected peers" ,
Aggregation : view . Sum ( ) ,
}
DialsView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_peers_dials" ,
2021-10-16 17:50:49 -04:00
Measure : Dials ,
Description : "Number of peer dials" ,
Aggregation : view . Count ( ) ,
}
MessageView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_node_messages" ,
2021-06-28 09:20:23 -04:00
Measure : Messages ,
2021-10-16 17:50:49 -04:00
Description : "The number of the messages received" ,
2021-06-28 09:20:23 -04:00
Aggregation : view . Count ( ) ,
}
2023-04-25 14:48:47 -04:00
MessageSizeView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_histogram_message_size" ,
2023-04-25 14:48:47 -04:00
Measure : MessageSize ,
Description : "message size histogram in kB" ,
Aggregation : view . Distribution ( 0.0 , 5.0 , 15.0 , 50.0 , 100.0 , 300.0 , 700.0 , 1000.0 ) ,
}
2023-04-19 12:09:03 -04:00
2022-11-25 16:54:11 -04:00
StoreQueriesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_store_queries" ,
2022-11-25 16:54:11 -04:00
Measure : StoreQueries ,
Description : "The number of the store queries received" ,
Aggregation : view . Count ( ) ,
}
2021-06-28 09:20:23 -04:00
StoreErrorTypesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_store_errors" ,
2021-10-30 19:19:03 -04:00
Measure : StoreErrors ,
2021-06-28 09:20:23 -04:00
Description : "The distribution of the store protocol errors" ,
Aggregation : view . Count ( ) ,
2022-07-23 08:14:49 -04:00
TagKeys : [ ] tag . Key { ErrorType } ,
2021-06-28 09:20:23 -04:00
}
2023-04-19 12:09:03 -04:00
2023-04-19 16:54:33 -04:00
ArchiveMessagesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_archive_messages" ,
2023-04-19 16:54:33 -04:00
Measure : ArchiveMessages ,
Description : "The distribution of the archive protocol messages" ,
Aggregation : view . LastValue ( ) ,
TagKeys : [ ] tag . Key { KeyType } ,
}
ArchiveErrorTypesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_archive_errors" ,
2023-04-19 16:54:33 -04:00
Measure : StoreErrors ,
Description : "Number of errors in archive protocol" ,
Aggregation : view . Count ( ) ,
TagKeys : [ ] tag . Key { ErrorType } ,
}
ArchiveInsertDurationView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_archive_insert_duration_seconds" ,
2023-04-19 16:54:33 -04:00
Measure : ArchiveInsertDurationSeconds ,
Description : "Message insertion duration" ,
Aggregation : view . Count ( ) ,
}
ArchiveQueryDurationView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_archive_query_duration_seconds" ,
2023-04-19 16:54:33 -04:00
Measure : ArchiveQueryDurationSeconds ,
Description : "History query duration" ,
Aggregation : view . Count ( ) ,
}
2023-04-19 12:09:03 -04:00
LegacyFilterSubscriptionsView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_legacy_filter_subscriptions" ,
2023-04-19 12:09:03 -04:00
Measure : LegacyFilterSubscriptions ,
Description : "The number of legacy filter subscriptions" ,
Aggregation : view . Count ( ) ,
}
LegacyFilterSubscribersView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_legacy_filter_subscribers" ,
2023-04-19 12:09:03 -04:00
Measure : LegacyFilterSubscribers ,
Description : "The number of legacy filter subscribers" ,
Aggregation : view . LastValue ( ) ,
}
LegacyFilterMessagesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_legacy_filter_messages" ,
2023-04-19 12:09:03 -04:00
Measure : LegacyFilterMessages ,
Description : "The distribution of the legacy filter protocol messages received" ,
Aggregation : view . Count ( ) ,
TagKeys : [ ] tag . Key { KeyType } ,
}
LegacyFilterErrorTypesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_legacy_filter_errors" ,
2023-04-19 12:09:03 -04:00
Measure : LegacyFilterErrors ,
Description : "The distribution of the legacy filter protocol errors" ,
Aggregation : view . Count ( ) ,
TagKeys : [ ] tag . Key { ErrorType } ,
}
FilterSubscriptionsView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_filter_subscriptions" ,
2023-04-19 12:09:03 -04:00
Measure : FilterSubscriptions ,
Description : "The number of filter subscriptions" ,
Aggregation : view . Count ( ) ,
}
FilterRequestsView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_filter_requests" ,
2023-04-19 12:09:03 -04:00
Measure : FilterRequests ,
Description : "The number of filter requests" ,
Aggregation : view . Count ( ) ,
}
FilterMessagesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_filter_messages" ,
2023-04-19 12:09:03 -04:00
Measure : FilterMessages ,
Description : "The distribution of the filter protocol messages received" ,
Aggregation : view . Count ( ) ,
TagKeys : [ ] tag . Key { KeyType } ,
}
FilterErrorTypesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_filter_errors" ,
2023-04-19 12:09:03 -04:00
Measure : FilterErrors ,
Description : "The distribution of the filter protocol errors" ,
Aggregation : view . Count ( ) ,
TagKeys : [ ] tag . Key { ErrorType } ,
}
2023-04-19 16:54:33 -04:00
2023-04-19 12:09:03 -04:00
FilterRequestDurationView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_filter_request_duration_seconds" ,
2023-04-19 12:09:03 -04:00
Measure : FilterRequestDurationSeconds ,
Description : "Duration of Filter Subscribe Requests" ,
Aggregation : view . Count ( ) ,
}
FilterHandleMessageDurationView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_filter_handle_msessageduration_seconds" ,
2023-04-19 12:09:03 -04:00
Measure : FilterHandleMessageDurationSeconds ,
Description : "Duration to Push Message to Filter Subscribers" ,
Aggregation : view . Count ( ) ,
}
LightpushMessagesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_lightpush_messages" ,
2023-04-19 16:54:33 -04:00
Measure : LightpushMessages ,
2023-04-19 12:09:03 -04:00
Description : "The distribution of the lightpush protocol messages" ,
Aggregation : view . LastValue ( ) ,
TagKeys : [ ] tag . Key { KeyType } ,
}
2021-10-30 19:19:03 -04:00
LightpushErrorTypesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_lightpush_errors" ,
2021-10-30 19:19:03 -04:00
Measure : LightpushErrors ,
Description : "The distribution of the lightpush protocol errors" ,
Aggregation : view . Count ( ) ,
2022-06-13 14:36:04 -04:00
TagKeys : [ ] tag . Key { ErrorType } ,
2021-10-30 19:19:03 -04:00
}
2022-11-25 17:24:34 -04:00
VersionView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_version" ,
2022-11-25 17:24:34 -04:00
Measure : WakuVersion ,
Description : "The gowaku version" ,
Aggregation : view . LastValue ( ) ,
TagKeys : [ ] tag . Key { GitVersion } ,
}
2023-04-19 12:09:03 -04:00
DnsDiscoveryNodesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_dnsdisc_discovered" ,
2023-04-19 12:09:03 -04:00
Measure : DnsDiscoveryNodes ,
Description : "The number of nodes discovered via DNS discovery" ,
Aggregation : view . Count ( ) ,
}
DnsDiscoveryErrorTypesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_dnsdisc_errors" ,
2023-04-19 12:09:03 -04:00
Measure : DnsDiscoveryErrors ,
Description : "The distribution of the dns discovery protocol errors" ,
Aggregation : view . Count ( ) ,
TagKeys : [ ] tag . Key { ErrorType } ,
}
2023-04-19 16:54:33 -04:00
DiscV5ErrorTypesView = & view . View {
2023-04-27 09:44:58 -04:00
Name : "waku_discv5_errors" ,
2023-04-19 16:54:33 -04:00
Measure : DiscV5Errors ,
Description : "The distribution of the discv5 protocol errors" ,
Aggregation : view . Count ( ) ,
TagKeys : [ ] tag . Key { ErrorType } ,
}
2021-06-28 09:20:23 -04:00
)
2021-10-30 19:19:03 -04:00
2022-12-17 13:15:55 -04: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 14:17:06 -04:00
utils . Logger ( ) . Error ( "failed to record with tags" , zap . Error ( err ) )
2021-10-30 19:19:03 -04:00
}
}
2023-04-19 12:09:03 -04:00
func RecordLightpushMessage ( ctx context . Context , tagType string ) {
if err := stats . RecordWithTags ( ctx , [ ] tag . Mutator { tag . Insert ( KeyType , tagType ) } , LightpushMessages . M ( 1 ) ) ; err != nil {
utils . Logger ( ) . Error ( "failed to record with tags" , zap . Error ( err ) )
}
}
2022-12-17 13:15:55 -04:00
func RecordLightpushError ( ctx context . Context , tagType string ) {
recordWithTags ( ctx , ErrorType , tagType , LightpushErrors . M ( 1 ) )
}
2023-04-19 12:09:03 -04:00
func RecordLegacyFilterError ( ctx context . Context , tagType string ) {
recordWithTags ( ctx , ErrorType , tagType , LegacyFilterErrors . M ( 1 ) )
}
2023-04-19 16:54:33 -04:00
func RecordArchiveError ( ctx context . Context , tagType string ) {
recordWithTags ( ctx , ErrorType , tagType , ArchiveErrors . M ( 1 ) )
}
2023-04-19 12:09:03 -04:00
func RecordFilterError ( ctx context . Context , tagType string ) {
recordWithTags ( ctx , ErrorType , tagType , FilterErrors . M ( 1 ) )
}
func RecordFilterRequest ( ctx context . Context , tagType string , duration time . Duration ) {
if err := stats . RecordWithTags ( ctx , [ ] tag . Mutator { tag . Insert ( KeyType , tagType ) } , FilterRequests . M ( 1 ) ) ; err != nil {
utils . Logger ( ) . Error ( "failed to record with tags" , zap . Error ( err ) )
}
FilterRequestDurationSeconds . M ( int64 ( duration . Seconds ( ) ) )
}
func RecordFilterMessage ( ctx context . Context , tagType string , len int ) {
if err := stats . RecordWithTags ( ctx , [ ] tag . Mutator { tag . Insert ( KeyType , tagType ) } , FilterMessages . M ( int64 ( len ) ) ) ; err != nil {
utils . Logger ( ) . Error ( "failed to record with tags" , zap . Error ( err ) )
}
}
func RecordLegacyFilterMessage ( ctx context . Context , tagType string , len int ) {
if err := stats . RecordWithTags ( ctx , [ ] tag . Mutator { tag . Insert ( KeyType , tagType ) } , LegacyFilterMessages . M ( int64 ( len ) ) ) ; err != nil {
utils . Logger ( ) . Error ( "failed to record with tags" , zap . Error ( err ) )
}
}
2022-10-23 09:13:43 -04:00
func RecordPeerExchangeError ( ctx context . Context , tagType string ) {
2022-12-17 13:15:55 -04:00
recordWithTags ( ctx , ErrorType , tagType , PeerExchangeError . M ( 1 ) )
2022-10-23 09:13:43 -04:00
}
2023-04-19 12:09:03 -04:00
func RecordDnsDiscoveryError ( ctx context . Context , tagType string ) {
recordWithTags ( ctx , ErrorType , tagType , DnsDiscoveryErrors . M ( 1 ) )
}
2023-04-19 16:54:33 -04:00
func RecordDiscV5Error ( ctx context . Context , tagType string ) {
recordWithTags ( ctx , ErrorType , tagType , DiscV5Errors . M ( 1 ) )
}
func RecordArchiveMessage ( ctx context . Context , tagType string , len int ) {
if err := stats . RecordWithTags ( ctx , [ ] tag . Mutator { tag . Insert ( KeyType , tagType ) } , ArchiveMessages . M ( int64 ( len ) ) ) ; err != nil {
2022-01-18 14:17:06 -04:00
utils . Logger ( ) . Error ( "failed to record with tags" , zap . Error ( err ) )
2021-10-30 19:19:03 -04:00
}
}
2022-11-25 16:54:11 -04:00
func RecordStoreQuery ( ctx context . Context ) {
stats . Record ( ctx , StoreQueries . M ( 1 ) )
}
2021-10-30 19:19:03 -04:00
func RecordStoreError ( ctx context . Context , tagType string ) {
2022-12-17 13:15:55 -04:00
recordWithTags ( ctx , ErrorType , tagType , StoreErrors . M ( 1 ) )
2021-10-30 19:19:03 -04:00
}
2022-11-25 17:24:34 -04:00
func RecordVersion ( ctx context . Context , version string , commit string ) {
v := fmt . Sprintf ( "%s-%s" , version , commit )
2022-12-17 13:15:55 -04:00
recordWithTags ( ctx , GitVersion , v , WakuVersion . M ( 1 ) )
2022-11-25 17:24:34 -04:00
}