A list of the metrics this query will ask for.
420585
def description(self):
        

        if 'metrics' in self.raw:
            metrics = self.raw['metrics']
            head = metrics[0:-1] or metrics[0:1]
            text = ", ".join(head)
            if len(metrics) > 1:
                tail = metrics[-1]
                text = text + " and " + tail
        else:
            text = 'n/a'

        return text
1320311
def refresh_metrics(self):
        """"""
        metrics = self.get_metrics()
        dbmetrics = (
            db.session.query(DruidMetric)
            .filter(DruidMetric.datasource_id == self.datasource_id)
            .filter(DruidMetric.metric_name.in_(metrics.keys()))
        )
        dbmetrics = {metric.metric_name: metric for metric in dbmetrics}
        for metric in metrics.values():
            dbmetric = dbmetrics.get(metric.metric_name)
            if dbmetric:
                for attr in ['json', 'metric_type']:
                    setattr(dbmetric, attr, getattr(metric, attr))
            else:
                with db.session.no_autoflush:
                    metric.datasource_id = self.datasource_id
                    db.session.add(metric)
637410
def _collect_for_instance(self, instance, connection):
        """"""
        with connection.cursor() as cursor:
            for queue, metrics in self.get_queue_info(instance, cursor):
                for name, metric in metrics.items():
                    self.publish('.'.join((instance, queue, name)), metric)

        with connection.cursor() as cursor:
            consumers = self.get_consumer_info(instance, cursor)
            for queue, consumer, metrics in consumers:
                for name, metric in metrics.items():
                    key_parts = (instance, queue, 'consumers', consumer, name)
                    self.publish('.'.join(key_parts), metric)
816274
def build_messages_metrics(messages):
    """"""
    count_types = collections.Counter(
        line.get('type') or None
        for line in messages)
    count_modules = collections.Counter(
        line.get('module') or None
        for line in messages)
    count_symbols = collections.Counter(
        line.get('symbol') or None
        for line in messages)
    count_paths = collections.Counter(
        line.get('path') or None
        for line in messages)

    return {
        'types': count_types,
        'modules': count_modules,
        'symbols': count_symbols,
        'paths': count_paths,
    }
783540
private List<String> constructMetricQueryKeys(MetricQuery query) {
        Long startTimeQuery = query.getStartTimestamp();
        Long endTimeQuery = query.getEndTimestamp();
        List<String> metricQueryKeys = new ArrayList<String>();

        for (Long timeStamp = startTimeQuery; timeStamp < endTimeQuery; timeStamp = timeStamp + DURATION_IN_MILLIS) {
            StringBuilder sb = new StringBuilder();

            sb.append(timeStamp).append(":");
            sb.append(query.getNamespace()).append(":");
            sb.append(query.getScope()).append(":");
            sb.append(query.getMetric()).append(":");

            // sort the tag key and values within each tag key alphabetically
            Map<String, String> treeMap = new TreeMap<String, String>();

            for (Map.Entry<String, String> tag : query.getTags().entrySet()) {
                String[] tagValues = tag.getValue().split("\\|");

                Arrays.sort(tagValues);

                StringBuilder sbTag = new StringBuilder();
                String separator = "";

                for (String tagValue : tagValues) {
                    sbTag.append(separator);
                    separator = "|";
                    sbTag.append(tagValue);
                }
                treeMap.put(tag.getKey(), sbTag.toString());
            }
            sb.append(treeMap).append(":");
            sb.append(query.getAggregator()).append(":");
            sb.append(query.getDownsampler()).append(":");
            sb.append(query.getDownsamplingPeriod());
            metricQueryKeys.add(sb.toString());
        }
        return metricQueryKeys;
    }
