Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert utcnow change #400

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/scout_apm/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from celery.signals import before_task_publish, task_postrun, task_prerun

import scout_apm.core
from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from scout_apm.core.tracked_request import TrackedRequest


def before_publish_callback(headers=None, properties=None, **kwargs):
if "scout_task_start" not in headers:
headers["scout_task_start"] = datetime_to_timestamp(dt.datetime.now(tz=utc))
headers["scout_task_start"] = datetime_to_timestamp(dt.datetime.utcnow())


def prerun_callback(task=None, **kwargs):
Expand All @@ -21,7 +21,7 @@ def prerun_callback(task=None, **kwargs):

start = getattr(task.request, "scout_task_start", None)
if start is not None:
now = datetime_to_timestamp(dt.datetime.now(tz=utc))
now = datetime_to_timestamp(dt.datetime.utcnow())
try:
queue_time = now - start
except TypeError:
Expand Down
25 changes: 3 additions & 22 deletions src/scout_apm/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,15 @@ def decorated(*args, **kwds):
# Python 2.x
import Queue as queue


if sys.version_info[0] >= 3:
utc = dt.timezone.utc
else:

class UTC(dt.tzinfo):
_offset = dt.timedelta(0)

def utcoffset(self, dt):
return self._offset

def dst(self, dt):
return self._offset

def tzname(self, dt):
return "UTC"

utc = UTC()

# datetime_to_timestamp converts a UTC datetime to a unix timestamp
# datetime_to_timestamp converts a naive UTC datetime to a unix timestamp
if sys.version_info >= (3, 3):

def datetime_to_timestamp(datetime_obj):
return datetime_obj.timestamp()
return datetime_obj.replace(tzinfo=dt.timezone.utc).timestamp()


else:
_EPOCH = dt.datetime(1970, 1, 1, tzinfo=utc)
_EPOCH = dt.datetime(1970, 1, 1)

def datetime_to_timestamp(datetime_obj):
return (datetime_obj - _EPOCH).total_seconds()
Expand Down
5 changes: 2 additions & 3 deletions src/scout_apm/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import sys
from os import getpid

from scout_apm.compat import utc
from scout_apm.core.commands import ApplicationEvent
from scout_apm.core.config import scout_config
from scout_apm.core.socket import CoreAgentSocket
Expand All @@ -17,7 +16,7 @@ def report_app_metadata():
event_type="scout.metadata",
event_value=get_metadata(),
source="Pid: " + str(getpid()),
timestamp=dt.datetime.now(tz=utc),
timestamp=dt.datetime.utcnow(),
)
)

