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 19import enum 20from typing import AnyStr, Dict, List, Mapping, Set, Tuple 21 22 23class Exporter(metaclass=abc.ABCMeta): 24 """Abstract base class for census data exporters.""" 25 26 @abc.abstractmethod 27 def export_stats_data(self, stats_data: List[TracingData]) -> None: 28 """Exports a list of TracingData objects to the exporter's destination. 29 30 Args: 31 stats_data: A list of TracingData objects to export. 32 """ 33 raise NotImplementedError() 34 35 @abc.abstractmethod 36 def export_tracing_data(self, tracing_data: List[StatsData]) -> None: 37 """Exports a list of StatsData objects to the exporter's destination. 38 39 Args: 40 tracing_data: A list of StatsData objects to export. 41 """ 42 raise NotImplementedError() 43 44 45@dataclass(frozen=True) 46class StatsData: 47 """A data class representing stats data. 48 49 Attributes: 50 name: An element of grpc_observability._cyobservability.MetricsName, e.g. 51 MetricsName.CLIENT_STARTED_RPCS. 52 measure_double: A bool indicate whether the metric is a floating-point 53 value. 54 value_int: The actual metric value if measure_double is False. 55 value_float: The actual metric value if measure_double is True. 56 include_exchange_labels: Whether this data should include exchanged labels. 57 labels: A dictionary that maps label tags associated with this metric to 58 corresponding label value. 59 identifiers: A set of strings identifying which stats plugins this StatsData 60 belongs to. 61 registered_method: Whether the method in this data is a registered method 62 in stubs. 63 """ 64 65 name: "grpc_observability._cyobservability.MetricsName" 66 measure_double: bool 67 value_int: int = 0 68 value_float: float = 0.0 69 include_exchange_labels: bool = False 70 labels: Dict[str, AnyStr] = field(default_factory=dict) 71 identifiers: Set[str] = field(default_factory=set) 72 registered_method: bool = False 73 74 75@dataclass(frozen=True) 76class TracingData: 77 """A data class representing tracing data. 78 79 Attributes: 80 name: The name for tracing data, also the name for the Span. 81 start_time: The start time for the span in RFC3339 UTC "Zulu" format, e.g. 82 2014-10-02T15:01:23Z 83 end_time: The end time for the span in RFC3339 UTC "Zulu" format, e.g. 84 2014-10-02T15:01:23Z 85 trace_id: The identifier for the trace associated with this span as a 86 32-character hexadecimal encoded string, 87 e.g. 26ed0036f2eff2b7317bccce3e28d01f 88 span_id: The identifier for the span as a 16-character hexadecimal encoded 89 string. e.g. 113ec879e62583bc 90 parent_span_id: An option identifier for the span's parent id. 91 status: An element of grpc.StatusCode in string format representing the 92 final status for the trace data. 93 should_sample: A bool indicates whether the span is sampled. 94 child_span_count: The number of child span associated with this span. 95 span_labels: A dictionary that maps labels tags associated with this 96 span to corresponding label value. 97 span_annotations: A dictionary that maps annotation timeStamp with 98 description. The timeStamp have a format which can be converted 99 to Python datetime.datetime, e.g. 2023-05-29 17:07:09.895 100 """ 101 102 name: str 103 start_time: str 104 end_time: str 105 trace_id: str 106 span_id: str 107 parent_span_id: str 108 status: str 109 should_sample: bool 110 child_span_count: int 111 span_labels: Mapping[str, AnyStr] = field(default_factory=dict) 112 span_annotations: List[Tuple[str, str]] = field(default_factory=list) 113 114 115@enum.unique 116class OptionalLabelType(enum.Enum): 117 """What kinds of optional labels to add to metrics.""" 118 119 XDS_SERVICE_LABELS = "kXdsServiceLabels" 120