Skip to content

Commit

Permalink
Fix datetime.utcnow deprecation warnings (#1162)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp authored Feb 20, 2024
1 parent 8c8cc9a commit 64da7f8
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ariadne/contrib/tracing/apollotracing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from inspect import iscoroutinefunction
from typing import Any, List, Optional, cast

Expand All @@ -20,6 +19,19 @@ def perf_counter_ns() -> int:
return int(perf_counter() * NS_IN_SECOND)


try:
from datetime import UTC, datetime # type: ignore[attr-defined]

def utc_now():
return datetime.now(UTC)

except ImportError:
from datetime import datetime

def utc_now():
return datetime.utcnow()


TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"


Expand All @@ -33,7 +45,7 @@ def __init__(self, trace_default_resolver: bool = False) -> None:
self._totals = None

def request_started(self, context: ContextValue):
self.start_date = datetime.utcnow()
self.start_date = utc_now()
self.start_timestamp = perf_counter_ns()

def resolve(self, next_: Resolver, obj: Any, info: GraphQLResolveInfo, **kwargs):
Expand Down Expand Up @@ -93,7 +105,7 @@ def get_totals(self):
def _get_totals(self):
return {
"start": self.start_date,
"end": datetime.utcnow(),
"end": utc_now(),
"duration": perf_counter_ns() - self.start_timestamp,
"resolvers": self.resolvers,
}
Expand Down

0 comments on commit 64da7f8

Please sign in to comment.