Exemplo n.º 1
0
 def describe(self):
     if 'Stats' in self.__class__.__name__:
         statkey_yaml = self.read_collector_config()['statkeys']
         for statkey_pair in statkey_yaml[self.__class__.__name__]:
             statkey_suffix = statkey_pair['metric_suffix']
             yield GaugeMetricFamily(
                 'vrops_' + self.vrops_entity_name + '_' +
                 statkey_suffix.lower(), 'vrops-exporter')
     if 'Properties' in self.__class__.__name__:
         properties_yaml = self.read_collector_config()['properties']
         if 'number_metrics' in properties_yaml[self.__class__.__name__]:
             for num in properties_yaml[
                     self.__class__.__name__]['number_metrics']:
                 yield GaugeMetricFamily(
                     'vrops_' + self.vrops_entity_name + '_' +
                     num['metric_suffix'].lower(), 'vrops-exporter')
         if 'enum_metrics' in properties_yaml[self.__class__.__name__]:
             for enum in properties_yaml[
                     self.__class__.__name__]['enum_metrics']:
                 yield UnknownMetricFamily(
                     'vrops_' + self.vrops_entity_name + '_' +
                     enum['metric_suffix'].lower(), 'vrops-exporter')
         if 'info_metrics' in properties_yaml[self.__class__.__name__]:
             for info in properties_yaml[
                     self.__class__.__name__]['info_metrics']:
                 yield InfoMetricFamily(
                     'vrops_' + self.vrops_entity_name + '_' +
                     info['metric_suffix'].lower(), 'vrops-exporter')
Exemplo n.º 2
0
    def _translate_to_prometheus(self, metric_record: MetricRecord):
        prometheus_metric = None
        label_values = []
        label_keys = []
        for label_tuple in metric_record.label_set.labels:
            label_keys.append(self._sanitize(label_tuple[0]))
            label_values.append(label_tuple[1])

        metric_name = ""
        if self._prefix != "":
            metric_name = self._prefix + "_"
        metric_name += self._sanitize(metric_record.metric.name)

        if isinstance(metric_record.metric, Counter):
            prometheus_metric = CounterMetricFamily(
                name=metric_name,
                documentation=metric_record.metric.description,
                labels=label_keys,
            )
            prometheus_metric.add_metric(
                labels=label_values, value=metric_record.aggregator.checkpoint)
        # TODO: Add support for histograms when supported in OT
        elif isinstance(metric_record.metric, Measure):
            prometheus_metric = UnknownMetricFamily(
                name=metric_name,
                documentation=metric_record.metric.description,
                labels=label_keys,
            )
            prometheus_metric.add_metric(
                labels=label_values, value=metric_record.aggregator.checkpoint)

        else:
            logger.warning("Unsupported metric type. %s",
                           type(metric_record.metric))
        return prometheus_metric
Exemplo n.º 3
0
    def generate_states(self, calling_class, vrops_entity_name, labelnames):
        if not isinstance(labelnames, list):
            print("Can't generate Gauges without label list, called from",
                  calling_class)
            return {}
        properties_yaml = self.read_collector_config()['properties']
        if 'enum_metrics' in properties_yaml[calling_class]:
            states = dict()
            for property_pair in properties_yaml[calling_class][
                    'enum_metrics']:
                property_suffix = property_pair['metric_suffix']
                states[property_suffix] = {
                    'state':
                    UnknownMetricFamily('vrops_' + vrops_entity_name + '_' +
                                        property_suffix.lower(),
                                        'vrops-exporter',
                                        labels=labelnames),
                    'property':
                    property_pair['property'],
                    'expected':
                    property_pair['expected']
                }
            return states

        if os.environ['DEBUG'] >= '1':
            print("No Enum metric type generated, from", calling_class)
        return {}
