Execute and gather metrics.

@param cl
the command line
@throws MetricGatheringException
on any error gathering metrics
@return the metrics
439004
public final Collection<Metric> gatherMetrics(final ICommandLine cl) throws MetricGatheringException {
        LOG.debug(getContext(), "check_mysql_query gather metrics");
        List<Metric> metrics = new ArrayList<Metric>();
        Mysql mysql = new Mysql(cl);
        Connection conn = null;
        try {
            conn = mysql.getConnection();
        } catch (ClassNotFoundException e) {
            LOG.error(getContext(), "Mysql driver library not found into the classpath" + ": download and put it in the same directory " + "of this plugin");
            throw new MetricGatheringException("CHECK_MYSQL_QUERY - CRITICAL: Error accessing the " + "MySQL server - JDBC driver not installed",
                    Status.CRITICAL, e);
        } catch (Exception e) {
            LOG.error(getContext(), "Error accessing the MySQL server", e);
            throw new MetricGatheringException("CHECK_MYSQL_QUERY - CRITICAL: Error accessing " + "the MySQL server - " + e.getMessage(),
                    Status.CRITICAL, e);
        }

        String query = cl.getOptionValue("query");
        Statement st = null;
        ResultSet set = null;
        try {
            st = conn.createStatement();
            st.execute(query);
            set = st.getResultSet();
            BigDecimal value = null;
            if (set.first()) {
                value = set.getBigDecimal(1);
            }

            metrics.add(new Metric("rows", "CHECK_MYSQL_QUERY - Returned value is " + (value != null ? value.longValue() : null), value, null, null));

        } catch (SQLException e) {
            
            String message = e.getMessage();
            LOG.warn(getContext(), "Error executing plugin CheckMysqlQuery : " + message, e);
            throw new MetricGatheringException("CHECK_MYSQL_QUERY - CRITICAL: " + message, Status.CRITICAL, e);
        } finally {
            DBUtils.closeQuietly(set);
            DBUtils.closeQuietly(st);
            DBUtils.closeQuietly(conn);
        }

        return metrics;
    }
985795
public void processMetricAggregation() {
        if (!isEmpty(metricAggregation)) {
            String[] aggregations = metricAggregation.split(";");
            for (int i = 0; i < aggregations.length; i++) {
                String definition = aggregations[i].trim();
                int index0 = definition.indexOf("=");
                int index1 = definition.indexOf("(");
                int index2 = definition.indexOf(")");
                String aggregatedName = definition.substring(0, index0);
                String operation = definition.substring(index0 + 1, index1);
                String parameters = definition.substring(index1 + 1, index2);
                List<String> metricNames = new ArrayList<>();
                metricNames.add(operation);
                // Case a list of metricNames
                if (parameters.contains(",")) {
                    String[] listMetricNames = parameters.split(",");
                    for (int j = 0; j < listMetricNames.length; j++) {
                        metricNames.add(listMetricNames[j].trim());
                    }
                } else {
                    Pattern pattern = Pattern.compile(parameters);
                    for (String metricName : nameIdMetrics.keySet()) {
                        if (pattern.matcher(metricName).matches()) {
                            metricNames.add(metricName);
                        }
                    }
                }
                aggregatedMetrics.put(aggregatedName, metricNames);
            }
            log.debugf("Gnocchi Aggregated Metrics from %s -> %s", metricAggregation, aggregatedMetrics);
        }
    }