Expand All @@ -26,7 +25,7 @@ def get_metadata():
data = {
"language": "python",
"language_version": "{}.{}.{}".format(*sys.version_info[:3]),
"server_time": dt.datetime.now(tz=utc).isoformat() + "Z",
"server_time": dt.datetime.utcnow().isoformat() + "Z",
"framework": scout_config.value("framework"),
"framework_version": scout_config.value("framework_version"),
"environment": "",
Expand Down
6 changes: 2 additions & 4 deletions src/scout_apm/core/samplers/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import psutil

from scout_apm.compat import utc

logger = logging.getLogger(__name__)


Expand All @@ -17,15 +15,15 @@ class Cpu(object):
human_name = "Process CPU"

def __init__(self):
self.last_run = dt.datetime.now(tz=utc)
self.last_run = dt.datetime.utcnow()
self.last_cpu_times = psutil.Process().cpu_times()
self.num_processors = psutil.cpu_count()
if self.num_processors is None:
logger.debug("Could not determine CPU count - assuming there is one.")
self.num_processors = 1

def run(self):
now = dt.datetime.now(tz=utc)
now = dt.datetime.utcnow()
process = psutil.Process() # get a handle on the current process
cpu_times = process.cpu_times()

Expand Down
3 changes: 1 addition & 2 deletions src/scout_apm/core/samplers/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import threading

from scout_apm.compat import utc
from scout_apm.core.commands import ApplicationEvent
from scout_apm.core.samplers.cpu import Cpu
from scout_apm.core.samplers.memory import Memory
Expand Down Expand Up @@ -61,7 +60,7 @@ def run(self):
event = ApplicationEvent(
event_value=event_value,
event_type=event_type,
timestamp=dt.datetime.now(tz=utc),
timestamp=dt.datetime.utcnow(),
source="Pid: " + str(os.getpid()),
)
CoreAgentSocket.instance().send(event)
Expand Down
11 changes: 5 additions & 6 deletions src/scout_apm/core/tracked_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
from uuid import uuid4

from scout_apm.compat import utc
from scout_apm.core import backtrace, objtrace
from scout_apm.core.commands import BatchCommand
from scout_apm.core.n_plus_one_call_set import NPlusOneCallSet
Expand Down Expand Up @@ -43,7 +42,7 @@ def instance(cls):

def __init__(self):
self.request_id = "req-" + str(uuid4())
self.start_time = dt.datetime.now(tz=utc)
self.start_time = dt.datetime.utcnow()
self.end_time = None
self.active_spans = []
self.complete_spans = []
Expand Down Expand Up @@ -119,7 +118,7 @@ def current_span(self):
def finish(self):
logger.debug("Stopping request: %s", self.request_id)
if self.end_time is None:
self.end_time = dt.datetime.now(tz=utc)
self.end_time = dt.datetime.utcnow()
if self.is_real_request:
self.tag("mem_delta", self._get_mem_delta())
if not self.is_ignored():
Expand Down Expand Up @@ -168,7 +167,7 @@ def __init__(
should_capture_backtrace=True,
):
self.span_id = "span-" + str(uuid4())
self.start_time = dt.datetime.now(tz=utc)
self.start_time = dt.datetime.utcnow()
self.end_time = None
self.request_id = request_id
self.operation = operation
Expand All @@ -187,7 +186,7 @@ def __repr__(self):
)

def stop(self):
self.end_time = dt.datetime.now(tz=utc)
self.end_time = dt.datetime.utcnow()
self.end_objtrace_counts = objtrace.get_counts()

def tag(self, key, value):
Expand All @@ -203,7 +202,7 @@ def duration(self):
return (self.end_time - self.start_time).total_seconds()
else:
# Current, running duration
return (dt.datetime.now(tz=utc) - self.start_time).total_seconds()
return (dt.datetime.utcnow() - self.start_time).total_seconds()

# Add any interesting annotations to the span. Assumes that we are in the
# process of stopping this span.
Expand Down
5 changes: 1 addition & 4 deletions src/scout_apm/rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from rq.job import Job

import scout_apm.core
from scout_apm.compat import utc
from scout_apm.core.tracked_request import TrackedRequest