946402
def collect_metrics(self, instance):
        
        i_key = self._instance_key(instance)
        if not self.mor_cache.contains(i_key):
            self.log.debug("Not collecting metrics for instance '{}', nothing to do yet.".format(i_key))
            return

        vm_count = 0
        custom_tags = instance.get('tags', [])
        tags = ["vcenter_server:{}".format(ensure_unicode(instance.get('name')))] + custom_tags

        n_mors = self.mor_cache.instance_size(i_key)
        if not n_mors:
            self.gauge('vsphere.vm.count', vm_count, tags=tags)
            self.log.debug("No Mor objects to process for instance '{}', skip...".format(i_key))
            return

        self.log.debug("Collecting metrics for {} mors".format(ensure_unicode(n_mors)))

        # Request metrics for several objects at once. We can limit the number of objects with batch_size
        # If batch_size is 0, process everything at once
        batch_size = self.batch_morlist_size or n_mors
        for batch in self.mor_cache.mors_batch(i_key, batch_size):
            query_specs = []
            for _, mor in iteritems(batch):
                if mor['mor_type'] == 'vm':
                    vm_count += 1
                if mor['mor_type'] not in REALTIME_RESOURCES and ('metrics' not in mor or not mor['metrics']):
                    continue

                query_spec = vim.PerformanceManager.QuerySpec()
                query_spec.entity = mor["mor"]
                query_spec.intervalId = mor["interval"]
                query_spec.maxSample = 1
                if mor['mor_type'] in REALTIME_RESOURCES:
                    query_spec.metricId = self.metadata_cache.get_metric_ids(i_key)
                else:
                    query_spec.metricId = mor["metrics"]
                query_specs.append(query_spec)

            if query_specs:
                self.pool.apply_async(self._collect_metrics_async, args=(instance, query_specs))

        self.gauge('vsphere.vm.count', vm_count, tags=tags)
1232267
public List<String> suggestMetrics(final String search,
      final int max_results) {
    return metrics.suggest(search, max_results);
  }
884698
def _collect_stats(
        self,
        key,
        db,
        instance_tags,
        relations,
        custom_metrics,
        collect_function_metrics,
        collect_count_metrics,
        collect_activity_metrics,
        collect_database_size_metrics,
        collect_default_db,
        interface_error,
        programming_error,
    ):
        

        db_instance_metrics = self._get_instance_metrics(key, db, collect_database_size_metrics, collect_default_db)
        bgw_instance_metrics = self._get_bgw_metrics(key, db)
        archiver_instance_metrics = self._get_archiver_metrics(key, db)

        metric_scope = [self.CONNECTION_METRICS, self.LOCK_METRICS]

        if collect_function_metrics:
            metric_scope.append(self.FUNCTION_METRICS)
        if collect_count_metrics:
            metric_scope.append(self.COUNT_METRICS)

        # Do we need relation-specific metrics?
        relations_config = {}
        if relations:
            metric_scope += [self.REL_METRICS, self.IDX_METRICS, self.SIZE_METRICS, self.STATIO_METRICS]
            relations_config = self._build_relations_config(relations)

        replication_metrics = self._get_replication_metrics(key, db)
        if replication_metrics is not None:
            # FIXME: constants shouldn't be modified
            self.REPLICATION_METRICS['metrics'] = replication_metrics
            metric_scope.append(self.REPLICATION_METRICS)

        try:
            cursor = db.cursor()
            results_len = self._query_scope(
                cursor, db_instance_metrics, key, db, instance_tags, False, programming_error, relations_config
            )
            if results_len is not None:
                self.gauge(
                    "postgresql.db.count", results_len, tags=[t for t in instance_tags if not t.startswith("db:")]
                )

            self._query_scope(
                cursor, bgw_instance_metrics, key, db, instance_tags, False, programming_error, relations_config
            )
            self._query_scope(
                cursor, archiver_instance_metrics, key, db, instance_tags, False, programming_error, relations_config
            )

            if collect_activity_metrics:
                activity_metrics = self._get_activity_metrics(key, db)
                self._query_scope(
                    cursor, activity_metrics, key, db, instance_tags, False, programming_error, relations_config
                )

            for scope in list(metric_scope) + custom_metrics:
                self._query_scope(
                    cursor, scope, key, db, instance_tags, scope in custom_metrics, programming_error, relations_config
                )

            cursor.close()
        except (interface_error, socket.error) as e:
            self.log.error("Connection error: %s" % str(e))
            raise ShouldRestartException