Exemplo n.º 4
0
    def generate_states(self, calling_class, vrops_entity_name, labelnames):
        if not isinstance(labelnames, list):
            logger.error(
                f'Cannot generate Gauges without label list, called from {calling_class}'
            )
            return {}
        properties_yaml = self.read_collector_config()['properties']
        if 'enum_metrics' in properties_yaml[calling_class]:
            states = dict()
            for property_pair in properties_yaml[calling_class][
                    'enum_metrics']:
                property_suffix = property_pair['metric_suffix']
                states[property_suffix] = {
                    'state':
                    UnknownMetricFamily('vrops_' + vrops_entity_name + '_' +
                                        property_suffix.lower(),
                                        'vrops-exporter',
                                        labels=labelnames),
                    'property':
                    property_pair['property'],
                    'expected':
                    property_pair['expected']
                }
            return states

        logger.info(f'No enum metric type generated, from {calling_class}')
        return {}
Exemplo n.º 5
0
    def _translate_to_prometheus(self, export_record: ExportRecord):
        prometheus_metric = None
        label_values = []
        label_keys = []
        for label_tuple in export_record.labels:
            label_keys.append(self._sanitize(label_tuple[0]))
            label_values.append(label_tuple[1])

        metric_name = ""
        if self._prefix != "":
            metric_name = self._prefix + "_"
        metric_name += self._sanitize(export_record.instrument.name)

        description = getattr(export_record.instrument, "description", "")
        if isinstance(export_record.instrument, Counter):
            prometheus_metric = CounterMetricFamily(name=metric_name,
                                                    documentation=description,
                                                    labels=label_keys)
            prometheus_metric.add_metric(
                labels=label_values, value=export_record.aggregator.checkpoint)
        elif isinstance(export_record.instrument, Observer):
            prometheus_metric = GaugeMetricFamily(name=metric_name,
                                                  documentation=description,
                                                  labels=label_keys)
            prometheus_metric.add_metric(
                labels=label_values,
                value=export_record.aggregator.checkpoint.last)
        # TODO: Add support for histograms when supported in OT
        elif isinstance(export_record.instrument, ValueRecorder):
            value = export_record.aggregator.checkpoint
            if isinstance(export_record.aggregator, MinMaxSumCountAggregator):
                prometheus_metric = SummaryMetricFamily(
                    name=metric_name,
                    documentation=description,
                    labels=label_keys,
                )
                prometheus_metric.add_metric(
                    labels=label_values,
                    count_value=value.count,
                    sum_value=value.sum,
                )
            else:
                prometheus_metric = UnknownMetricFamily(
                    name=metric_name,
                    documentation=description,
                    labels=label_keys,
                )
                prometheus_metric.add_metric(labels=label_values, value=value)

        else:
            logger.warning("Unsupported metric type. %s",
                           type(export_record.instrument))
        return prometheus_metric
Exemplo n.º 6
0
    def describe(self):
        if 'Stats' in self.collector:
            statkey_yaml = self.read_collector_config()['statkeys']
            rubrics = [r for r in statkey_yaml[self.collector]]
            if self.rubricated and not self.rubric:
                logger.warning(
                    f'{self.collector} is rubricated and has no rubric given. Considering all'
                )
                logger.info(f'Rubrics to be considered: {rubrics}')
                for r in rubrics:
                    for statkey_pair in statkey_yaml[self.collector][r]:
                        statkey_suffix = statkey_pair.get('metric_suffix')
                        yield GaugeMetricFamily(
                            'vrops_' + self.vrops_entity_name + '_' +
                            statkey_suffix.lower(), 'vrops-exporter')
            if self.rubricated and self.rubric:
                logger.info(f'Rubric to be considered: {self.rubric}')
                for statkey_pair in statkey_yaml[self.collector][self.rubric]:
                    statkey_suffix = statkey_pair.get('metric_suffix')
                    yield GaugeMetricFamily(
                        'vrops_' + self.vrops_entity_name + '_' +
                        statkey_suffix.lower(), 'vrops-exporter')
            if not self.rubricated:
                for statkey_pair in statkey_yaml[self.collector]:
                    statkey_suffix = statkey_pair.get('metric_suffix')
                    yield GaugeMetricFamily(
                        'vrops_' + self.vrops_entity_name + '_' +
                        statkey_suffix.lower(), 'vrops-exporter')

        if 'Properties' in self.collector:
            properties_yaml = self.read_collector_config()['properties']
            if 'number_metrics' in properties_yaml[self.collector]:
                for num in properties_yaml[self.collector]['number_metrics']:
                    yield GaugeMetricFamily(
                        'vrops_' + self.vrops_entity_name + '_' +
                        num['metric_suffix'].lower(), 'vrops-exporter')
            if 'enum_metrics' in properties_yaml[self.collector]:
                for enum in properties_yaml[self.collector]['enum_metrics']:
                    yield UnknownMetricFamily(
                        'vrops_' + self.vrops_entity_name + '_' +
                        enum['metric_suffix'].lower(), 'vrops-exporter')
            if 'info_metrics' in properties_yaml[self.collector]:
                for info in properties_yaml[self.collector]['info_metrics']:
                    yield InfoMetricFamily(
                        'vrops_' + self.vrops_entity_name + '_' +
                        info['metric_suffix'].lower(), 'vrops-exporter')
