This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add latency & count metrics for content routing client
- Loading branch information
Showing
6 changed files
with
211 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/ipfs/go-cid" | ||
proto "github.com/ipfs/go-delegated-routing/gen/proto" | ||
ipns "github.com/ipfs/go-ipns" | ||
logging "github.com/ipfs/go-log/v2" | ||
record "github.com/libp2p/go-libp2p-record" | ||
"github.com/libp2p/go-libp2p/core/crypto" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
) | ||
|
||
var logger = logging.Logger("service/client/delegatedrouting") | ||
|
||
type DelegatedRoutingClient interface { | ||
FindProviders(ctx context.Context, key cid.Cid) ([]peer.AddrInfo, error) | ||
FindProvidersAsync(ctx context.Context, key cid.Cid) (<-chan FindProvidersAsyncResult, error) | ||
GetIPNS(ctx context.Context, id []byte) ([]byte, error) | ||
GetIPNSAsync(ctx context.Context, id []byte) (<-chan GetIPNSAsyncResult, error) | ||
PutIPNS(ctx context.Context, id []byte, record []byte) error | ||
PutIPNSAsync(ctx context.Context, id []byte, record []byte) (<-chan PutIPNSAsyncResult, error) | ||
Provide(ctx context.Context, key []cid.Cid, ttl time.Duration) (time.Duration, error) | ||
ProvideAsync(ctx context.Context, key []cid.Cid, ttl time.Duration) (<-chan time.Duration, error) | ||
} | ||
|
||
type Client struct { | ||
client proto.DelegatedRouting_Client | ||
validator record.Validator | ||
|
||
provider *Provider | ||
identity crypto.PrivKey | ||
} | ||
|
||
var _ DelegatedRoutingClient = (*Client)(nil) | ||
|
||
// NewClient creates a client. | ||
// The Provider and identity parameters are option. If they are nil, the `Provide` method will not function. | ||
func NewClient(c proto.DelegatedRouting_Client, p *Provider, identity crypto.PrivKey) (*Client, error) { | ||
if p != nil && !p.Peer.ID.MatchesPublicKey(identity.GetPublic()) { | ||
return nil, errors.New("identity does not match provider") | ||
} | ||
|
||
return &Client{ | ||
client: c, | ||
validator: ipns.Validator{}, | ||
provider: p, | ||
identity: identity, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
"go.opencensus.io/tag" | ||
) | ||
|
||
var ( | ||
defaultDurationDistribution = view.Distribution(0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000) | ||
|
||
measureDuration = stats.Float64("delegated_routing/duration", "The time to complete an entire request", stats.UnitMilliseconds) | ||
measureRequests = stats.Float64("delegated_routing/requests", "The number of requests made", stats.UnitDimensionless) | ||
|
||
keyName = tag.MustNewKey("name") | ||
keyError = tag.MustNewKey("error") | ||
|
||
durationView = &view.View{ | ||
Measure: measureDuration, | ||
TagKeys: []tag.Key{keyName, keyError}, | ||
Aggregation: defaultDurationDistribution, | ||
} | ||
requestsView = &view.View{ | ||
Measure: measureRequests, | ||
TagKeys: []tag.Key{keyName, keyError}, | ||
Aggregation: view.Sum(), | ||
} | ||
|
||
DefaultViews = []*view.View{ | ||
durationView, | ||
requestsView, | ||
} | ||
) | ||
|
||
// startMetrics begins recording metrics. | ||
// The returned function flushes the metrics when called, recording metrics about the passed error. | ||
func startMetrics(ctx context.Context, name string) (done func(err error)) { | ||
start := time.Now() | ||
|
||
return func(err error) { | ||
latency := time.Since(start) | ||
|
||
errStr := "None" | ||
if err != nil { | ||
logger.Warnw("received delegated routing error", "Error", err) | ||
if errors.Is(err, context.Canceled) { | ||
errStr = "Canceled" | ||
} else if errors.Is(err, context.DeadlineExceeded) { | ||
errStr = "DeadlineExceeded" | ||
} else { | ||
errStr = "Unknown" | ||
} | ||
} | ||
|
||
stats.RecordWithTags(ctx, | ||
[]tag.Mutator{ | ||
tag.Upsert(keyName, name), | ||
tag.Upsert(keyError, errStr), | ||
}, | ||
[]stats.Measurement{ | ||
measureDuration.M(float64(latency.Milliseconds())), | ||
measureRequests.M(1), | ||
}..., | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.