install_attempted = False
Expand Down Expand Up @@ -62,9 +61,7 @@ def wrap_perform(wrapped, instance, args, kwargs):
tracked_request.is_real_request = True
tracked_request.tag("task_id", instance.get_id())
tracked_request.tag("queue", instance.origin)
# rq stores UTC datetime as naive, we always use timezone aware datetimes
enqueued_at_utc = instance.enqueued_at.replace(tzinfo=utc)
queue_time = (dt.datetime.now(tz=utc) - enqueued_at_utc).total_seconds()
queue_time = (dt.datetime.utcnow() - instance.enqueued_at).total_seconds()
tracked_request.tag("queue_time", queue_time)
tracked_request.start_span(operation="Job/{}".format(instance.func_name))
try:
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from scout_apm.api import Config
from scout_apm.bottle import ScoutPlugin
from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from tests.integration.util import (
parametrize_filtered_params,
parametrize_queue_time_header_name,
Expand Down Expand Up @@ -109,7 +109,7 @@ def test_user_ip(headers, client_address, expected, tracked_requests):
@parametrize_queue_time_header_name
def test_queue_time(header_name, tracked_requests):
# Not testing floats due to Python 2/3 rounding differences
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/", headers={header_name: str("t=") + str(queue_start)}
Expand All @@ -123,7 +123,7 @@ def test_queue_time(header_name, tracked_requests):


def test_amazon_queue_time(tracked_requests):
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from django.test.utils import override_settings
from webtest import TestApp

from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from scout_apm.core.config import scout_config
from scout_apm.django.instruments.sql import ensure_sql_instrumented
from scout_apm.django.instruments.template import ensure_templates_instrumented
Expand Down Expand Up @@ -654,7 +654,7 @@ def test_old_style_urlconf(tracked_requests):
@parametrize_queue_time_header_name
def test_queue_time(header_name, tracked_requests):
# Not testing floats due to Python 2/3 rounding differences
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/", headers={header_name: str("t=") + str(queue_start)}
Expand All @@ -668,7 +668,7 @@ def test_queue_time(header_name, tracked_requests):


def test_amazon_queue_time(tracked_requests):
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from webtest import TestApp

from scout_apm.api import Config
from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from scout_apm.falcon import ScoutMiddleware
from tests.integration.util import (
parametrize_filtered_params,
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_filtered_params(params, expected_path, tracked_requests):
@parametrize_queue_time_header_name
def test_queue_time(header_name, tracked_requests):
# Not testing floats due to Python 2/3 rounding differences
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/", headers={header_name: str("t=") + str(queue_start)}
Expand All @@ -207,7 +207,7 @@ def test_queue_time(header_name, tracked_requests):


def test_amazon_queue_time(tracked_requests):
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from webtest import TestApp

from scout_apm.api import Config
from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from scout_apm.flask import ScoutApm
from tests.integration.util import (
parametrize_filtered_params,
Expand Down Expand Up @@ -114,7 +114,7 @@ def test_user_ip(headers, client_address, expected, tracked_requests):
@parametrize_queue_time_header_name
def test_queue_time(header_name, tracked_requests):
# Not testing floats due to Python 2/3 rounding differences
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/", headers={header_name: str("t=") + str(queue_start)}
Expand All @@ -128,7 +128,7 @@ def test_queue_time(header_name, tracked_requests):


def test_amazon_queue_time(tracked_requests):
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_nameko.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from werkzeug.wrappers import Response

from scout_apm.api import Config
from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from scout_apm.nameko import ScoutReporter
from tests.integration.util import (
parametrize_filtered_params,
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_user_ip(headers, client_address, expected, tracked_requests):
@parametrize_queue_time_header_name
def test_queue_time(header_name, tracked_requests):
# Not testing floats due to Python 2/3 rounding differences
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/", headers={header_name: str("t=") + str(queue_start)}
Expand All @@ -150,7 +150,7 @@ def test_queue_time(header_name, tracked_requests):


def test_amazon_queue_time(tracked_requests):
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from webtest import TestApp

from scout_apm.api import Config
from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from tests.integration.util import (
parametrize_filtered_params,
parametrize_queue_time_header_name,
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_user_ip(headers, client_address, expected, tracked_requests):
@parametrize_queue_time_header_name
def test_queue_time(header_name, tracked_requests):
# Not testing floats due to Python 2/3 rounding differences
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/", headers={header_name: str("t=") + str(queue_start)}
Expand All @@ -122,7 +122,7 @@ def test_queue_time(header_name, tracked_requests):


def test_amazon_queue_time(tracked_requests):
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
response = TestApp(app).get(
"/",
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/test_starlette_py36plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from scout_apm.api import Config
from scout_apm.async_.starlette import ScoutMiddleware
from scout_apm.compat import datetime_to_timestamp, utc
from scout_apm.compat import datetime_to_timestamp
from tests.integration.util import (
parametrize_filtered_params,
parametrize_queue_time_header_name,
Expand Down Expand Up @@ -219,7 +219,7 @@ async def test_user_ip(headers, client_address, expected, tracked_requests):
@async_test
async def test_queue_time(header_name, tracked_requests):
# Not testing floats due to Python 2/3 rounding differences
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
communicator = ApplicationCommunicator(
app,
Expand All @@ -238,7 +238,7 @@ async def test_queue_time(header_name, tracked_requests):

@async_test
async def test_amazon_queue_time(tracked_requests):
queue_start = int(datetime_to_timestamp(dt.datetime.now(tz=utc))) - 2
queue_start = int(datetime_to_timestamp(dt.datetime.utcnow())) - 2
with app_with_scout() as app:
communicator = ApplicationCommunicator(
app,
Expand Down
Loading