1from typing import Dict, NamedTuple, Optional, Sequence, Union
2
3
4class Timestamp:
5 """A nanosecond-resolution timestamp."""
6
7 def __init__(self, sec: float, nsec: float) -> None:
8 if nsec < 0 or nsec >= 1e9:
9 raise ValueError(f"Invalid value for nanoseconds in Timestamp: {nsec}")
10 if sec < 0:
11 nsec = -nsec
12 self.sec: int = int(sec)
13 self.nsec: int = int(nsec)
14
15 def __str__(self) -> str:
16 return f"{self.sec}.{self.nsec:09d}"
17
18 def __repr__(self) -> str:
19 return f"Timestamp({self.sec}, {self.nsec})"
20
21 def __float__(self) -> float:
22 return float(self.sec) + float(self.nsec) / 1e9
23
24 def __eq__(self, other: object) -> bool:
25 return isinstance(other, Timestamp) and self.sec == other.sec and self.nsec == other.nsec
26
27 def __ne__(self, other: object) -> bool:
28 return not self == other
29
30 def __gt__(self, other: "Timestamp") -> bool:
31 return self.nsec > other.nsec if self.sec == other.sec else self.sec > other.sec
32
33 def __lt__(self, other: "Timestamp") -> bool:
34 return self.nsec < other.nsec if self.sec == other.sec else self.sec < other.sec
35
36
37# BucketSpan is experimental and subject to change at any time.
38class BucketSpan(NamedTuple):
39 offset: int
40 length: int
41
42
43# NativeHistogram is experimental and subject to change at any time.
44class NativeHistogram(NamedTuple):
45 count_value: float
46 sum_value: float
47 schema: int
48 zero_threshold: float
49 zero_count: float
50 pos_spans: Optional[Sequence[BucketSpan]] = None
51 neg_spans: Optional[Sequence[BucketSpan]] = None
52 pos_deltas: Optional[Sequence[int]] = None
53 neg_deltas: Optional[Sequence[int]] = None
54
55
56# Timestamp and exemplar are optional.
57# Value can be an int or a float.
58# Timestamp can be a float containing a unixtime in seconds,
59# a Timestamp object, or None.
60# Exemplar can be an Exemplar object, or None.
61class Exemplar(NamedTuple):
62 labels: Dict[str, str]
63 value: float
64 timestamp: Optional[Union[float, Timestamp]] = None
65
66
67class Sample(NamedTuple):
68 name: str
69 labels: Dict[str, str]
70 value: float
71 timestamp: Optional[Union[float, Timestamp]] = None
72 exemplar: Optional[Exemplar] = None
73 native_histogram: Optional[NativeHistogram] = None