Skip to content

Commit

Permalink
Fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
actualwitch committed Sep 25, 2023
1 parent 5c61050 commit 664cb10
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/autometrics/exposition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import cast, Literal, TypedDict, Optional, Union
from typing import cast, Dict, Literal, TypedDict, Optional, Union
from opentelemetry.sdk.metrics.export import (
AggregationTemporality,
MetricReader,
Expand All @@ -19,11 +19,11 @@ class OTLPExporterOptions(TypedDict):
type: Literal["otlp-proto-http", "otlp-proto-grpc"]
endpoint: str
insecure: bool
headers: dict[str, str]
headers: Dict[str, str]
credentials: ChannelCredentials
push_interval: int
timeout: int
preferred_temporality: dict[type, AggregationTemporality]
preferred_temporality: Dict[type, AggregationTemporality]


class OTELPrometheusExporterOptions(TypedDict):
Expand Down
22 changes: 15 additions & 7 deletions src/autometrics/tracker/opentelemetry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from typing import Optional
from typing import Dict, Optional, Mapping

from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.metrics import (
Expand All @@ -13,6 +13,7 @@
from opentelemetry.sdk.metrics.view import View, ExplicitBucketHistogramAggregation
from opentelemetry.sdk.metrics.export import MetricReader
from opentelemetry.sdk.resources import Resource
from opentelemetry.util.types import AttributeValue

from ..exemplar import get_exemplar
from .types import Result
Expand All @@ -33,6 +34,18 @@
)
from ..settings import get_settings

LabelValue = AttributeValue
Attributes = Dict[str, LabelValue]


def get_resource_attrs() -> Attributes:
attrs: Attributes = {}
if get_settings()["service_name"] is not None:
attrs[ResourceAttributes.SERVICE_NAME] = get_settings()["service_name"]
if get_settings()["version"] is not None:
attrs[ResourceAttributes.SERVICE_VERSION] = get_settings()["version"]
return attrs


class OpenTelemetryTracker:
"""Tracker for OpenTelemetry."""
Expand All @@ -51,12 +64,7 @@ def __init__(self, reader: Optional[MetricReader] = None):
boundaries=get_settings()["histogram_buckets"]
),
)
attrs = {}
if get_settings()["service_name"] is not None:
attrs[ResourceAttributes.SERVICE_NAME] = get_settings()["service_name"]
if get_settings()["version"] is not None:
attrs[ResourceAttributes.SERVICE_VERSION] = get_settings()["version"]
resource = Resource.create(attrs)
resource = Resource.create(get_resource_attrs())
readers = [reader or PrometheusMetricReader("")]
meter_provider = MeterProvider(
views=[view],
Expand Down
8 changes: 4 additions & 4 deletions src/autometrics/tracker/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Union, Optional, Protocol, List, Literal
from typing import Union, Optional, Protocol, List, Literal, Tuple

from ..objectives import Objective

Expand Down Expand Up @@ -52,8 +52,8 @@ class TrackerType(Enum):


TrackerMessage = Union[
tuple[Literal["start"], str, str, Optional[bool]],
tuple[
Tuple[Literal["start"], str, str, Optional[bool]],
Tuple[
Literal["finish"],
float,
str,
Expand All @@ -64,6 +64,6 @@ class TrackerType(Enum):
Optional[Objective],
Optional[bool],
],
tuple[Literal["initialize_counters"], str, str, Optional[Objective]],
Tuple[Literal["initialize_counters"], str, str, Optional[Objective]],
]
MessageQueue = List[TrackerMessage]

0 comments on commit 664cb10

Please sign in to comment.