Skip to content

Commit

Permalink
Adds formatting to send to grafana.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwils committed Jun 20, 2024
1 parent e765446 commit a8a8ec5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
26 changes: 26 additions & 0 deletions canvas_sdk/utils/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from time import time


def get_duration_ms(start_time: time) -> int:
return int((time() - start_time) * 1000)


LINE_PROTOCOL_TRANSLATION = str.maketrans(
{
",": r"\,",
"=": r"\=",
" ": r"\ ",
":": r"__",
}
)


def tags_to_line_protocol(tags: dict[str, Any]) -> str:
"""Generate a tags string compatible with the InfluxDB line protocol.
See: https://docs.influxdata.com/influxdb/v1.1/write_protocols/line_protocol_tutorial/
"""
return ",".join(
f"{tag_name}={str(tag_value).translate(LINE_PROTOCOL_TRANSLATION)}"
for tag_name, tag_value in tags.items()
)
5 changes: 0 additions & 5 deletions canvas_sdk/utils/timing.py

This file was deleted.

8 changes: 3 additions & 5 deletions plugin_runner/plugin_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from sandbox import Sandbox

from canvas_sdk.events import Event, EventResponse, EventType
from canvas_sdk.utils.timing import get_duration_ms
from canvas_sdk.utils.stats import get_duration_ms, tags_to_line_protocol
from generated.messages.plugins_pb2 import ReloadPluginsRequest, ReloadPluginsResponse
from generated.services.plugin_runner_pb2_grpc import (
PluginRunnerServicer,
Expand Down Expand Up @@ -73,11 +73,9 @@ async def HandleEvent(self, request: Event, context):
compute_duration = get_duration_ms(compute_start_time)
log.info(f"{plugin_name}.compute() completed ({compute_duration} ms)")
self.statsd_client.timing(
f"plugins.protocol_duration_ms,plugin={plugin_name}",
f"plugins.protocol_duration_ms,{tags_to_line_protocol({"plugin": plugin_name})}",
delta=compute_duration,
)
# TODO - remove this, just for testing
log.info("Sent to statsd")
except Exception as e:
log.error(traceback.format_exception(e))
continue
Expand All @@ -88,7 +86,7 @@ async def HandleEvent(self, request: Event, context):
if relevant_plugins:
log.info(f"Responded to Event {event_name} ({event_duration} ms)")
self.statsd_client.timing(
f"plugins.event_duration_ms,event={event_name}",
f"plugins.event_duration_ms,{tags_to_line_protocol({"event": event_name})}",
delta=event_duration,
)
yield EventResponse(success=True, effects=effect_list)
Expand Down

0 comments on commit a8a8ec5

Please sign in to comment.