Skip to content

Commit

Permalink
Centralize datetime formatting for agent
Browse files Browse the repository at this point in the history
  • Loading branch information
lancetarn committed Nov 1, 2024
1 parent 0530934 commit a499525
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/scout_apm/core/agent/commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# coding=utf-8

import datetime as dt
import logging
import re

Expand All @@ -10,6 +11,17 @@
key_regex = re.compile(r"^[a-zA-Z0-9]{20}$")


def format_dt_for_core_agent(event_time: dt.datetime) -> str:
"""
Returns expected format for Core Agent compatibility. Agent expects UTC datetime
with 'Z' suffix. Coerce any tz-aware datetime to UTC just in case.
"""
# if we somehow got a non-UTC datetime, convert it to UTC
if event_time.tzinfo is not None:
event_time = event_time.astimezone(dt.timezone.utc)
return event_time.strftime("%Y-%m-%dT%H:%M:%SZ")


class Register(object):
__slots__ = ("app", "key", "hostname")

Expand Down Expand Up @@ -49,7 +61,7 @@ def __init__(self, timestamp, request_id, span_id, parent, operation):
def message(self):
return {
"StartSpan": {
"timestamp": self.timestamp.isoformat() + "Z",
"timestamp": format_dt_for_core_agent(self.timestamp),
"request_id": self.request_id,
"span_id": self.span_id,
"parent_id": self.parent,
Expand All @@ -69,7 +81,7 @@ def __init__(self, timestamp, request_id, span_id):
def message(self):
return {
"StopSpan": {
"timestamp": self.timestamp.isoformat() + "Z",
"timestamp": format_dt_for_core_agent(self.timestamp),
"request_id": self.request_id,
"span_id": self.span_id,
}
Expand All @@ -86,7 +98,7 @@ def __init__(self, timestamp, request_id):
def message(self):
return {
"StartRequest": {
"timestamp": self.timestamp.isoformat() + "Z",
"timestamp": format_dt_for_core_agent(self.timestamp),
"request_id": self.request_id,
}
}
Expand All @@ -102,7 +114,7 @@ def __init__(self, timestamp, request_id):
def message(self):
return {
"FinishRequest": {
"timestamp": self.timestamp.isoformat() + "Z",
"timestamp": format_dt_for_core_agent(self.timestamp),
"request_id": self.request_id,
}
}
Expand All @@ -121,7 +133,7 @@ def __init__(self, timestamp, request_id, span_id, tag, value):
def message(self):
return {
"TagSpan": {
"timestamp": self.timestamp.isoformat() + "Z",
"timestamp": format_dt_for_core_agent(self.timestamp),
"request_id": self.request_id,
"span_id": self.span_id,
"tag": self.tag,
Expand All @@ -142,7 +154,7 @@ def __init__(self, timestamp, request_id, tag, value):
def message(self):
return {
"TagRequest": {
"timestamp": self.timestamp.isoformat() + "Z",
"timestamp": format_dt_for_core_agent(self.timestamp),
"request_id": self.request_id,
"tag": self.tag,
"value": self.value,
Expand All @@ -162,7 +174,7 @@ def __init__(self, event_type, event_value, source, timestamp):
def message(self):
return {
"ApplicationEvent": {
"timestamp": self.timestamp.isoformat() + "Z",
"timestamp": format_dt_for_core_agent(self.timestamp),
"event_type": self.event_type,
"event_value": self.event_value,
"source": self.source,
Expand Down
4 changes: 2 additions & 2 deletions src/scout_apm/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
from os import getpid

from scout_apm.core.agent.commands import ApplicationEvent
from scout_apm.core.agent.commands import ApplicationEvent, format_dt_for_core_agent
from scout_apm.core.agent.socket import CoreAgentSocketThread
from scout_apm.core.config import scout_config

Expand All @@ -24,7 +24,7 @@ def get_metadata():
data = {
"language": "python",
"language_version": "{}.{}.{}".format(*sys.version_info[:3]),
"server_time": dt.datetime.now(dt.timezone.utc).isoformat() + "Z",
"server_time": format_dt_for_core_agent(dt.datetime.now(dt.timezone.utc)),
"framework": scout_config.value("framework"),
"framework_version": scout_config.value("framework_version"),
"environment": "",
Expand Down

0 comments on commit a499525

Please sign in to comment.