2023-11-16 18:21:52 +01:00
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/metric"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UpdateChannelMetrics represents all metrics related to the UpdateChannel
|
|
|
|
type UpdateChannelMetrics struct {
|
2024-05-27 08:39:18 +02:00
|
|
|
createChannelDurationMicro metric.Int64Histogram
|
|
|
|
closeChannelDurationMicro metric.Int64Histogram
|
|
|
|
closeChannelsDurationMicro metric.Int64Histogram
|
|
|
|
closeChannels metric.Int64Histogram
|
|
|
|
sendUpdateDurationMicro metric.Int64Histogram
|
|
|
|
getAllConnectedPeersDurationMicro metric.Int64Histogram
|
|
|
|
getAllConnectedPeers metric.Int64Histogram
|
|
|
|
hasChannelDurationMicro metric.Int64Histogram
|
2023-11-16 18:21:52 +01:00
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUpdateChannelMetrics creates an instance of UpdateChannel
|
|
|
|
func NewUpdateChannelMetrics(ctx context.Context, meter metric.Meter) (*UpdateChannelMetrics, error) {
|
2024-05-27 08:39:18 +02:00
|
|
|
createChannelDurationMicro, err := meter.Int64Histogram("management.updatechannel.create.duration.micro")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
closeChannelDurationMicro, err := meter.Int64Histogram("management.updatechannel.close.one.duration.micro")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
closeChannelsDurationMicro, err := meter.Int64Histogram("management.updatechannel.close.multiple.duration.micro")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
closeChannels, err := meter.Int64Histogram("management.updatechannel.close.multiple.channels")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
sendUpdateDurationMicro, err := meter.Int64Histogram("management.updatechannel.send.duration.micro")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
getAllConnectedPeersDurationMicro, err := meter.Int64Histogram("management.updatechannel.get.all.duration.micro")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
getAllConnectedPeers, err := meter.Int64Histogram("management.updatechannel.get.all.peers")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-27 08:39:18 +02:00
|
|
|
hasChannelDurationMicro, err := meter.Int64Histogram("management.updatechannel.haschannel.duration.micro")
|
2023-11-16 18:21:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &UpdateChannelMetrics{
|
|
|
|
createChannelDurationMicro: createChannelDurationMicro,
|
|
|
|
closeChannelDurationMicro: closeChannelDurationMicro,
|
|
|
|
closeChannelsDurationMicro: closeChannelsDurationMicro,
|
|
|
|
closeChannels: closeChannels,
|
|
|
|
sendUpdateDurationMicro: sendUpdateDurationMicro,
|
|
|
|
getAllConnectedPeersDurationMicro: getAllConnectedPeersDurationMicro,
|
|
|
|
getAllConnectedPeers: getAllConnectedPeers,
|
2023-12-05 14:17:56 +01:00
|
|
|
hasChannelDurationMicro: hasChannelDurationMicro,
|
2023-11-16 18:21:52 +01:00
|
|
|
ctx: ctx,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountCreateChannelDuration counts the duration of the CreateChannel method,
|
|
|
|
// closed indicates if existing channel was closed before creation of a new one
|
|
|
|
func (metrics *UpdateChannelMetrics) CountCreateChannelDuration(duration time.Duration, closed bool) {
|
2024-05-27 08:39:18 +02:00
|
|
|
opts := metric.WithAttributeSet(attribute.NewSet(attribute.Bool("closed", closed)))
|
|
|
|
metrics.createChannelDurationMicro.Record(metrics.ctx, duration.Microseconds(), opts)
|
2023-11-16 18:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CountCloseChannelDuration counts the duration of the CloseChannel method
|
|
|
|
func (metrics *UpdateChannelMetrics) CountCloseChannelDuration(duration time.Duration) {
|
|
|
|
metrics.closeChannelDurationMicro.Record(metrics.ctx, duration.Microseconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountCloseChannelsDuration counts the duration of the CloseChannels method and the number of channels have been closed
|
|
|
|
func (metrics *UpdateChannelMetrics) CountCloseChannelsDuration(duration time.Duration, channels int) {
|
|
|
|
metrics.closeChannelsDurationMicro.Record(metrics.ctx, duration.Microseconds())
|
|
|
|
metrics.closeChannels.Record(metrics.ctx, int64(channels))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountSendUpdateDuration counts the duration of the SendUpdate method
|
|
|
|
// found indicates if peer had channel, dropped indicates if the message was dropped due channel buffer overload
|
|
|
|
func (metrics *UpdateChannelMetrics) CountSendUpdateDuration(duration time.Duration, found, dropped bool) {
|
2024-05-27 08:39:18 +02:00
|
|
|
opts := metric.WithAttributeSet(attribute.NewSet(attribute.Bool("found", found), attribute.Bool("dropped", dropped)))
|
|
|
|
metrics.sendUpdateDurationMicro.Record(metrics.ctx, duration.Microseconds(), opts)
|
2023-11-16 18:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// CountGetAllConnectedPeersDuration counts the duration of the GetAllConnectedPeers method and the number of peers have been returned
|
|
|
|
func (metrics *UpdateChannelMetrics) CountGetAllConnectedPeersDuration(duration time.Duration, peers int) {
|
|
|
|
metrics.getAllConnectedPeersDurationMicro.Record(metrics.ctx, duration.Microseconds())
|
|
|
|
metrics.getAllConnectedPeers.Record(metrics.ctx, int64(peers))
|
|
|
|
}
|
2023-12-05 14:17:56 +01:00
|
|
|
|
|
|
|
// CountHasChannelDuration counts the duration of the HasChannel method
|
|
|
|
func (metrics *UpdateChannelMetrics) CountHasChannelDuration(duration time.Duration) {
|
|
|
|
metrics.hasChannelDurationMicro.Record(metrics.ctx, duration.Microseconds())
|
|
|
|
}
|