Skip to content

Commit

Permalink
Add MeterProvider and Meter to the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Oct 19, 2021
1 parent fe18372 commit a7e439d
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 3 deletions.
185 changes: 185 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=function-redefined,too-many-ancestors

from typing import Optional
from logging import getLogger
from atexit import register

from opentelemetry.metrics import Meter, MeterProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.metrics.api import MetricReader, MetricExporter, View
from opentelemetry.sdk.util.instrumentation import InstrumentationInfo
from opentelemetry.sdk.metrics.instrument import (
Counter,
Histogram,
ObservableCounter,
ObservableGauge,
ObservableUpDownCounter,
UpDownCounter,
)

_logger = getLogger(__name__)


class Meter(Meter):

def __init__(self, instrumentation_info: InstrumentationInfo):
self._instrumentation_info = instrumentation_info
self._meter_provider = None

def create_counter(self, name, unit=None, description=None) -> Counter:
pass

def create_up_down_counter(
self, name, unit=None, description=None
) -> UpDownCounter:
pass

def create_observable_counter(
self, name, callback, unit=None, description=None
) -> ObservableCounter:
pass

def create_histogram(self, name, unit=None, description=None) -> Histogram:
pass

def create_observable_gauge(
self, name, callback, unit=None, description=None
) -> ObservableGauge:
pass

def create_observable_up_down_counter(
self, name, callback, unit=None, description=None
) -> ObservableUpDownCounter:
pass


class MeterProvider(MeterProvider):
"""See `opentelemetry.metrics.Provider`."""

def __init__(
self,
resource: Resource = Resource.create({}),
shutdown_on_exit: bool = True,
):
self._resource = resource
self._atexit_handler = None

if shutdown_on_exit:
self._atexit_handler = register(self.shutdown)

self._metric_readers = []
self._metric_exporters = []
self._views = []
self._shutdown = False

@property
def metric_readers(self):
return self._metric_readers

@property
def metric_exporters(self):
return self._metric_exporters

@property
def views(self):
return self._views

@property
def resource(self) -> Resource:
return self._resource

def get_meter(
self,
instrumenting_module_name: str,
instrumenting_library_version: Optional[str] = None,
schema_url: Optional[str] = None,
) -> Meter:
meter = Meter(
InstrumentationInfo(
instrumenting_module_name,
instrumenting_library_version,
schema_url,
)
)

meter._meter_provider = self

return meter

def shutdown(self):

if self._shutdown:
_logger.warning("shutdown can only be called once")
return False

result = True

for metric_reader in self._metric_readers:
result = result and metric_reader.shutdown()

for metric_exporter in self._metric_exporters:
result = result and metric_exporter.shutdown()

self._shutdown = True

return result

def force_flush(self) -> bool:
result = True

for metric_reader in self._metric_readers:
result = result and metric_reader.force_flush()

for metric_exporter in self._metric_exporters:
result = result and metric_exporter.force_flush()

return result

def register_metric_reader(self, metric_reader: "MetricReader") -> None:
self._metric_readers.append(metric_reader)

def register_metric_exporter(
self, metric_exporter: "MetricExporter"
) -> None:
self._metric_exporters.append(metric_exporter)

def register_view(self, view: "View") -> None:
self._views.append(view)


class MetricReader(MetricReader):

def collect(self):
pass


class MetricExporter(MetricExporter):

def __init__(self):
self._shutdown = False

def export(self):
pass

def shutdown(self):
self._shutdown = True


class View(View):

def __init__(self):
pass
54 changes: 54 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/metrics/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# pylint: disable=function-redefined,too-many-ancestors

from abc import ABC, abstractmethod
from logging import getLogger


_logger = getLogger(__name__)


class MetricReader(ABC):

def __init__(self):
self._shutdown = False

@abstractmethod
def collect(self):
pass

def shutdown(self):
self._shutdown = True


class MetricExporter(ABC):

def __init__(self):
self._shutdown = False

@abstractmethod
def export(self):
pass

def shutdown(self):
self._shutdown = True


class View(ABC):

@abstractmethod
def __init__(self):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import pkg_resources

from opentelemetry.attributes import BoundedAttributes

from opentelemetry.sdk.environment_variables import (
OTEL_RESOURCE_ATTRIBUTES,
OTEL_SERVICE_NAME,
Expand Down
Loading

0 comments on commit a7e439d

Please sign in to comment.