metrics.go 4.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
package metrics

import (
	pb "github.com/libp2p/go-libp2p-kad-dht/pb"
	"go.opencensus.io/stats"
	"go.opencensus.io/stats/view"
	"go.opencensus.io/tag"
)

var (
	defaultBytesDistribution        = view.Distribution(1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296)
	defaultMillisecondsDistribution = view.Distribution(0.01, 0.05, 0.1, 0.3, 0.6, 0.8, 1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000)
)

// Keys
var (
	KeyMessageType, _ = tag.NewKey("message_type")
	KeyPeerID, _      = tag.NewKey("peer_id")
	// KeyInstanceID identifies a dht instance by the pointer address.
	// Useful for differentiating between different dhts that have the same peer id.
	KeyInstanceID, _ = tag.NewKey("instance_id")
)

// UpsertMessageType is a convenience upserts the message type
// of a pb.Message into the KeyMessageType.
func UpsertMessageType(m *pb.Message) tag.Mutator {
	return tag.Upsert(KeyMessageType, m.Type.String())
}

// Measures
var (
	ReceivedMessages       = stats.Int64("libp2p.io/dht/kad/received_messages", "Total number of messages received per RPC", stats.UnitDimensionless)
	ReceivedMessageErrors  = stats.Int64("libp2p.io/dht/kad/received_message_errors", "Total number of errors for messages received per RPC", stats.UnitDimensionless)
	ReceivedBytes          = stats.Int64("libp2p.io/dht/kad/received_bytes", "Total received bytes per RPC", stats.UnitBytes)
	InboundRequestLatency  = stats.Float64("libp2p.io/dht/kad/inbound_request_latency", "Latency per RPC", stats.UnitMilliseconds)
	OutboundRequestLatency = stats.Float64("libp2p.io/dht/kad/outbound_request_latency", "Latency per RPC", stats.UnitMilliseconds)
	SentMessages           = stats.Int64("libp2p.io/dht/kad/sent_messages", "Total number of messages sent per RPC", stats.UnitDimensionless)
	SentMessageErrors      = stats.Int64("libp2p.io/dht/kad/sent_message_errors", "Total number of errors for messages sent per RPC", stats.UnitDimensionless)
	SentRequests           = stats.Int64("libp2p.io/dht/kad/sent_requests", "Total number of requests sent per RPC", stats.UnitDimensionless)
	SentRequestErrors      = stats.Int64("libp2p.io/dht/kad/sent_request_errors", "Total number of errors for requests sent per RPC", stats.UnitDimensionless)
	SentBytes              = stats.Int64("libp2p.io/dht/kad/sent_bytes", "Total sent bytes per RPC", stats.UnitBytes)
)

Adrian Lanzafame's avatar
Adrian Lanzafame committed
44 45 46
// Views
var (
	ReceivedMessagesView = &view.View{
47 48 49
		Measure:     ReceivedMessages,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: view.Count(),
Adrian Lanzafame's avatar
Adrian Lanzafame committed
50 51
	}
	ReceivedMessageErrorsView = &view.View{
52 53 54
		Measure:     ReceivedMessageErrors,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: view.Count(),
Adrian Lanzafame's avatar
Adrian Lanzafame committed
55 56
	}
	ReceivedBytesView = &view.View{
57 58 59
		Measure:     ReceivedBytes,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: defaultBytesDistribution,
Adrian Lanzafame's avatar
Adrian Lanzafame committed
60 61
	}
	InboundRequestLatencyView = &view.View{
62 63 64
		Measure:     InboundRequestLatency,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: defaultMillisecondsDistribution,
Adrian Lanzafame's avatar
Adrian Lanzafame committed
65 66
	}
	OutboundRequestLatencyView = &view.View{
67 68 69
		Measure:     OutboundRequestLatency,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: defaultMillisecondsDistribution,
Adrian Lanzafame's avatar
Adrian Lanzafame committed
70 71
	}
	SentMessagesView = &view.View{
72 73 74
		Measure:     SentMessages,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: view.Count(),
Adrian Lanzafame's avatar
Adrian Lanzafame committed
75 76
	}
	SentMessageErrorsView = &view.View{
77 78 79
		Measure:     SentMessageErrors,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: view.Count(),
Adrian Lanzafame's avatar
Adrian Lanzafame committed
80 81
	}
	SentRequestsView = &view.View{
82 83 84
		Measure:     SentRequests,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: view.Count(),
Adrian Lanzafame's avatar
Adrian Lanzafame committed
85 86
	}
	SentRequestErrorsView = &view.View{
87 88 89
		Measure:     SentRequestErrors,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: view.Count(),
Adrian Lanzafame's avatar
Adrian Lanzafame committed
90 91
	}
	SentBytesView = &view.View{
92 93 94
		Measure:     SentBytes,
		TagKeys:     []tag.Key{KeyMessageType, KeyPeerID, KeyInstanceID},
		Aggregation: defaultBytesDistribution,
Adrian Lanzafame's avatar
Adrian Lanzafame committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	}
)

// DefaultViews with all views in it.
var DefaultViews = []*view.View{
	ReceivedMessagesView,
	ReceivedMessageErrorsView,
	ReceivedBytesView,
	InboundRequestLatencyView,
	OutboundRequestLatencyView,
	SentMessagesView,
	SentMessageErrorsView,
	SentRequestsView,
	SentRequestErrorsView,
	SentBytesView,
110
}