Skip to content

Commit dcca613

Browse files
nonsensegbalint
authored andcommitted
swarm: initial instrumentation (#15969)
* swarm: initial instrumentation with go-metrics * swarm: initialise metrics collection and add ResettingTimer to HTTP requests * swarm: update metrics flags names. remove redundant Timer. * swarm: rename method for periodically updating gauges * swarm: finalise metrics after feedback * swarm/network: always init kad metrics containers * swarm/network: off-by-one index in metrics containers * swarm, metrics: resolved conflicts
1 parent b677a07 commit dcca613

16 files changed

Lines changed: 381 additions & 10 deletions

File tree

‎cmd/swarm/main.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/ethereum/go-ethereum/params"
4444
"github.com/ethereum/go-ethereum/swarm"
4545
bzzapi "github.com/ethereum/go-ethereum/swarm/api"
46+
swarmmetrics "github.com/ethereum/go-ethereum/swarm/metrics"
4647

4748
"gopkg.in/urfave/cli.v1"
4849
)
@@ -359,9 +360,14 @@ DEPRECATED: use 'swarm db clean'.
359360
DeprecatedEnsAddrFlag,
360361
}
361362
app.Flags = append(app.Flags, debug.Flags...)
363+
app.Flags = append(app.Flags, swarmmetrics.Flags...)
362364
app.Before = func(ctx *cli.Context) error {
363365
runtime.GOMAXPROCS(runtime.NumCPU())
364-
return debug.Setup(ctx)
366+
if err := debug.Setup(ctx); err != nil {
367+
return err
368+
}
369+
swarmmetrics.Setup(ctx)
370+
return nil
365371
}
366372
app.After = func(ctx *cli.Context) error {
367373
debug.Exit()

‎metrics/metrics.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ func init() {
3535
Enabled = true
3636
}
3737
}
38-
//exp.Exp(DefaultRegistry)
3938
}
4039

4140
// CollectProcessMetrics periodically collects various metrics about the running

‎swarm/api/api.go‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,31 @@ import (
3232

3333
"github.com/ethereum/go-ethereum/common"
3434
"github.com/ethereum/go-ethereum/log"
35+
"github.com/ethereum/go-ethereum/metrics"
3536
"github.com/ethereum/go-ethereum/swarm/storage"
3637
)
3738

3839
var hashMatcher = regexp.MustCompile("^[0-9A-Fa-f]{64}")
3940

41+
//setup metrics
42+
var (
43+
apiResolveCount = metrics.NewRegisteredCounter("api.resolve.count", nil)
44+
apiResolveFail = metrics.NewRegisteredCounter("api.resolve.fail", nil)
45+
apiPutCount = metrics.NewRegisteredCounter("api.put.count", nil)
46+
apiPutFail = metrics.NewRegisteredCounter("api.put.fail", nil)
47+
apiGetCount = metrics.NewRegisteredCounter("api.get.count", nil)
48+
apiGetNotFound = metrics.NewRegisteredCounter("api.get.notfound", nil)
49+
apiGetHttp300 = metrics.NewRegisteredCounter("api.get.http.300", nil)
50+
apiModifyCount = metrics.NewRegisteredCounter("api.modify.count", nil)
51+
apiModifyFail = metrics.NewRegisteredCounter("api.modify.fail", nil)
52+
apiAddFileCount = metrics.NewRegisteredCounter("api.addfile.count", nil)
53+
apiAddFileFail = metrics.NewRegisteredCounter("api.addfile.fail", nil)
54+
apiRmFileCount = metrics.NewRegisteredCounter("api.removefile.count", nil)
55+
apiRmFileFail = metrics.NewRegisteredCounter("api.removefile.fail", nil)
56+
apiAppendFileCount = metrics.NewRegisteredCounter("api.appendfile.count", nil)
57+
apiAppendFileFail = metrics.NewRegisteredCounter("api.appendfile.fail", nil)
58+
)
59+
4060
type Resolver interface {
4161
Resolve(string) (common.Hash, error)
4262
}
@@ -155,6 +175,7 @@ type ErrResolve error
155175