771140
def StreamMetrics(self, request_iterator, context):
        """"""
        LOG.debug("StreamMetrics called")

        # set up arguments
        collect_args = (next(request_iterator))
        max_metrics_buffer = 0
        max_collect_duration = 0
        cfg = Metric(pb=collect_args.Metrics_Arg.metrics[0])
        try:
            max_metrics_buffer = int(cfg.config["max-metrics-buffer"])
        except Exception as ex:
            LOG.debug("Unable to get schedule parameters: {}".format(ex))
        try:
            max_collect_duration = int(cfg.config["max-collect-duration"])
        except Exception as ex:
            LOG.debug("Unable to get schedule parameters: {}".format(ex))
        if max_metrics_buffer > 0:
            self.max_metrics_buffer = max_metrics_buffer
        if max_collect_duration > 0:
            self.max_collect_duration = max_collect_duration

        # start collection thread
        thread = threading.Thread(target=self._stream_wrapper, args=(collect_args,),)
        thread.daemon = True
        thread.start()

        # stream metrics
        metrics = []
        metrics_to_stream = []
        stream_timeout = self.max_collect_duration
        while context.is_active():
            try:
                # wait for metrics until timeout is reached
                t_start = time.time()
                metrics = self.metrics_queue.get(block=True, timeout=stream_timeout)
                elapsed = round(time.time() - t_start)
                stream_timeout -= elapsed
            except queue.Empty:
                LOG.debug("Max collect duration exceeded. Streaming {} metrics".format(len(metrics_to_stream)))
                metrics_col = CollectReply(Metrics_Reply=MetricsReply(metrics=[m.pb for m in metrics_to_stream]))
                metrics_to_stream = []
                stream_timeout = self.max_collect_duration
                yield metrics_col
            else:
                for metric in metrics:
                    metrics_to_stream.append(metric)
                    if len(metrics_to_stream) == self.max_metrics_buffer:
                        LOG.debug("Max metrics buffer reached. Streaming {} metrics".format(len(metrics_to_stream)))
                        metrics_col = CollectReply(
                            Metrics_Reply=MetricsReply(metrics=[m.pb for m in metrics_to_stream]))
                        metrics_to_stream = []
                        stream_timeout = self.max_collect_duration
                        yield metrics_col
                # stream metrics if max_metrics_buffer is 0 or enough metrics has been collected
                if self.max_metrics_buffer == 0:
                    LOG.debug("Max metrics buffer set to 0. Streaming {} metrics".format(len(metrics_to_stream)))
                    metrics_col = CollectReply(Metrics_Reply=MetricsReply(metrics=[m.pb for m in metrics_to_stream]))
                    metrics_to_stream = []
                    stream_timeout = self.max_collect_duration
                    yield metrics_col

        # sent notification if stream has been stopped
        self.done_queue.put(True)
595718
public java.util.List<MetricCollectionType> getMetrics() {
        if (metrics == null) {
            metrics = new com.amazonaws.internal.SdkInternalList<MetricCollectionType>();
        }
        return metrics;
    }
1099556
@Override
  public void getMetrics(MetricsCollector collector, boolean all) {
    MetricsRecordBuilder metricsRecordBuilder = collector.addRecord("sparkShuffleService");

    for (Map.Entry<String, Metric> entry : metricSet.getMetrics().entrySet()) {
      collectMetric(metricsRecordBuilder, entry.getKey(), entry.getValue());
    }
  }
56785
@SuppressWarnings("unchecked")
	public static <T, L extends LoggerInformer> L getLogger(Class<T> clazz) {
		return (L)SLF4JLoggerFactory.getLogger(clazz);
	}
1122880
public Metric createMetric(String metricName) {
    // [START createMetric]
    MetricInfo metricInfo = MetricInfo.of(metricName, "severity>=ERROR");
    Metric metric = logging.create(metricInfo);
    // [END createMetric]
    return metric;
  }
704632
public JAnnotation getAnnotation(Class cl)
  {
    if (cl != null)
      return getAnnotation(cl.getName());
    else
      return null;
  }
