-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyroscope_instrument.py
More file actions
46 lines (35 loc) · 1.73 KB
/
pyroscope_instrument.py
File metadata and controls
46 lines (35 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import dataclasses
import typing
from lite_bootstrap import import_checker
from lite_bootstrap.instruments.base import BaseConfig, BaseInstrument
if import_checker.is_pyroscope_installed:
import pyroscope
@dataclasses.dataclass(kw_only=True, frozen=True)
class PyroscopeConfig(BaseConfig):
pyroscope_endpoint: str | None = None
pyroscope_sample_rate: int = 100
pyroscope_tags: dict[str, str] = dataclasses.field(default_factory=dict)
pyroscope_additional_params: dict[str, typing.Any] = dataclasses.field(default_factory=dict)
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
class PyroscopeInstrument(BaseInstrument):
bootstrap_config: PyroscopeConfig
not_ready_message = "pyroscope_endpoint is empty"
missing_dependency_message = "pyroscope is not installed"
def is_ready(self) -> bool:
return bool(self.bootstrap_config.pyroscope_endpoint) and import_checker.is_pyroscope_installed
@staticmethod
def check_dependencies() -> bool:
return import_checker.is_pyroscope_installed
def bootstrap(self) -> None:
namespace: str | None = getattr(self.bootstrap_config, "opentelemetry_namespace", None)
tags = ({"service_namespace": namespace} if namespace else {}) | self.bootstrap_config.pyroscope_tags
pyroscope.configure(
application_name=getattr(self.bootstrap_config, "opentelemetry_service_name", None)
or self.bootstrap_config.service_name,
server_address=self.bootstrap_config.pyroscope_endpoint,
sample_rate=self.bootstrap_config.pyroscope_sample_rate,
tags=tags,
**self.bootstrap_config.pyroscope_additional_params,
)
def teardown(self) -> None:
pyroscope.shutdown()