ipfs-cluster/informer/disk/disk_test.go
Adrian Lanzafame 3b3f786d68
add opencensus tracing and metrics
This commit adds support for OpenCensus tracing
and metrics collection. This required support for
context.Context propogation throughout the cluster
codebase, and in particular, the ipfscluster component
interfaces.

The tracing propogates across RPC and HTTP boundaries.
The current default tracing backend is Jaeger.

The metrics currently exports the metrics exposed by
the opencensus http plugin as well as the pprof metrics
to a prometheus endpoint for scraping.
The current default metrics backend is Prometheus.

Metrics are currently exposed by default due to low
overhead, can be turned off if desired, whereas tracing
is off by default as it has a much higher performance
overhead, though the extent of the performance hit can be
adjusted with smaller sampling rates.

License: MIT
Signed-off-by: Adrian Lanzafame <adrianlanzafame92@gmail.com>
2019-02-04 18:53:21 +10:00

118 lines
2.3 KiB
Go

package disk
import (
"context"
"errors"
"testing"
rpc "github.com/libp2p/go-libp2p-gorpc"
"github.com/ipfs/ipfs-cluster/api"
"github.com/ipfs/ipfs-cluster/test"
)
type badRPCService struct {
}
func badRPCClient(t *testing.T) *rpc.Client {
s := rpc.NewServer(nil, "mock")
c := rpc.NewClientWithServer(nil, "mock", s)
err := s.RegisterName("Cluster", &badRPCService{})
if err != nil {
t.Fatal(err)
}
return c
}
func (mock *badRPCService) IPFSRepoStat(ctx context.Context, in struct{}, out *api.IPFSRepoStat) error {
return errors.New("fake error")
}
func Test(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
cfg.Default()
inf, err := NewInformer(cfg)
if err != nil {
t.Fatal(err)
}
defer inf.Shutdown(ctx)
m := inf.GetMetric(ctx)
if m.Valid {
t.Error("metric should be invalid")
}
inf.SetClient(test.NewMockRPCClient(t))
m = inf.GetMetric(ctx)
if !m.Valid {
t.Error("metric should be valid")
}
}
func TestFreeSpace(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
cfg.Default()
cfg.Type = MetricFreeSpace
inf, err := NewInformer(cfg)
if err != nil {
t.Fatal(err)
}
defer inf.Shutdown(ctx)
m := inf.GetMetric(ctx)
if m.Valid {
t.Error("metric should be invalid")
}
inf.SetClient(test.NewMockRPCClient(t))
m = inf.GetMetric(ctx)
if !m.Valid {
t.Error("metric should be valid")
}
// The mock client reports 100KB and 2 pins of 1 KB
if m.Value != "98000" {
t.Error("bad metric value")
}
}
func TestRepoSize(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
cfg.Default()
cfg.Type = MetricRepoSize
inf, err := NewInformer(cfg)
if err != nil {
t.Fatal(err)
}
defer inf.Shutdown(ctx)
m := inf.GetMetric(ctx)
if m.Valid {
t.Error("metric should be invalid")
}
inf.SetClient(test.NewMockRPCClient(t))
m = inf.GetMetric(ctx)
if !m.Valid {
t.Error("metric should be valid")
}
// The mock client reports 100KB and 2 pins of 1 KB
if m.Value != "2000" {
t.Error("bad metric value")
}
}
func TestWithErrors(t *testing.T) {
ctx := context.Background()
cfg := &Config{}
cfg.Default()
inf, err := NewInformer(cfg)
if err != nil {
t.Fatal(err)
}
defer inf.Shutdown(ctx)
inf.SetClient(badRPCClient(t))
m := inf.GetMetric(ctx)
if m.Valid {
t.Errorf("metric should be invalid")
}
}