Skip to content

Commit bb72408

Browse files
acudnonsense
authored andcommitted
cmd/swarm, metrics, swarm/api/client, swarm/storage, swarm/metrics, swarm/api/http: add instrumentation (#18274)
1 parent b2aac65 commit bb72408

File tree

8 files changed

+368
-82
lines changed

8 files changed

+368
-82
lines changed

cmd/swarm/swarm-smoke/feed_upload_and_sync.go

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package main
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/md5"
67
"fmt"
78
"io"
89
"io/ioutil"
910
"net/http"
11+
"net/http/httptrace"
1012
"os"
1113
"os/exec"
1214
"strings"
@@ -16,8 +18,13 @@ import (
1618
"github.com/ethereum/go-ethereum/common/hexutil"
1719
"github.com/ethereum/go-ethereum/crypto"
1820
"github.com/ethereum/go-ethereum/log"
21+
"github.com/ethereum/go-ethereum/metrics"
22+
"github.com/ethereum/go-ethereum/swarm/api/client"
23+
"github.com/ethereum/go-ethereum/swarm/spancontext"
1924
"github.com/ethereum/go-ethereum/swarm/storage/feed"
25+
"github.com/ethereum/go-ethereum/swarm/testutil"
2026
colorable "github.com/mattn/go-colorable"
27+
opentracing "github.com/opentracing/opentracing-go"
2128
"github.com/pborman/uuid"
2229
cli "gopkg.in/urfave/cli.v1"
2330
)
@@ -26,11 +33,29 @@ const (
2633
feedRandomDataLength = 8
2734
)
2835

29-
// TODO: retrieve with manifest + extract repeating code
3036
func cliFeedUploadAndSync(c *cli.Context) error {
31-
37+
metrics.GetOrRegisterCounter("feed-and-sync", nil).Inc(1)
3238
log.Root().SetHandler(log.CallerFileHandler(log.LvlFilterHandler(log.Lvl(verbosity), log.StreamHandler(colorable.NewColorableStderr(), log.TerminalFormat(true)))))
3339

40+
errc := make(chan error)
41+
go func() {
42+
errc <- feedUploadAndSync(c)
43+
}()
44+
45+
select {
46+
case err := <-errc:
47+
if err != nil {
48+
metrics.GetOrRegisterCounter("feed-and-sync.fail", nil).Inc(1)
49+
}
50+
return err
51+
case <-time.After(time.Duration(timeout) * time.Second):
52+
metrics.GetOrRegisterCounter("feed-and-sync.timeout", nil).Inc(1)
53+
return fmt.Errorf("timeout after %v sec", timeout)
54+
}
55+
}
56+
57+
// TODO: retrieve with manifest + extract repeating code
58+
func feedUploadAndSync(c *cli.Context) error {
3459
defer func(now time.Time) { log.Info("total time", "time", time.Since(now), "size (kb)", filesize) }(time.Now())
3560

3661
generateEndpoints(scheme, cluster, appName, from, to)
@@ -204,12 +229,12 @@ func cliFeedUploadAndSync(c *cli.Context) error {
204229
log.Info("all endpoints synced random data successfully")
205230

206231
// upload test file
207-
log.Info("uploading to " + endpoints[0] + " and syncing")
232+
seed := int(time.Now().UnixNano() / 1e6)
233+
log.Info("feed uploading to "+endpoints[0]+" and syncing", "seed", seed)
208234

209-
f, cleanup := generateRandomFile(filesize * 1000)
210-
defer cleanup()
235+
randomBytes := testutil.RandomBytes(seed, filesize*1000)
211236

212-
hash, err := upload(f, endpoints[0])
237+
hash, err := upload(&randomBytes, endpoints[0])
213238
if err != nil {
214239
return err
215240
}
@@ -218,7 +243,7 @@ func cliFeedUploadAndSync(c *cli.Context) error {
218243
return err
219244
}
220245
multihashHex := hexutil.Encode(hashBytes)
221-
fileHash, err := digest(f)
246+
fileHash, err := digest(bytes.NewReader(randomBytes))
222247
if err != nil {
223248
return err
224249
}
@@ -284,14 +309,37 @@ func cliFeedUploadAndSync(c *cli.Context) error {
284309
}
285310

286311
func fetchFeed(topic string, user string, endpoint string, original []byte, ruid string) error {
312+
ctx, sp := spancontext.StartSpan(context.Background(), "feed-and-sync.fetch")
313+
defer sp.Finish()
314+
287315
log.Trace("sleeping", "ruid", ruid)
288316
time.Sleep(3 * time.Second)
289317

290318
log.Trace("http get request (feed)", "ruid", ruid, "api", endpoint, "topic", topic, "user", user)
291-
res, err := http.Get(endpoint + "/bzz-feed:/?topic=" + topic + "&user=" + user)
319+
320+
var tn time.Time
321+
reqUri := endpoint + "/bzz-feed:/?topic=" + topic + "&user=" + user
322+
req, _ := http.NewRequest("GET", reqUri, nil)
323+
324+
opentracing.GlobalTracer().Inject(
325+
sp.Context(),
326+
opentracing.HTTPHeaders,
327+
opentracing.HTTPHeadersCarrier(req.Header))
328+
329+
trace := client.GetClientTrace("feed-and-sync - http get", "feed-and-sync", ruid, &tn)
330+
331+
req = req.WithContext(httptrace.WithClientTrace(ctx, trace))
332+
transport := http.DefaultTransport
333+
334+
//transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
335+
336+
tn = time.Now()
337+
res, err := transport.RoundTrip(req)
292338
if err != nil {
339+
log.Error(err.Error(), "ruid", ruid)
293340
return err
294341
}
342+
295343
log.Trace("http get response (feed)", "ruid", ruid, "api", endpoint, "topic", topic, "user", user, "code", res.StatusCode, "len", res.ContentLength)
296344

297345
if res.StatusCode != 200 {

cmd/swarm/swarm-smoke/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,38 @@
1717
package main
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"sort"
2223

24+
"github.com/ethereum/go-ethereum/cmd/utils"
25+
gethmetrics "github.com/ethereum/go-ethereum/metrics"
26+
"github.com/ethereum/go-ethereum/metrics/influxdb"
27+
swarmmetrics "github.com/ethereum/go-ethereum/swarm/metrics"
28+
"github.com/ethereum/go-ethereum/swarm/tracing"
29+
2330
"github.com/ethereum/go-ethereum/log"
2431

2532
cli "gopkg.in/urfave/cli.v1"
2633
)
2734

35+
var (
36+
gitCommit string // Git SHA1 commit hash of the release (set via linker flags)
37+
)
38+
2839
var (
2940
endpoints []string
3041
includeLocalhost bool
3142
cluster string
3243
appName string
3344
scheme string
3445
filesize int
46+
syncDelay int
3547
from int
3648
to int
3749
verbosity int
50+
timeout int
51+
single bool
3852
)
3953

4054
func main() {
@@ -85,14 +99,42 @@ func main() {
8599
Usage: "file size for generated random file in KB",
86100
Destination: &filesize,
87101
},
102+
cli.IntFlag{
103+
Name: "sync-delay",
104+
Value: 5,
105+
Usage: "duration of delay in seconds to wait for content to be synced",
106+
Destination: &syncDelay,
107+
},
88108
cli.IntFlag{
89109
Name: "verbosity",
90110
Value: 1,
91111
Usage: "verbosity",
92112
Destination: &verbosity,
93113
},
114+
cli.IntFlag{
115+
Name: "timeout",
116+
Value: 120,
117+
Usage: "timeout in seconds after which kill the process",
118+
Destination: &timeout,
119+
},
120+
cli.BoolFlag{
121+
Name: "single",
122+
Usage: "whether to fetch content from a single node or from all nodes",
123+
Destination: &single,
124+
},
94125
}
95126

127+
app.Flags = append(app.Flags, []cli.Flag{
128+
utils.MetricsEnabledFlag,
129+
swarmmetrics.MetricsInfluxDBEndpointFlag,
130+
swarmmetrics.MetricsInfluxDBDatabaseFlag,
131+
swarmmetrics.MetricsInfluxDBUsernameFlag,
132+
swarmmetrics.MetricsInfluxDBPasswordFlag,
133+
swarmmetrics.MetricsInfluxDBHostTagFlag,
134+
}...)
135+
136+
app.Flags = append(app.Flags, tracing.Flags...)
137+
96138
app.Commands = []cli.Command{
97139
{
98140
Name: "upload_and_sync",
@@ -111,9 +153,38 @@ func main() {
111153
sort.Sort(cli.FlagsByName(app.Flags))
112154
sort.Sort(cli.CommandsByName(app.Commands))
113155

156+
app.Before = func(ctx *cli.Context) error {
157+
tracing.Setup(ctx)
158+
return nil
159+
}
160+
161+
app.After = func(ctx *cli.Context) error {
162+
return emitMetrics(ctx)
163+
}
164+
114165
err := app.Run(os.Args)
115166
if err != nil {
116167
log.Error(err.Error())
168+
117169
os.Exit(1)
118170
}
119171
}
172+
173+
func emitMetrics(ctx *cli.Context) error {
174+
if gethmetrics.Enabled {
175+
var (
176+
endpoint = ctx.GlobalString(swarmmetrics.MetricsInfluxDBEndpointFlag.Name)
177+
database = ctx.GlobalString(swarmmetrics.MetricsInfluxDBDatabaseFlag.Name)
178+
username = ctx.GlobalString(swarmmetrics.MetricsInfluxDBUsernameFlag.Name)
179+
password = ctx.GlobalString(swarmmetrics.MetricsInfluxDBPasswordFlag.Name)
180+
hosttag = ctx.GlobalString(swarmmetrics.MetricsInfluxDBHostTagFlag.Name)
181+
)
182+
return influxdb.InfluxDBWithTagsOnce(gethmetrics.DefaultRegistry, endpoint, database, username, password, "swarm-smoke.", map[string]string{
183+
"host": hosttag,
184+
"version": gitCommit,
185+
"filesize": fmt.Sprintf("%v", filesize),
186+
})
187+
}
188+
189+
return nil
190+
}

0 commit comments

Comments
 (0)