258304
def get_query_params(self):
        """"""
        params = self.request.query_params.copy()
        params.update(self.request.data)
        return params
616324
def do_metric_list(mc, args):
    ''''''
    fields = {}
    if args.name:
        fields['name'] = args.name
    if args.dimensions:
        fields['dimensions'] = utils.format_dimensions_query(args.dimensions)
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.starttime:
        _translate_starttime(args)
        fields['start_time'] = args.starttime
    if args.endtime:
        fields['end_time'] = args.endtime
    if args.tenant_id:
        fields['tenant_id'] = args.tenant_id

    try:
        metric = mc.metrics.list(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(metric))
            return
        cols = ['name', 'dimensions']
        formatters = {
            'name': lambda x: x['name'],
            'dimensions': lambda x: utils.format_dict(x['dimensions']),
        }
        if isinstance(metric, list):
            # print the list
            utils.print_list(metric, cols, formatters=formatters)
        else:
            # add the dictionary to a list, so print_list works
            metric_list = list()
            metric_list.append(metric)
            utils.print_list(
                metric_list,
                cols,
                formatters=formatters)
770020
def GetMetadata():
  """"""
  return [
      # GRR user-management metrics.
      stats_utils.CreateEventMetadata(
          "acl_check_time", fields=[("check_type", str)]),
      stats_utils.CreateCounterMetadata(
          "approval_searches",
          fields=[("reason_presence", str), ("source", str)]),

      # Cronjob metrics.
      stats_utils.CreateCounterMetadata("cron_internal_error"),
      stats_utils.CreateCounterMetadata(
          "cron_job_failure", fields=[("cron_job_id", str)]),
      stats_utils.CreateCounterMetadata(
          "cron_job_timeout", fields=[("cron_job_id", str)]),
      stats_utils.CreateEventMetadata(
          "cron_job_latency", fields=[("cron_job_id", str)]),

      # Access-control metrics.
      stats_utils.CreateCounterMetadata("grr_expired_tokens"),

      # Datastore metrics.
      stats_utils.CreateCounterMetadata("grr_commit_failure"),
      stats_utils.CreateCounterMetadata("datastore_retries"),
      stats_utils.CreateGaugeMetadata(
          "datastore_size",
          int,
          docstring="Size of data store in bytes",
          units="BYTES"),
      stats_utils.CreateCounterMetadata("grr_task_retransmission_count"),
      stats_utils.CreateCounterMetadata("grr_task_ttl_expired_count"),
      stats_utils.CreateEventMetadata(
          "db_request_latency",
          fields=[("call", str)],
          bins=[0.05 * 1.2**x for x in range(30)]),  # 50ms to ~10 secs
      stats_utils.CreateCounterMetadata(
          "db_request_errors", fields=[("call", str), ("type", str)]),

      # Threadpool metrics.
      stats_utils.CreateGaugeMetadata(
          "threadpool_outstanding_tasks", int, fields=[("pool_name", str)]),
      stats_utils.CreateGaugeMetadata(
          "threadpool_threads", int, fields=[("pool_name", str)]),
      stats_utils.CreateGaugeMetadata(
          "threadpool_cpu_use", float, fields=[("pool_name", str)]),
      stats_utils.CreateCounterMetadata(
          "threadpool_task_exceptions", fields=[("pool_name", str)]),
      stats_utils.CreateEventMetadata(
          "threadpool_working_time", fields=[("pool_name", str)]),
      stats_utils.CreateEventMetadata(
          "threadpool_queueing_time", fields=[("pool_name", str)]),

      # Worker and flow-related metrics.
      stats_utils.CreateCounterMetadata("grr_flows_stuck"),
      stats_utils.CreateCounterMetadata(
          "worker_bad_flow_objects", fields=[("type", str)]),
      stats_utils.CreateCounterMetadata(
          "worker_session_errors", fields=[("type", str)]),
      stats_utils.CreateCounterMetadata(
          "worker_flow_lock_error",
          docstring="Worker lock failures. We expect these to be high when the "
          "systemis idle."),
      stats_utils.CreateEventMetadata(
          "worker_flow_processing_time", fields=[("flow", str)]),
      stats_utils.CreateEventMetadata("worker_time_to_retrieve_notifications"),
      stats_utils.CreateCounterMetadata("grr_flow_completed_count"),
      stats_utils.CreateCounterMetadata("grr_flow_errors"),
      stats_utils.CreateCounterMetadata("grr_flow_invalid_flow_count"),
      stats_utils.CreateCounterMetadata("grr_request_retransmission_count"),
      stats_utils.CreateCounterMetadata("grr_response_out_of_order"),
      stats_utils.CreateCounterMetadata("grr_unique_clients"),
      stats_utils.CreateCounterMetadata("grr_worker_states_run"),
      stats_utils.CreateCounterMetadata("grr_well_known_flow_requests"),
      stats_utils.CreateCounterMetadata("flow_starts", fields=[("flow", str)]),
      stats_utils.CreateCounterMetadata("flow_errors", fields=[("flow", str)]),
      stats_utils.CreateCounterMetadata(
          "flow_completions", fields=[("flow", str)]),
      stats_utils.CreateCounterMetadata(
          "well_known_flow_requests", fields=[("flow", str)]),
      stats_utils.CreateCounterMetadata(
          "well_known_flow_errors", fields=[("flow", str)]),
      stats_utils.CreateEventMetadata("fleetspeak_last_ping_latency_millis"),

      # Hunt-related metrics.
      stats_utils.CreateCounterMetadata(
          "hunt_output_plugin_verifications", fields=[("status", str)]),
      stats_utils.CreateCounterMetadata(
          "hunt_output_plugin_verification_errors"),
      stats_utils.CreateCounterMetadata(
          "hunt_output_plugin_errors", fields=[("plugin", str)]),
      stats_utils.CreateCounterMetadata(
          "hunt_results_ran_through_plugin", fields=[("plugin", str)]),
      stats_utils.CreateCounterMetadata("hunt_results_compacted"),
      stats_utils.CreateCounterMetadata(
          "hunt_results_compaction_locking_errors"),
      stats_utils.CreateCounterMetadata("hunt_results_added"),

      # Metric used to identify the master in a distributed server setup.
      stats_utils.CreateGaugeMetadata("is_master", int),

      # GRR-API metrics.
      stats_utils.CreateEventMetadata(
          "api_method_latency",
          fields=[("method_name", str), ("protocol", str), ("status", str)]),
      stats_utils.CreateEventMetadata(
          "api_access_probe_latency",
          fields=[("method_name", str), ("protocol", str), ("status", str)]),

      # Client-related metrics.
      stats_utils.CreateCounterMetadata("grr_client_crashes"),
      stats_utils.CreateCounterMetadata(
          "client_pings_by_label", fields=[("label", str)]),

      # Metrics specific to GRR frontends.
      stats_utils.CreateGaugeMetadata(
          "frontend_active_count", int, fields=[("source", str)]),
      stats_utils.CreateGaugeMetadata("frontend_max_active_count", int),
      stats_utils.CreateCounterMetadata(
          "frontend_http_requests", fields=[("action", str), ("protocol",
                                                              str)]),
      stats_utils.CreateCounterMetadata(
          "frontend_in_bytes", fields=[("source", str)]),
      stats_utils.CreateCounterMetadata(
          "frontend_out_bytes", fields=[("source", str)]),
      stats_utils.CreateCounterMetadata(
          "frontend_request_count", fields=[("source", str)]),
      stats_utils.CreateCounterMetadata(
          "frontend_inactive_request_count", fields=[("source", str)]),
      stats_utils.CreateEventMetadata(
          "frontend_request_latency", fields=[("source", str)]),
      stats_utils.CreateEventMetadata("grr_frontendserver_handle_time"),
      stats_utils.CreateCounterMetadata("grr_frontendserver_handle_num"),
      stats_utils.CreateGaugeMetadata("grr_frontendserver_client_cache_size",
                                      int),
      stats_utils.CreateCounterMetadata("grr_messages_sent"),
      stats_utils.CreateCounterMetadata(
          "grr_pub_key_cache", fields=[("type", str)]),
  ]