156176
// DNS Resolver
157177
func (self *Api) Resolve(uri *URI) (storage.Key, error) {
178+
apiResolveCount.Inc(1)
158179
log.Trace(fmt.Sprintf("Resolving : %v", uri.Addr))
159180

160181
// if the URI is immutable, check if the address is a hash
@@ -169,6 +190,7 @@ func (self *Api) Resolve(uri *URI) (storage.Key, error) {
169190
// if DNS is not configured, check if the address is a hash
170191
if self.dns == nil {
171192
if !isHash {
193+
apiResolveFail.Inc(1)
172194
return nil, fmt.Errorf("no DNS to resolve name: %q", uri.Addr)
173195
}
174196
return common.Hex2Bytes(uri.Addr), nil
@@ -179,23 +201,27 @@ func (self *Api) Resolve(uri *URI) (storage.Key, error) {
179201
if err == nil {
180202
return resolved[:], nil
181203
} else if !isHash {
204+
apiResolveFail.Inc(1)
182205
return nil, err
183206
}
184207
return common.Hex2Bytes(uri.Addr), nil
185208
}
186209

187210
// Put provides singleton manifest creation on top of dpa store
188211
func (self *Api) Put(content, contentType string) (storage.Key, error) {
212+
apiPutCount.Inc(1)
189213
r := strings.NewReader(content)
190214
wg := &sync.WaitGroup{}
191215
key, err := self.dpa.Store(r, int64(len(content)), wg, nil)
192216
if err != nil {
217+
apiPutFail.Inc(1)
193218
return nil, err
194219
}
195220
manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType)
196221
r = strings.NewReader(manifest)
197222
key, err = self.dpa.Store(r, int64(len(manifest)), wg, nil)
198223
if err != nil {
224+
apiPutFail.Inc(1)
199225
return nil, err
200226
}
201227
wg.Wait()
@@ -206,8 +232,10 @@ func (self *Api) Put(content, contentType string) (storage.Key, error) {
206232
// to resolve basePath to content using dpa retrieve
207233
// it returns a section reader, mimeType, status and an error
208234
func (self *Api) Get(key storage.Key, path string) (reader storage.LazySectionReader, mimeType string, status int, err error) {
235+
apiGetCount.Inc(1)
209236
trie, err := loadManifest(self.dpa, key, nil)
210237
if err != nil {
238+
apiGetNotFound.Inc(1)
211239
status = http.StatusNotFound
212240
log.Warn(fmt.Sprintf("loadManifestTrie error: %v", err))
213241
return
@@ -221,6 +249,7 @@ func (self *Api) Get(key storage.Key, path string) (reader storage.LazySectionRe
221249
key = common.Hex2Bytes(entry.Hash)
222250
status = entry.Status
223251
if status == http.StatusMultipleChoices {
252+
apiGetHttp300.Inc(1)
224253
return
225254
} else {
226255
mimeType = entry.ContentType
@@ -229,16 +258,19 @@ func (self *Api) Get(key storage.Key, path string) (reader storage.LazySectionRe
229258
}
230259
} else {
231260
status = http.StatusNotFound
261+
apiGetNotFound.Inc(1)
232262
err = fmt.Errorf("manifest entry for '%s' not found", path)
233263
log.Warn(fmt.Sprintf("%v", err))
234264
}
235265
return
236266
}
237267

238268
func (self *Api) Modify(key storage.Key, path, contentHash, contentType string) (storage.Key, error) {
269+
apiModifyCount.Inc(1)
239270
quitC := make(chan bool)
240271
trie, err := loadManifest(self.dpa, key, quitC)
241272
if err != nil {
273+
apiModifyFail.Inc(1)
242274
return nil, err
243275
}
244276
if contentHash != "" {
@@ -253,19 +285,23 @@ func (self *Api) Modify(key storage.Key, path, contentHash, contentType string)
253285
}
254286

255287
if err := trie.recalcAndStore(); err != nil {
288+
apiModifyFail.Inc(1)
256289
return nil, err
257290
}
258291
return trie.hash, nil
259292
}
260293

261294
func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver bool) (storage.Key, string, error) {
295+
apiAddFileCount.Inc(1)
262296

263297
uri, err := Parse("bzz:/" + mhash)
264298
if err != nil {
299+
apiAddFileFail.Inc(1)
265300
return nil, "", err
266301
}
267302
mkey, err := self.Resolve(uri)
268303
if err != nil {
304+
apiAddFileFail.Inc(1)
269305
return nil, "", err
270306
}
271307

@@ -284,16 +320,19 @@ func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver
284320

285321
mw, err := self.NewManifestWriter(mkey, nil)
286322
if err != nil {
323+
apiAddFileFail.Inc(1)
287324
return nil, "", err
288325
}
289326

290327
fkey, err := mw.AddEntry(bytes.NewReader(content), entry)
291328
if err != nil {
329+
apiAddFileFail.Inc(1)
292330
return nil, "", err
293331
}
294332

295333
newMkey, err := mw.Store()
296334
if err != nil {
335+
apiAddFileFail.Inc(1)
297336
return nil, "", err
298337

299338
}
@@ -303,13 +342,16 @@ func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver
303342
}
304343

305344
func (self *Api) RemoveFile(mhash, path, fname string, nameresolver bool) (string, error) {
345+
apiRmFileCount.Inc(1)
306346

307347
uri, err := Parse("bzz:/" + mhash)
308348
if err != nil {
349+
apiRmFileFail.Inc(1)
309350
return "", err
310351
}
311352
mkey, err := self.Resolve(uri)
312353
if err != nil {
354+
apiRmFileFail.Inc(1)
313355
return "", err
314356
}
315357

@@ -320,16 +362,19 @@ func (self *Api) RemoveFile(mhash, path, fname string, nameresolver bool) (strin
320362

321363
mw, err := self.NewManifestWriter(mkey, nil)
322364
if err != nil {
365+
apiRmFileFail.Inc(1)
323366
return "", err
324367
}
325368

326369
err = mw.RemoveEntry(filepath.Join(path, fname))
327370
if err != nil {
371+
apiRmFileFail.Inc(1)
328372
return "", err
329373
}
330374

331375
newMkey, err := mw.Store()
332376
if err != nil {
377+
apiRmFileFail.Inc(1)
333378
return "", err
334379

335380
}
@@ -338,6 +383,7 @@ func (self *Api) RemoveFile(mhash, path, fname string, nameresolver bool) (strin
338383
}
339384

340385
func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, content []byte, oldKey storage.Key, offset int64, addSize int64, nameresolver bool) (storage.Key, string, error) {
386+
apiAppendFileCount.Inc(1)
341387

342388
buffSize := offset + addSize
343389
if buffSize < existingSize {
@@ -366,10 +412,12 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
366412

367413
uri, err := Parse("bzz:/" + mhash)
368414
if err != nil {
415+
apiAppendFileFail.Inc(1)
369416
return nil, "", err
370417
}
371418
mkey, err := self.Resolve(uri)
372419
if err != nil {
420+
apiAppendFileFail.Inc(1)
373421
return nil, "", err
374422
}
375423

@@ -380,11 +428,13 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
380428

381429
mw, err := self.NewManifestWriter(mkey, nil)
382430
if err != nil {
431+
apiAppendFileFail.Inc(1)
383432
return nil, "", err
384433
}
385434

386435
err = mw.RemoveEntry(filepath.Join(path, fname))
387436
if err != nil {
437+
apiAppendFileFail.Inc(1)
388438
return nil, "", err
389439
}
390440

@@ -398,11 +448,13 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
398448

399449
fkey, err := mw.AddEntry(io.Reader(combinedReader), entry)
400450
if err != nil {
451+
apiAppendFileFail.Inc(1)
401452
return nil, "", err
402453
}
403454

404455
newMkey, err := mw.Store()
405456
if err != nil {
457+
apiAppendFileFail.Inc(1)
406458
return nil, "", err
407459

408460
}
@@ -412,6 +464,7 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
412464
}
413465

414466
func (self *Api) BuildDirectoryTree(mhash string, nameresolver bool) (key storage.Key, manifestEntryMap map[string]*manifestTrieEntry, err error) {
467+
415468
uri, err := Parse("bzz:/" + mhash)
416469
if err != nil {
417470
return nil, nil, err

‎swarm/api/http/error.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ import (
2929
"time"
3030

3131
"github.com/ethereum/go-ethereum/log"
32+
"github.com/ethereum/go-ethereum/metrics"
3233
"github.com/ethereum/go-ethereum/swarm/api"
3334
)
3435

3536
//templateMap holds a mapping of an HTTP error code to a template
3637
var templateMap map[int]*template.Template
3738

39+
//metrics variables
40+
var (
41+
htmlCounter = metrics.NewRegisteredCounter("api.http.errorpage.html.count", nil)
42+
jsonCounter = metrics.NewRegisteredCounter("api.http.errorpage.json.count", nil)
43+
)
44+
3845
//parameters needed for formatting the correct HTML page
3946
type ErrorParams struct {
4047
Msg string
@@ -132,6 +139,7 @@ func respond(w http.ResponseWriter, r *http.Request, params *ErrorParams) {
132139

133140
//return a HTML page
134141
func respondHtml(w http.ResponseWriter, params *ErrorParams) {
142+
htmlCounter.Inc(1)
135143
err := params.template.Execute(w, params)
136144
if err != nil {
137145
log.Error(err.Error())
@@ -140,6 +148,7 @@ func respondHtml(w http.ResponseWriter, params *ErrorParams) {
140148

141149
//return JSON
142150
func respondJson(w http.ResponseWriter, params *ErrorParams) {
151+
jsonCounter.Inc(1)
143152
w.Header().Set("Content-Type", "application/json")
144153
json.NewEncoder(w).Encode(params)
145154
}

0 commit comments

Comments
 (0)