Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Replace null telemetry client with null sender
Browse files Browse the repository at this point in the history
  • Loading branch information
c-w committed Mar 7, 2019
1 parent 70fc567 commit 2322625
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions agogosml/agogosml/utils/logger.py
Expand Up @@ -5,31 +5,19 @@
from pathlib import Path
from typing import Dict
from typing import Optional
from typing import Union

import yaml
from applicationinsights import TelemetryClient
from applicationinsights.channel import AsynchronousQueue
from applicationinsights.channel import AsynchronousSender
from applicationinsights.channel import NullSender
from applicationinsights.channel import SynchronousQueue
from applicationinsights.channel import TelemetryChannel
from applicationinsights.channel import TelemetryContext
from cached_property import cached_property
from singleton_decorator import singleton


class NullTelemetryClient:
"""Null-object implementation of the TelemetryClient."""

def __init__(self):
"""Null-object implementation of the TelemetryClient."""

def track_trace(self, name, properties=None, severity=None):
"""Do nothing."""

def track_event(self, name, properties=None, measurements=None):
"""Do nothing."""


@singleton
class Logger:
"""Logger."""
Expand Down Expand Up @@ -65,20 +53,23 @@ def _logger(self) -> logging.Logger:
return logging.getLogger(self.name)

@cached_property
def _telemetry(self) -> Union[TelemetryClient, NullTelemetryClient]:
def _telemetry(self) -> TelemetryClient:
"""Create the telemetry client."""
queue_class = AsynchronousQueue
if not self.ikey:
return NullTelemetryClient()

if self.endpoint:
sender = NullSender()
# TODO: remove when https://github.com/Microsoft/ApplicationInsights-Python/pull/155 is merged
queue_class = SynchronousQueue
elif self.endpoint:
sender = AsynchronousSender(self.endpoint)
else:
sender = AsynchronousSender()
queue = AsynchronousQueue(sender)
ikey = self.ikey or '00000000-0000-0000-0000-000000000000'
queue = queue_class(sender)
context = TelemetryContext()
context.instrumentation_key = self.ikey
context.instrumentation_key = ikey
channel = TelemetryChannel(context, queue)
return TelemetryClient(self.ikey, telemetry_channel=channel)
return TelemetryClient(ikey, telemetry_channel=channel)

def debug(self, message: str, *args):
"""Log debug message."""
Expand Down

0 comments on commit 2322625

Please sign in to comment.