• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14from __future__ import annotations
15
16import abc
17from dataclasses import dataclass
18from dataclasses import field
19from typing import List, Mapping, Tuple
20
21
22class Exporter(metaclass=abc.ABCMeta):
23    """Abstract base class for census data exporters."""
24
25    @abc.abstractmethod
26    def export_stats_data(self, stats_data: List[TracingData]) -> None:
27        """Exports a list of TracingData objects to the exporter's destination.
28
29        Args:
30          stats_data: A list of TracingData objects to export.
31        """
32        raise NotImplementedError()
33
34    @abc.abstractmethod
35    def export_tracing_data(self, tracing_data: List[StatsData]) -> None:
36        """Exports a list of StatsData objects to the exporter's destination.
37
38        Args:
39          tracing_data: A list of StatsData objects to export.
40        """
41        raise NotImplementedError()
42
43
44@dataclass(frozen=True)
45class StatsData:
46    """A data class representing stats data.
47
48    Attributes:
49      name: An element of grpc_observability._cyobservability.MetricsName, e.g.
50        MetricsName.CLIENT_STARTED_RPCS.
51      measure_double: A bool indicate whether the metric is a floating-point
52        value.
53      value_int: The actual metric value if measure_double is False.
54      value_float: The actual metric value if measure_double is True.
55      labels: A dictionary that maps label tags associated with this metric to
56       corresponding label value.
57    """
58
59    name: "grpc_observability._cyobservability.MetricsName"
60    measure_double: bool
61    value_int: int = 0
62    value_float: float = 0.0
63    labels: Mapping[str, str] = field(default_factory=dict)
64
65
66@dataclass(frozen=True)
67class TracingData:
68    """A data class representing tracing data.
69
70    Attributes:
71      name: The name for tracing data, also the name for the Span.
72      start_time: The start time for the span in RFC3339 UTC "Zulu" format, e.g.
73       2014-10-02T15:01:23Z
74      end_time: The end time for the span in RFC3339 UTC "Zulu" format, e.g.
75       2014-10-02T15:01:23Z
76      trace_id: The identifier for the trace associated with this span as a
77       32-character hexadecimal encoded string,
78       e.g. 26ed0036f2eff2b7317bccce3e28d01f
79      span_id: The identifier for the span as a 16-character hexadecimal encoded
80       string. e.g. 113ec879e62583bc
81      parent_span_id: An option identifier for the span's parent id.
82      status: An element of grpc.StatusCode in string format representing the
83       final status for the trace data.
84      should_sample: A bool indicates whether the span is sampled.
85      child_span_count: The number of child span associated with this span.
86      span_labels: A dictionary that maps labels tags associated with this
87       span to corresponding label value.
88      span_annotations: A dictionary that maps annotation timeStamp with
89       description. The timeStamp have a format which can be converted
90       to Python datetime.datetime, e.g. 2023-05-29 17:07:09.895
91    """
92
93    name: str
94    start_time: str
95    end_time: str
96    trace_id: str
97    span_id: str
98    parent_span_id: str
99    status: str
100    should_sample: bool
101    child_span_count: int
102    span_labels: Mapping[str, str] = field(default_factory=dict)
103    span_annotations: List[Tuple[str, str]] = field(default_factory=list)
104