864366
@SuppressWarnings("unchecked")
	public void execute() {
		Utils.require(m_metricPaths != null && m_metricPaths.length != 0, "Metric ('m') parameter is required");
        List<String> allfieldNames = new ArrayList<String>();
		for (GroupSetEntry groupSetEntry : m_groupSet) {
    		for (GroupPath groupPath : groupSetEntry.m_groupPaths){
    			preserveGroupLink(groupPath, true);
    			groupSetEntry.m_metricPath.addGroupPath(groupPath);
    		}
    		preserveGroupLink(groupSetEntry.m_metricPath, false);		
   		    allfieldNames.addAll(groupSetEntry.m_metricPath.fieldNames);
            if (groupSetEntry.m_isComposite) {
                groupSetEntry.m_compositeGroup = groupSetEntry.m_totalGroup.createSubgroup(Group.COMPOSITE_GROUP_NAME);
            }
		}
        final List<String> fieldNames = new ArrayList<String>(new HashSet<String>(allfieldNames)); // remove duplicates

		String queryText = m_query;
		if (queryText == null) queryText = "*";
		
		ArrayList<Integer> iGroupSet2Skip = new ArrayList<Integer>();
		if (queryText.equals("*")) { 												// all items
		    for (int i = 0; i < m_groupSet.length; i++) {
		        if ((m_groupSet[i].m_groupPathsParam == null) && 					// no groups
		        (m_groupSet[i].m_metricPath.branches.size() == 0) &&            	// no branches
		        (m_groupSet[i].m_metricPath.fieldType == FieldType.TIMESTAMP)) {	// TIMESTAMP field
			        Date date = null;
			        if (m_groupSet[i].m_metricPath.function.equals("MAX")) {
		                date = MaxMinHelper.getMaxDate(m_tableDef, m_groupSet[i].m_metricPath.name);
			        } else if (m_groupSet[i].m_metricPath.function.equals("MIN")) {
			            date = MaxMinHelper.getMinDate(m_tableDef, m_groupSet[i].m_metricPath.name);
			        }
			        if (date != null) {
			            m_groupSet[i].m_totalGroup.update(Utils.formatDateUTC(date.getTime()));
			            iGroupSet2Skip.add(i);
				    }
			    }			
			}
		}
		if (iGroupSet2Skip.size() == m_groupSet.length)
			return;
				
		Timer searchTimer = new Timer();
		timers.start("Search");

		QueryExecutor executor = new QueryExecutor(m_tableDef); 
		executor.setL2rEnabled(m_l2rEnable);
		Iterable<ObjectID> hits = executor.search(queryText);
//		ArrayList<ObjectID> hhh = new ArrayList<>();
//		for (ObjectID hit : hits) {
//			String id = hit.toString();
//			hhh.add(IDHelper.createID(id));
//		}

		if (separateSearchTiming) {
			ArrayList<ObjectID> hitList = new ArrayList<ObjectID>();
			for(ObjectID id : hits) hitList.add(id);
			hits = hitList;
			timers.stop("Search");
			log.debug("Search '{}' took {}", new Object[] { queryText, searchTimer });
		}

        timers.start("Aggregating");

		DBEntitySequenceFactory factory = new DBEntitySequenceFactory();
		EntitySequence collection = factory.getSequence(m_tableDef, hits, fieldNames);

        List<Set<String>[]> groupKeysSet = new ArrayList<Set<String>[]>();
        for (GroupSetEntry groupSetEntry : m_groupSet) {
            Set<String>[] groupKeys = new HashSet[groupSetEntry.m_groupPaths.length];
            for (int i = 0; i < groupKeys.length; i ++){
                groupKeys[i] = new HashSet<String>();
            }
            groupKeysSet.add(groupKeys);
        }

        m_totalObjects = 0;
		for (Entity obj : collection) {
		    m_totalObjects++;
		    for (int i = 0; i < m_groupSet.length; i++) {
                if (iGroupSet2Skip.contains(i))
                	continue;
		    	process(obj, m_groupSet[i].m_metricPath, m_groupSet[i], groupKeysSet.get(i));
		    }
		}
		
		// task #32,752 - include metrics for empty batches
		for (GroupSetEntry groupSetEntry : m_groupSet) {
	        boolean useNullGroup = !groupSetEntry.m_metricPath.function.equals("COUNT");
            addEmptyGroups(groupSetEntry.m_totalGroup, groupSetEntry.m_groupPaths, 0, useNullGroup);
            
            if (groupSetEntry.m_isComposite) {
            	addEmptyGroups(groupSetEntry.m_compositeGroup, groupSetEntry.m_groupPaths, 1, useNullGroup);
            }
		}

        // When a multi-metric aggregate query ...
        if (m_metricPaths.length > 1) { 
            if (m_groups != null) {
                int cMetrics = m_metricPaths.length;
                int cGroups = m_groups.size();
                for (int ig = 0; ig < cGroups; ig++) {
                    GroupSetEntry groupSetEntry = m_groupSet[ig];
                    // uses the TOP or BOTTOM function in the outer grouping field for the first metric function ...
                    if ((groupSetEntry.m_groupPaths[0].groupOutputParameters.function == Selection.Top) ||
                        (groupSetEntry.m_groupPaths[0].groupOutputParameters.function == Selection.Bottom)) {
                        List<Group> definedgroups = new ArrayList<Group>();
                        for (int im = 1; im < cMetrics; im++) {
                                definedgroups.add(m_groupSet[im * cGroups + ig].m_totalGroup);
                        }
                        // the outer and inner groups are selected on this metric function.
                        defineGroupsSelection(m_groupSet[ig].m_totalGroup, m_groupSet[ig].m_groupPaths, 0, definedgroups);
                    }
                }
            }
        }

        factory.timers.log();

		timers.stop("Aggregating");
		timers.log("Aggregate '%s'", m_mParamValue);
	}
