Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Unified Diff: infra_libs/ts_mon/common/helpers.py

Issue 2213143002: Add infra_libs as a bootstrap dependency. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Removed the ugly import hack Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: infra_libs/ts_mon/common/helpers.py
diff --git a/infra_libs/ts_mon/common/helpers.py b/infra_libs/ts_mon/common/helpers.py
deleted file mode 100644
index 80a35846c20349dd3bbdb84d3cb37d9c65608614..0000000000000000000000000000000000000000
--- a/infra_libs/ts_mon/common/helpers.py
+++ /dev/null
@@ -1,53 +0,0 @@
-# Copyright 2015 The Chromium Authors. All rights reserved.
-# Use of this source code is governed by a BSD-style license that can be
-# found in the LICENSE file.
-
-"""Helper classes that make it easier to instrument code for monitoring."""
-
-
-class ScopedIncrementCounter(object):
- """Increment a counter when the wrapped code exits.
-
- The counter will be given a 'status' = 'success' or 'failure' label whose
- value will be set to depending on whether the wrapped code threw an exception.
-
- Example:
-
- mycounter = Counter('foo/stuff_done')
- with ScopedIncrementCounter(mycounter):
- DoStuff()
-
- To set a custom status label and status value:
-
- mycounter = Counter('foo/http_requests')
- with ScopedIncrementCounter(mycounter, 'response_code') as sc:
- status = MakeHttpRequest()
- sc.set_status(status) # This custom status now won't be overwritten if
- # the code later raises an exception.
- """
-
- def __init__(self, counter, label='status', success_value='success',
- failure_value='failure'):
- self.counter = counter
- self.label = label
- self.success_value = success_value
- self.failure_value = failure_value
- self.status = None
-
- def set_failure(self):
- self.set_status(self.failure_value)
-
- def set_status(self, status):
- self.status = status
-
- def __enter__(self):
- self.status = None
- return self
-
- def __exit__(self, exc_type, exc_value, traceback):
- if self.status is None:
- if exc_type is None:
- self.status = self.success_value
- else:
- self.status = self.failure_value
- self.counter.increment({self.label: self.status})

Powered by Google App Engine
This is Rietveld 408576698