644267
def do_metric_name_list(mc, args):
    ''''''
    fields = {}
    if args.dimensions:
        fields['dimensions'] = utils.format_dimensions_query(args.dimensions)
    if args.limit:
        fields['limit'] = args.limit
    if args.offset:
        fields['offset'] = args.offset
    if args.tenant_id:
        fields['tenant_id'] = args.tenant_id

    try:
        metric_names = mc.metrics.list_names(**fields)
    except (osc_exc.ClientException, k_exc.HttpError) as he:
        raise osc_exc.CommandError('%s\n%s' % (he.message, he.details))
    else:
        if args.json:
            print(utils.json_formatter(metric_names))
            return
        if isinstance(metric_names, list):
            utils.print_list(metric_names, ['Name'], formatters={'Name': lambda x: x['name']})
1008483
def list_metrics():
    """"""
    for name, operator in ALL_OPERATORS.items():
        print(f"{name} operator:")
        if len(operator.cls.metrics) > 0:
            print(
                tabulate.tabulate(
                    headers=("Name", "Description", "Type"),
                    tabular_data=operator.cls.metrics,
                    tablefmt=DEFAULT_GRID_STYLE,
                )
            )
943280
def _publish_metrics(self, prev_keys, key, data, publishfn=None):
        """"""
        if key not in data:
            return
        value = data[key]
        keys = prev_keys + [key]
        if not publishfn:
            publishfn = self.publish
        if isinstance(value, dict):
            for new_key in value:
                self._publish_metrics(keys, new_key, value)
        elif isinstance(value, int) or isinstance(value, float):
            publishfn('.'.join(keys), value)
        elif isinstance(value, long):
            publishfn('.'.join(keys), float(value))
