@@ -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
3839var 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+
4060type Resolver interface {
4161 Resolve (string ) (common.Hash , error )
4262}
@@ -155,6 +175,7 @@ type ErrResolve error
155175
156176// DNS Resolver
157177func (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
188211func (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
208234func (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
238268func (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
261294func (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
305344func (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
340385func (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
414466func (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
0 commit comments