1288788
@Override
  public Collection<Measurement> fetch() {
    Collection<Measurement> result = new ArrayList<>();
    Instant now = context.checkpoint();

    List<String> boltComponents = physicalPlanProvider.getBoltNames();
    Duration duration = getDuration();

    for (String component : boltComponents) {
      String[] boltInstanceNames = packingPlanProvider.getBoltInstanceNames(component);
      for (String instance : boltInstanceNames) {
        String metric = getMetricName() + instance + MetricName.METRIC_WAIT_Q_SIZE_SUFFIX;

        Collection<Measurement> stmgrResult
            = metricsProvider.getMeasurements(now, duration, metric, COMPONENT_STMGR);
        if (stmgrResult.isEmpty()) {
          continue;
        }

        MeasurementsTable table = MeasurementsTable.of(stmgrResult).component(COMPONENT_STMGR);
        if (table.size() == 0) {
          continue;
        }
        double totalSize = table.type(metric).sum();

        Measurement measurement
            = new Measurement(component, instance, getMetricName(), now, totalSize);
        result.add(measurement);
      }
    }

    return result;
  }
606658
private Deferred<Object> resolveMetric() {
    if (metric == null || metric.isEmpty()) {
      throw new IllegalArgumentException("The metric cannot be empty");
    }
    if (tags == null) {
      throw new IllegalArgumentException("Tags cannot be null or empty "
          + "when getting a TSUID");
    }
    
    class TagsCB implements Callback<Object, ArrayList<byte[]>> {
      @Override
      public Object call(final ArrayList<byte[]> tag_list) throws Exception {
        setTagUIDs(tag_list);
        return null;
      }
      @Override
      public String toString() {
        return "Tag resolution callback";
      }
    }
    
    class MetricCB implements Callback<Deferred<Object>, byte[]> {
      @Override
      public Deferred<Object> call(final byte[] uid) 
          throws Exception {
        setMetricUID(uid);
        if (tags.isEmpty()) {
          setTagUIDs(new ArrayList<byte[]>(0));
          return null;
        }
        return Tags.resolveAllAsync(tsdb, tags).addCallback(new TagsCB());
      }
      @Override
      public String toString() {
        return "Metric resolution callback";
      }
    }
    
    return tsdb.getUIDAsync(UniqueIdType.METRIC, metric)
        .addCallbackDeferring(new MetricCB());
  }
131839
static Map<String, Double> processMetrics(String prefix, Iterable<MetricsInfo> metrics) {
    Map<String, Double> map = new HashMap<>();
    for (MetricsInfo r : metrics) {
      try {
        map.put(prefix + r.getName(), Double.valueOf(r.getValue()));
      } catch (NumberFormatException ne) {
        LOG.log(Level.SEVERE, "Could not parse metric, Name: "
            + r.getName() + " Value: " + r.getValue(), ne);
      }
    }
    return map;
  }