Exemplo n.º 7
0
    def to_metric(self, desc, tag_values, agg_data):
        """ to_metric translate the data that OpenCensus create
        to Prometheus format, using Prometheus Metric object

        :type desc: dict
        :param desc: The map that describes view definition

        :type tag_values: tuple of :class:
            `~opencensus.tags.tag_value.TagValue`
        :param object of opencensus.tags.tag_value.TagValue:
            TagValue object used as label values

        :type agg_data: object of :class:
            `~opencensus.stats.aggregation_data.AggregationData`
        :param object of opencensus.stats.aggregation_data.AggregationData:
            Aggregated data that needs to be converted as Prometheus samples

        :rtype: :class:`~prometheus_client.core.CounterMetricFamily` or
                :class:`~prometheus_client.core.HistogramMetricFamily` or
                :class:`~prometheus_client.core.UnknownMetricFamily` or
                :class:`~prometheus_client.core.GaugeMetricFamily`
        :returns: A Prometheus metric object
        """
        metric_name = desc['name']
        metric_description = desc['documentation']
        label_keys = desc['labels']

        assert (len(tag_values) == len(label_keys))
        # Prometheus requires that all tag values be strings hence
        # the need to cast none to the empty string before exporting. See
        # https://github.com/census-instrumentation/opencensus-python/issues/480
        tag_values = [tv if tv else "" for tv in tag_values]

        if isinstance(agg_data, aggregation_data_module.CountAggregationData):
            metric = CounterMetricFamily(name=metric_name,
                                         documentation=metric_description,
                                         labels=label_keys)
            metric.add_metric(labels=tag_values, value=agg_data.count_data)
            return metric

        elif isinstance(agg_data,
                        aggregation_data_module.DistributionAggregationData):

            assert (agg_data.bounds == sorted(agg_data.bounds))
            # buckets are a list of buckets. Each bucket is another list with
            # a pair of bucket name and value, or a triple of bucket name,
            # value, and exemplar. buckets need to be in order.
            buckets = []
            cum_count = 0  # Prometheus buckets expect cumulative count.
            for ii, bound in enumerate(agg_data.bounds):
                cum_count += agg_data.counts_per_bucket[ii]
                bucket = [str(bound), cum_count]
                buckets.append(bucket)
            # Prometheus requires buckets to be sorted, and +Inf present.
            # In OpenCensus we don't have +Inf in the bucket bonds so need to
            # append it here.
            buckets.append(["+Inf", agg_data.count_data])
            metric = HistogramMetricFamily(name=metric_name,
                                           documentation=metric_description,
                                           labels=label_keys)
            metric.add_metric(
                labels=tag_values,
                buckets=buckets,
                sum_value=agg_data.sum,
            )
            return metric

        elif isinstance(agg_data,
                        aggregation_data_module.SumAggregationDataFloat):
            metric = UnknownMetricFamily(name=metric_name,
                                         documentation=metric_description,
                                         labels=label_keys)
            metric.add_metric(labels=tag_values, value=agg_data.sum_data)
            return metric

        elif isinstance(agg_data,
                        aggregation_data_module.LastValueAggregationData):
            metric = GaugeMetricFamily(name=metric_name,
                                       documentation=metric_description,
                                       labels=label_keys)
            metric.add_metric(labels=tag_values, value=agg_data.value)
            return metric

        else:
            raise ValueError("unsupported aggregation type %s" %
                             type(agg_data))