1008589
def get_list(self):
        
        field = self.FIELD_NAME
        query = ElasticQuery.get_agg(field=field,
                                     date_field=self.FIELD_DATE,
                                     start=self.start, end=self.end,
                                     filters=self.esfilters)
        logger.debug("Metric: '%s' (%s); Query: %s",
                     self.name, self.id, query)
        res = self.get_metrics_data(query)
        list_ = {field: [], "value": []}
        for bucket in res['aggregations'][str(ElasticQuery.AGGREGATION_ID)]['buckets']:
            list_[field].append(bucket['key'])
            list_['value'].append(bucket['doc_count'])
        return list_
318074
def __get_metrics(self):
        
        esfilters_closed = None
        esfilters_opened = None
        if self.esfilters:
            esfilters_closed = self.esfilters.copy()
            esfilters_opened = self.esfilters.copy()

        closed = self.closed_class(self.es_url, self.es_index,
                                   start=self.start, end=self.end,
                                   esfilters=esfilters_closed, interval=self.interval)
        opened = self.opened_class(self.es_url, self.es_index,
                                   start=self.start, end=self.end,
                                   esfilters=esfilters_opened, interval=self.interval)
        return (closed, opened)
126341
def get_metrics(self):
        """"""
        if(self.sloc == 0):
            if(self.comments == 0):
                ratio_comment_to_code = 0.00
            else:
                ratio_comment_to_code = 1.00
        else:
            ratio_comment_to_code = float(self.comments) / self.sloc
        metrics = OrderedDict([('sloc', self.sloc), ('comments', self.comments),
                               ('ratio_comment_to_code', round(ratio_comment_to_code, 2))])
        return metrics