Skip to content

Commit d7f1d28

Browse files
committed
first implementation of the metrics logger and env provider
1 parent 322850b commit d7f1d28

File tree

8 files changed

+110
-34
lines changed

8 files changed

+110
-34
lines changed

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,3 @@
55
```
66
pip install tox
77
```
8-
9-
https://github.com/nvbn/thefuck/blob/master/tox.ini
10-
11-
# Resources
12-
13-
- Versioneer: https://github.com/quantopian/zipline/blob/master/versioneer.py
14-
- Docker
15-
16-
- Development Guidelines: https://www.zipline.io/development-guidelines
17-
- Code Style: flake8
18-
- Testing py.test
19-
- Deployment
20-
- pandoc
21-
- https://github.com/nvbn/thefuck/blob/master/release.py
22-
- Type Checking
23-
- mypy https://github.com/open-telemetry/opentelemetry-python/blob/master/mypy.ini

aws_embedded_metrics/config/environment_configuration_provider.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from typing import TypeVar
32
from aws_embedded_metrics.config.configuration import Configuration
43

54
ENV_VAR_PREFIX = "AWS_EMF"
@@ -11,8 +10,6 @@
1110
LOG_STREAM_NAME = "LOG_STREAM_NAME"
1211
AGENT_ENDPOINT = "AGENT_ENDPOINT"
1312

14-
T = TypeVar("T")
15-
1613

1714
class EnvironmentConfigurationProvider:
1815
"""
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import abc
2+
from aws_embedded_metrics.logger.metrics_context import MetricsContext
3+
from aws_embedded_metrics.sinks import Sink
4+
5+
6+
class Environment(abc.ABC):
7+
@staticmethod
8+
@abc.abstractmethod
9+
def probe() -> bool:
10+
"""Determines whether or not we are executing in this environment"""
11+
12+
@staticmethod
13+
@abc.abstractmethod
14+
def get_name() -> str:
15+
"""Get the environment name. This will be used to set the ServiceName dimension."""
16+
17+
@staticmethod
18+
@abc.abstractmethod
19+
def get_type() -> str:
20+
"""Get the environment type. This will be used to set the ServiceType dimension."""
21+
22+
@staticmethod
23+
@abc.abstractmethod
24+
def get_log_group_name() -> str:
25+
"""Get log group name. This will be used to set the LogGroup dimension."""
26+
27+
@staticmethod
28+
@abc.abstractmethod
29+
def configure_context(context: MetricsContext) -> None:
30+
"""Configure the context with environment properties."""
31+
32+
@staticmethod
33+
@abc.abstractmethod
34+
def get_sink() -> Sink:
35+
"""Create the appropriate sink for this environment."""
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import abc
2+
from aws_embedded_metrics.environment import Environment
3+
4+
5+
class EnvironmentProvider(abc.ABC):
6+
@staticmethod
7+
@abc.abstractmethod
8+
async def get() -> Environment:
9+
"""Determine the current runtime environment"""

aws_embedded_metrics/logger/metrics_context.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def set_default_dimensions(self, default_dimensions: Dict) -> None:
7474
to the context.
7575
If no custom dimensions are specified, the metrics will be emitted
7676
with the defaults.
77-
If custome dimensions are specified, they will be prepended with
77+
If custom dimensions are specified, they will be prepended with
7878
the default dimensions.
7979
"""
8080
self.default_dimensions = default_dimensions
@@ -113,3 +113,7 @@ def create_copy_with_context(self) -> "MetricsContext":
113113
return MetricsContext(
114114
self.namespace, new_properties, new_dimensions, new_default_dimensions
115115
)
116+
117+
@staticmethod
118+
def empty() -> "MetricsContext":
119+
return MetricsContext()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from aws_embedded_metrics.environment import Environment
2+
from aws_embedded_metrics.environment.environment_provider import EnvironmentProvider
3+
from aws_embedded_metrics.logger.metrics_context import MetricsContext
4+
from aws_embedded_metrics.config import get_config
5+
6+
Config = get_config()
7+
8+
9+
class MetricsLogger:
10+
def __init__(
11+
self, env_provider: EnvironmentProvider, context: MetricsContext = None
12+
):
13+
self.env_provider = env_provider
14+
self.context: MetricsContext = context or MetricsContext.empty()
15+
16+
async def flush(self) -> None:
17+
# resolve the environment and get the sink
18+
# MOST of the time this will run synchonrously
19+
# This only runs asynchronously if executing for the
20+
# first time in a non-lambda environment
21+
environment = await self.env_provider.get()
22+
23+
self.__configureContextForEnvironment(environment)
24+
sink = environment.get_sink()
25+
26+
# accept and reset the context
27+
sink.accept(self.context)
28+
self.context = MetricsContext.empty()
29+
30+
def __configureContextForEnvironment(self, env: Environment) -> None:
31+
default_dimensions = {
32+
# LogGroup name will entirely depend on the environment since there
33+
# are some cases where the LogGroup cannot be configured (e.g. Lambda)
34+
"log_group": env.get_log_group_name(),
35+
"service_name": Config.service_name or env.get_name(),
36+
"service_type": Config.service_type or env.get_type(),
37+
}
38+
self.context.set_default_dimensions(default_dimensions)
39+
env.configure_context(self.context)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import abc
2+
from aws_embedded_metrics.logger.metrics_context import MetricsContext
3+
4+
5+
class Sink(abc.ABC):
6+
"""The mechanism by which logs are sent to their destination."""
7+
8+
@abc.abstractmethod
9+
def name(self) -> str:
10+
"""The name of the sink."""
11+
12+
@abc.abstractmethod
13+
def accept(self, context: MetricsContext) -> None:
14+
"""Flushes the metrics context to the sink."""

setup.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,20 @@
77
name="aws-embedded-metrics",
88
version="0.0.1",
99
author="Amazon Web Services",
10-
author_email="[email protected]",
11-
10+
author_email="[email protected]",
1211
description="AWS Embedded Metrics Package",
1312
long_description=long_description,
1413
long_description_content_type="text/markdown",
15-
16-
keywords='aws xray sdk',
17-
14+
keywords="aws logs metrics emf",
1815
url="https://github.com/awslabs/aws-embedded-metrics-python",
19-
2016
classifiers=[
2117
"Programming Language :: Python :: 3",
22-
"License :: OSI Approved :: MIT License",
2318
"Operating System :: OS Independent",
24-
'Development Status :: 5 - Production/Stable',
25-
'Intended Audience :: Developers',
26-
'Natural Language :: English',
27-
'License :: OSI Approved :: Apache Software License',
19+
"Development Status :: 5 - Production/Stable",
20+
"Intended Audience :: Developers",
21+
"Natural Language :: English",
22+
"License :: OSI Approved :: Apache Software License",
2823
],
29-
30-
packages=find_packages(exclude=['tests*']),
31-
include_package_data=True
24+
packages=find_packages(exclude=["tests*"]),
25+
include_package_data=True,
3226
)

0 commit comments

Comments
 (0)