Skip to content

Commit

Permalink
Fix client metric labels
Browse files Browse the repository at this point in the history
The docs on metric labels suggests that they should probably be strings,
and all others I can find are strings, and so these ought to be also.
Otherwise, some of the exporters/processors have to handle things
specifically, and not all of these come out as nice as could be when you
`str()` them.

I've also made sure to use the `StatusCode` name, as that's the
interesting thing.

Finally, there's no need to report specifically that `error=false`, so
I've removed that tag.
  • Loading branch information
alertedsnake committed Dec 24, 2020
1 parent c0223f3 commit 6fc7f26
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,32 @@ def __init__(self, meter, span_kind):

def record_bytes_in(self, bytes_in, method):
if self._meter:
labels = {"method": method}
labels = {"rpc.method": method}
self._bytes_in.add(bytes_in, labels)

def record_bytes_out(self, bytes_out, method):
if self._meter:
labels = {"method": method}
labels = {"rpc.method": method}
self._bytes_out.add(bytes_out, labels)

@contextmanager
def record_latency(self, method):
start_time = time()
labels = {"method": method, "status_code": grpc.StatusCode.OK}
labels = {
"rpc.system": "grpc",
"rpc.grpc.method": method,
"rpc.status_code": grpc.StatusCode.OK.name,
}
try:
yield labels
except grpc.RpcError as exc:
if self._meter:
# pylint: disable=no-member
labels["status_code"] = exc.code()
labels["rpc.status_code"] = exc.code().name
self._error_count.add(1, labels)
labels["error"] = True
labels["error"] = "true"
raise
finally:
if self._meter:
if "error" not in labels:
labels["error"] = False
elapsed_time = (time() - start_time) * 1000
self._duration.record(elapsed_time, labels)

0 comments on commit 6fc7f26

Please sign in to comment.