-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Optional | ||
from urllib.parse import urlparse | ||
import os | ||
|
||
from ddtrace import Span, patch, tracer, config | ||
from ddtrace.filters import TraceFilter | ||
from loguru import logger | ||
|
||
|
||
def configure_apm(enable_apm: bool, service_name: str): | ||
"""optionally enable datadog APM / profiler.""" | ||
if enable_apm: | ||
logger.info("Enabling DataDog APM") | ||
|
||
class FilterRootPathTraces(TraceFilter): | ||
def process_trace(self, trace: list[Span]) -> Optional[list[Span]]: | ||
for span in trace: | ||
if span.parent_id is not None: | ||
return trace | ||
|
||
if url := span.get_tag("http.url"): | ||
parsed_url = urlparse(url) | ||
|
||
if parsed_url.path == "/": | ||
return None | ||
|
||
return trace | ||
|
||
patch(fastapi=True, redis=True, asyncpg=True, aiohttp=True, celery=True) | ||
tracer.configure( | ||
settings={ | ||
"FILTERS": [ | ||
FilterRootPathTraces(), | ||
] | ||
} | ||
) | ||
|
||
# Override service name | ||
config.fastapi["service_name"] = service_name | ||
config.fastapi["request_span_name"] = f"{service_name}.request" | ||
|
||
else: | ||
logger.info("DataDog APM disabled") | ||
tracer.configure(enabled=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
from typing import Optional | ||
|
||
import datadog | ||
from loguru import logger | ||
|
||
|
||
def configure_metrics( | ||
enable_metrics: bool, statsd_host: str, statsd_port: int, namespace: str = "" | ||
): | ||
if not enable_metrics: | ||
logger.info("DogStatsD metrics disabled") | ||
return | ||
else: | ||
logger.info( | ||
"DogStatsD metrics enabled; statsd: {host}:{port}", | ||
host=statsd_host, | ||
port=statsd_port, | ||
) | ||
|
||
if not namespace: | ||
namespace = os.environ.get("DD_SERVICE", "") | ||
|
||
namespace = namespace.lower().replace("-", "_") | ||
datadog.initialize( | ||
statsd_host=statsd_host, | ||
statsd_port=statsd_port, | ||
statsd_namespace=f"permit.{namespace}", | ||
) | ||
|
||
|
||
def _format_tags(tags: Optional[dict[str, str]]) -> Optional[list[str]]: | ||
if not tags: | ||
return None | ||
|
||
return [f"{k}:{v}" for k, v in tags.items()] | ||
|
||
|
||
def increment(metric: str, tags: Optional[dict[str, str]] = None): | ||
datadog.statsd.increment(metric, tags=_format_tags(tags)) | ||
|
||
|
||
def decrement(metric: str, tags: Optional[dict[str, str]] = None): | ||
datadog.statsd.decrement(metric, tags=_format_tags(tags)) | ||
|
||
|
||
def gauge(metric: str, value: float, tags: Optional[dict[str, str]] = None): | ||
datadog.statsd.gauge(metric, value, tags=_format_tags(tags)) | ||
|
||
|
||
def event(title: str, message: str, tags: Optional[dict[str, str]] = None): | ||
datadog.statsd.event(title=title, message=message, tags=_format_tags(tags)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.