937794
function(target, methodName, methodArgs, probeData, rc) {
        // If no callback has been used then end the metrics after returning from the method instead
        if (aspect.findCallbackArg(methodArgs) === undefined) {
          // Need to get request method and URL again
          var ri = getRequestItems(methodArgs[0]);
          // End metrics (no response available so pass empty object)
          that.metricsProbeEnd(probeData, ri.requestMethod, ri.urlRequested, {}, ri.headers);
          that.requestProbeEnd(probeData, ri.requestMethod, ri.urlRequested, {}, ri.headers);
        }
        return rc;
      }
157643
private void handleGetMetricHistory(final HttpServletRequest req,
      final Map<String, Object> ret) throws ServletException {
    if (MetricReportManager.isAvailable()) {
      final MetricReportManager metricManager = MetricReportManager.getInstance();
      final InMemoryMetricEmitter memoryEmitter =
          extractInMemoryMetricEmitter(metricManager);

      // if we have a memory emitter
      if (memoryEmitter != null) {
        try {
          final List<InMemoryHistoryNode> result =
              memoryEmitter.getMetrics(
                  getParam(req, STATS_MAP_METRICNAMEPARAM),
                  parseDate(getParam(req, STATS_MAP_STARTDATE)),
                  parseDate(getParam(req, STATS_MAP_ENDDATE)),
                  getBooleanParam(req, STATS_MAP_METRICRETRIEVALMODE));

          if (result != null && result.size() > 0) {
            ret.put("data", result);
          } else {
            ret.put(RESPONSE_ERROR, "No metric stats available");
          }

        } catch (final ParseException ex) {
          ret.put(RESPONSE_ERROR, "Invalid Date filter");
        }
      } else {
        ret.put(RESPONSE_ERROR, "InMemoryMetricEmitter not instantiated");
      }
    } else {
      ret.put(RESPONSE_ERROR, "MetricReportManager is not available");
    }
  }
733861
@Override
  public void reportMetric(final IMetric<?> metric) throws MetricException {
    final String gangliaCommand = buildCommand(metric);

    if (gangliaCommand != null) {
      // executes shell command to report metric to ganglia dashboard
      try {
        final Process emission = Runtime.getRuntime().exec(gangliaCommand);
        final int exitCode;
        exitCode = emission.waitFor();
        if (exitCode != 0) {
          throw new MetricException("Failed to report metric using gmetric");
        }
      } catch (final Exception e) {
        throw new MetricException("Failed to report metric using gmetric");
      }
    } else {
      throw new MetricException("Failed to build ganglia Command");
    }
  }
605767
public void readAndExport(MetricExporter metricExporter) {
    Span span =
        tracer
            .spanBuilder(spanName)
            .setRecordEvents(true)
            .setSampler(probabilitySampler)
            .startSpan();
    Scope scope = tracer.withSpan(span);
    try {
      ArrayList<Metric> metricsList = new ArrayList<>();
      for (MetricProducer metricProducer : metricProducerManager.getAllMetricProducer()) {
        metricsList.addAll(metricProducer.getMetrics());
      }
      metricExporter.export(metricsList);
    } catch (Throwable e) {
      logger.log(Level.WARNING, "Exception thrown by the metrics exporter.", e);
      span.setStatus(
          Status.UNKNOWN.withDescription("Exception when export metrics: " + exceptionMessage(e)));
    } finally {
      scope.close();
      span.end();
    }
  }
362035
private void initOutputs(ClassLoader cl) throws Exception {
		this.chainedTasks = new ArrayList<ChainedDriver<?, ?>>();
		this.eventualOutputs = new ArrayList<RecordWriter<?>>();

		this.output = BatchTask.initOutputs(this, cl, this.config, this.chainedTasks, this.eventualOutputs,
				getExecutionConfig(), getEnvironment().getAccumulatorRegistry().getUserMap());
	}