Enables metrics for the server by setting "opt-out" to false in the config file and starting the metrics task.

@throws java.io.IOException
974788
public void enable() throws IOException {
        // This has to be synchronized or it can collide with the check in the task.
        synchronized (optOutLock) {
            // Check if the server owner has already set opt-out, if not, set it.
            if (isOptOut()) {
                properties.setProperty("opt-out", "false");
                properties.store(new FileOutputStream(configurationFile), "http://mcstats.org");
            }

            // Enable Task, if it is not running
            if (thread == null) {
                start();
            }
        }
    }
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");
    }
  }
88797
public static void startSinks(String metricsConfFile) {
    synchronized (MetricsSystem.class) {
      if (sSinks != null) {
        LOG.info("Sinks have already been started.");
        return;
      }
    }
    if (metricsConfFile.isEmpty()) {
      LOG.info("Metrics is not enabled.");
      return;
    }
    MetricsConfig config = new MetricsConfig(metricsConfFile);
    startSinksFromConfig(config);
  }
507035
public int run() throws IOException {
    mMetricsMap = new TreeMap<>(mMetaMasterClient.getMetrics());
    Long bytesReadLocal =
        mMetricsMap.getOrDefault(MetricsSystem.getClusterMetricName(ClientMetrics.BYTES_READ_LOCAL),
            MetricValue.newBuilder().setLongValue(0L).build()).getLongValue();
    Long bytesReadRemote = mMetricsMap
        .getOrDefault(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_READ_ALLUXIO),
            MetricValue.newBuilder().setLongValue(0L).build())
        .getLongValue();
    Long bytesReadUfs = mMetricsMap
        .getOrDefault(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_READ_UFS_ALL),
            MetricValue.newBuilder().setLongValue(0L).build())
        .getLongValue();

    mPrintStream.println("Total IO: ");
    printMetric(MetricsSystem.getClusterMetricName(ClientMetrics.BYTES_READ_LOCAL),
        "Short-circuit Read", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_READ_ALLUXIO),
        "From Remote Instances", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_READ_UFS_ALL),
        "Under Filesystem Read", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_WRITTEN_ALLUXIO),
        "Alluxio Write", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_WRITTEN_UFS_ALL),
        "Under Filesystem Write", true);

    mPrintStream.println("\nTotal IO Throughput (Last Minute): ");
    printMetric(MetricsSystem.getClusterMetricName(ClientMetrics.BYTES_READ_LOCAL_THROUGHPUT),
        "Short-circuit Read", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_READ_ALLUXIO_THROUGHPUT),
        "From Remote Instances", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_READ_UFS_THROUGHPUT),
        "Under Filesystem Read", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_WRITTEN_ALLUXIO_THROUGHPUT),
        "Alluxio Write", true);
    printMetric(MetricsSystem.getClusterMetricName(WorkerMetrics.BYTES_WRITTEN_UFS_THROUGHPUT),
        "Under Filesystem Write", true);

    mPrintStream.println("\nCache Hit Rate (Percentage): ");
    long bytesReadTotal = bytesReadLocal + bytesReadRemote + bytesReadUfs;
    String cacheHitLocalPercentage = String.format("%.2f",
        (bytesReadTotal > 0) ? (100D * bytesReadLocal / bytesReadTotal) : 0);
    String cacheHitRemotePercentage = String.format("%.2f",
        (bytesReadTotal > 0) ? (100D * bytesReadRemote / bytesReadTotal) : 0);
    String cacheMissPercentage = String.format("%.2f",
        (bytesReadTotal > 0) ? (100D * bytesReadUfs / bytesReadTotal) : 0);

    mPrintStream.println(INDENT
        + String.format(mInfoFormat, "Alluxio Local", cacheHitLocalPercentage));
    mPrintStream.println(INDENT
        + String.format(mInfoFormat, "Alluxio Remote", cacheHitRemotePercentage));
    mPrintStream.println(INDENT
        + String.format(mInfoFormat, "Miss", cacheMissPercentage));

    mPrintStream.println("\nLogical Operations: ");
    printMetric(MasterMetrics.DIRECTORIES_CREATED, "Directories Created", false);
    printMetric(MasterMetrics.FILE_BLOCK_INFOS_GOT, "File Block Infos Got", false);
    printMetric(MasterMetrics.FILE_INFOS_GOT, "File Infos Got", false);
    printMetric(MasterMetrics.FILES_COMPLETED, "Files Completed", false);
    printMetric(MasterMetrics.FILES_CREATED, "Files Created", false);
    printMetric(MasterMetrics.FILES_FREED, "Files Freed", false);
    printMetric(MasterMetrics.FILES_PERSISTED, "Files Persisted", false);
    printMetric(MasterMetrics.NEW_BLOCKS_GOT, "New Blocks Got", false);
    printMetric(MasterMetrics.PATHS_DELETED, "Paths Deleted", false);
    printMetric(MasterMetrics.PATHS_MOUNTED, "Paths Mounted", false);
    printMetric(MasterMetrics.PATHS_RENAMED, "Paths Renamed", false);
    printMetric(MasterMetrics.PATHS_UNMOUNTED, "Paths Unmounted", false);

    mPrintStream.println("\nRPC Invocations: ");
    printMetric(MasterMetrics.COMPLETE_FILE_OPS, "Complete File Operations", false);
    printMetric(MasterMetrics.CREATE_DIRECTORIES_OPS, "Create Directory Operations", false);
    printMetric(MasterMetrics.CREATE_FILES_OPS, "Create File Operations", false);
    printMetric(MasterMetrics.DELETE_PATHS_OPS, "Delete Path Operations", false);
    printMetric(MasterMetrics.FREE_FILE_OPS, "Free File Operations", false);
    printMetric(MasterMetrics.GET_FILE_BLOCK_INFO_OPS, "Get File Block Info Operations", false);
    printMetric(MasterMetrics.GET_FILE_INFO_OPS, "Get File Info Operations", false);
    printMetric(MasterMetrics.GET_NEW_BLOCK_OPS, "Get New Block Operations", false);
    printMetric(MasterMetrics.MOUNT_OPS, "Mount Operations", false);
    printMetric(MasterMetrics.RENAME_PATH_OPS, "Rename Path Operations", false);
    printMetric(MasterMetrics.SET_ACL_OPS, "Set ACL Operations", false);
    printMetric(MasterMetrics.SET_ATTRIBUTE_OPS, "Set Attribute Operations", false);
    printMetric(MasterMetrics.UNMOUNT_OPS, "Unmount Operations", false);

    // TODO(lu) improve printout info to sync with web UI
    mPrintStream.println("\nOther Metrics: ");
    mInfoFormat = "%s  (%s)"; // Some property names are too long to fit in previous info format
    for (Map.Entry<String, MetricValue> entry : mMetricsMap.entrySet()) {
      mPrintStream.println(INDENT + String.format(mInfoFormat,
          entry.getKey(), getFormattedValue(entry.getValue())));
    }
    return 0;
  }
1098344
public void init() {
        try {
            initPlugin();
        } catch (RuntimeException e) {
            LOG.error("init metrics plugin error:", e);
            System.exit(-1);
        }

        pushRefreshEvent();
        pushFlushEvent();
        pushMergeEvent();
        pushUploadEvent();
        pushDiagnosisEvent();
        LOG.info("Finish");
    }
866855
public void setAppServerDefaultDeploymentScanEnabled(boolean enabled) throws Exception {
        final String[] addressArr = { SUBSYSTEM, DEPLOYMENT_SCANNER, SCANNER, "default" };
        final Address address = Address.root().add(addressArr);
        final ModelNode req = createWriteAttributeRequest("scan-enabled", Boolean.toString(enabled), address);
        final ModelNode response = execute(req);

        if (!isSuccess(response)) {
            throw new FailureException(response);
        }
        return;
    }
469366
public void startMetricReporting(Properties properties) {
    if (this.metricsReportingStarted) {
      LOGGER.warn("Metric reporting has already started");
      return;
    }

    TimeUnit reportTimeUnit = TimeUnit.MILLISECONDS;
    long reportInterval = Long.parseLong(properties
        .getProperty(ConfigurationKeys.METRICS_REPORT_INTERVAL_KEY, ConfigurationKeys.DEFAULT_METRICS_REPORT_INTERVAL));
    ScheduledReporter.setReportingInterval(properties, reportInterval, reportTimeUnit);

    long startTime = System.currentTimeMillis();

    try {
      // Build and start the JMX reporter
      buildJmxMetricReporter(properties);
      if (this.jmxReporter.isPresent()) {
        LOGGER.info("Will start reporting metrics to JMX");
        this.jmxReporter.get().start();
      }

      // Build all other reporters
      buildFileMetricReporter(properties);
      buildKafkaMetricReporter(properties);
      buildGraphiteMetricReporter(properties);
      buildInfluxDBMetricReporter(properties);
      buildCustomMetricReporters(properties);
      buildFileFailureEventReporter(properties);

      // Start reporters that implement org.apache.gobblin.metrics.report.ScheduledReporter
      RootMetricContext.get().startReporting();

      // Start reporters that implement com.codahale.metrics.ScheduledReporter
      for (com.codahale.metrics.ScheduledReporter scheduledReporter : this.codahaleScheduledReporters) {
        scheduledReporter.start(reportInterval, reportTimeUnit);
      }
    } catch (Exception e) {
      LOGGER.error("Metrics reporting cannot be started due to {}", ExceptionUtils.getFullStackTrace(e));
      throw e;
    }

    this.metricsReportingStarted = true;
    LOGGER.info("Metrics reporting has been started in {} ms: GobblinMetrics {}",
        System.currentTimeMillis() - startTime, this.toString());
  }
1026334
public void initialize(URI name, Configuration conf) throws IOException {
    statistics = getStatistics(name.getScheme(), getClass());    
  }
65012
protected void initialize() throws IOException {
    // set service-level authorization security policy
    if (serviceAuthEnabled =
        getConf().getBoolean(
            ServiceAuthorizationManager.SERVICE_AUTHORIZATION_CONFIG, false)) {
      PolicyProvider policyProvider =
        (PolicyProvider)(ReflectionUtils.newInstance(
            getConf().getClass(PolicyProvider.POLICY_PROVIDER_CONFIG,
                HDFSPolicyProvider.class, PolicyProvider.class),
            getConf()));
      SecurityUtil.setPolicy(new ConfiguredPolicy(getConf(), policyProvider));
    }

    // This is a check that the port is free
    // create a socket and bind to it, throw exception if port is busy
    // This has to be done before we are reading Namesystem not to waste time and fail fast
    NetUtils.isSocketBindable(getClientProtocolAddress(getConf()));
    NetUtils.isSocketBindable(getDNProtocolAddress(getConf()));
    NetUtils.isSocketBindable(getHttpServerAddress(getConf()));

    long serverVersion = ClientProtocol.versionID;
    this.clientProtocolMethodsFingerprint = ProtocolSignature
        .getMethodsSigFingerPrint(ClientProtocol.class, serverVersion);

    myMetrics = new NameNodeMetrics(getConf(), this);

    this.clusterName = getConf().get(FSConstants.DFS_CLUSTER_NAME);
    this.namesystem = new FSNamesystem(this, getConf());
    // HACK: from removal of FSNamesystem.getFSNamesystem().
    JspHelper.fsn = this.namesystem;

    this.startDNServer();
    startHttpServer(getConf());
  }
1191359
public static void run(final Properties properties) throws IOException {
        // read parameters for output (do this at the beginning to avoid unnecessary reading)
        File outputFile = new File(properties.getProperty(OUTPUT_FILE));
        Boolean overwrite = Boolean.parseBoolean(properties.getProperty(OUTPUT_OVERWRITE, "false"));
        PrintStream outStatistics = null;
        if (outputFile.exists() && !overwrite) {
            throw new IllegalArgumentException("Cannot generate statistics because " + outputFile + " exists and overwrite is " + overwrite);
        } else {
            outStatistics = new PrintStream(outputFile, "UTF-8");
        }
        // read format
        String format = properties.getProperty(INPUT_FORMAT);
        // read users to avoid
        String[] usersToAvoidArray = properties.getProperty(AVOID_USERS, "").split(",");
        Set<String> usersToAvoid = new HashSet<String>();
        for (String u : usersToAvoidArray) {
            usersToAvoid.add(u);
        }
        try {
            // read baseline <-- this file is mandatory
            File baselineFile = new File(properties.getProperty(BASELINE_FILE));
            Map<String, Map<String, Double>> baselineMapMetricUserValues = readMetricFile(baselineFile, format, usersToAvoid);
            // read methods <-- at least one file should be provided
            String[] methodFiles = properties.getProperty(TEST_METHODS_FILES).split(",");
            if (methodFiles.length < 1) {
                throw new IllegalArgumentException("At least one test file should be provided!");
            }
            Map<String, Map<String, Map<String, Double>>> methodsMapMetricUserValues = new HashMap<String, Map<String, Map<String, Double>>>();
            for (String m : methodFiles) {
                File file = new File(m);
                Map<String, Map<String, Double>> mapMetricUserValues = readMetricFile(file, format, usersToAvoid);
                methodsMapMetricUserValues.put(m, mapMetricUserValues);
            }
            run(properties, outStatistics, baselineFile.getName(), baselineMapMetricUserValues, methodsMapMetricUserValues);
        } finally {
            // close files
            outStatistics.close();
        }
    }
504140
@SuppressWarnings("FutureReturnValueIgnored")
  public void reportMetric(final IMetric<?> metric) {
    if (metric != null && isAvailable()) {
      try {
        final IMetric<?> metricSnapshot;
        // take snapshot
        synchronized (metric) {
          metricSnapshot = metric.getSnapshot();
        }
        logger.debug(String
            .format("Submitting %s metric for metric emission pool", metricSnapshot.getName()));
        // report to all emitters
        for (final IMetricEmitter metricEmitter : this.metricEmitters) {
          this.executorService.submit(() -> {
            try {
              metricEmitter.reportMetric(metricSnapshot);
            } catch (final Exception ex) {
              logger.error(
                  String.format("Failed to report %s metric due to ", metricSnapshot.getName()),
                  ex);
            }
          });
        }
      } catch (final CloneNotSupportedException ex) {
        logger.error(
            String.format("Failed to take snapshot for %s metric", metric.getClass().getName()),
            ex);
      }
    }
  }
273373
private void loadConfigFile(String configFile) {
    try (InputStream is = new FileInputStream(configFile)) {
      mProperties.load(is);
    } catch (Exception e) {
      LOG.error("Error loading metrics configuration file.", e);
    }
  }
297735
public static void enable() {
        final MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
        synchronized (metaClassRegistry) {
            if (metaClassRegistry.getMetaClassCreationHandler() != instance) {
                ClassInfo.clearModifiedExpandos();
                metaClassRegistry.setMetaClassCreationHandle(instance);
            }
        }
    }
519138
public void start() throws Exception {
        Exceptions.checkNotClosed(this.closed, this);

        log.info("Initializing metrics provider ...");
        MetricsProvider.initialize(builderConfig.getConfig(MetricsConfig::builder));
        statsProvider = MetricsProvider.getMetricsProvider();
        statsProvider.start();

        log.info("Initializing ZooKeeper Client ...");
        this.zkClient = createZKClient();

        log.info("Initializing Service Builder ...");
        this.serviceBuilder.initialize();

        log.info("Creating StreamSegmentService ...");
        StreamSegmentStore service = this.serviceBuilder.createStreamSegmentService();

        log.info("Creating TableStoreService ...");
        TableStore tableStoreService = this.serviceBuilder.createTableStoreService();

        log.info("Creating Segment Stats recorder ...");
        autoScaleMonitor = new AutoScaleMonitor(service, builderConfig.getConfig(AutoScalerConfig::builder));

        TokenVerifierImpl tokenVerifier = new TokenVerifierImpl(builderConfig.getConfig(AutoScalerConfig::builder));

        // Log the configuration
        log.info(serviceConfig.toString());
        log.info(builderConfig.getConfig(AutoScalerConfig::builder).toString());

        this.listener = new PravegaConnectionListener(this.serviceConfig.isEnableTls(), this.serviceConfig.getListeningIPAddress(),
                                                      this.serviceConfig.getListeningPort(), service, tableStoreService,
                                                      autoScaleMonitor.getStatsRecorder(), autoScaleMonitor.getTableSegmentStatsRecorder(),
                                                      tokenVerifier, this.serviceConfig.getCertFile(), this.serviceConfig.getKeyFile(),
                                                      this.serviceConfig.isReplyWithStackTraceOnError());

        this.listener.startListening();
        log.info("PravegaConnectionListener started successfully.");
        log.info("StreamSegmentService started.");
    }
294798
public void enableServletCacheStats() {
        final String methodName = "enableServletCacheStats";
        try {
            if (null == _statsInstance) {

                _iCacheType = TYPE_SERVLET;

                if (tc.isDebugEnabled())
                    Tr.debug(tc, methodName + " Creating statistic for " + _sCacheName + " instance (servlet cache)");
                _statsInstance = StatsFactory.createStatsInstance(WSDynamicCacheStats.SERVLET_CACHE_TYPE_PREFIX + _sCacheName, _cacheRootStatsGroup,
                        null, this);
                if (_diskOffloadEnabled) {
                    if (_cacheDiskStatsGroup == null) {
                        if (tc.isDebugEnabled())
                            Tr.debug(tc, methodName + " Creating disk group" + " for cacheName=" + _sCacheName);
                        _cacheDiskStatsGroup = StatsFactory.createStatsGroup(WSDynamicCacheStats.DISK_GROUP, templateCount13, _statsInstance, null,
                                this);
                    }
                    _csmDisk = new CacheStatsModule(_sCacheName, WSDynamicCacheStats.DISK_OFFLOAD_ENABLED, _cacheDiskStatsGroup, this);
                }
            }
        } catch (StatsFactoryException e) {
            com.ibm.ws.ffdc.FFDCFilter.processException(e, "com.ibm.ws.cache.stat.CacheStatsModule", "198", this);
            if (tc.isDebugEnabled())
                Tr.debug(tc, methodName + " Exception while enabling servlet cache stats template - cacheName=" + _sCacheName + ": "
                        + ExceptionUtility.getStackTrace(e));
        }
    }
1049479
protected void configureMetrics() {
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS, ESMetrics.class.getName());
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS + ".client.type", "jest");
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS + ".client.protocol", "${apiman.es.protocol}");
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS + ".client.host", "${apiman.es.host}");
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS + ".client.port", "${apiman.es.port}");
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS + ".client.username", "${apiman.es.username}");
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS + ".client.password", "${apiman.es.password}");
        setConfigProperty(GatewayConfigProperties.METRICS_CLASS + ".client.index", "apiman_metrics");
    }
1027367
void enableManagement(boolean enabled) {
    requireNotClosed();

    synchronized (configuration) {
      if (enabled) {
        JmxRegistration.registerMXBean(this, cacheMXBean, MBeanType.Configuration);
      } else {
        JmxRegistration.unregisterMXBean(this, MBeanType.Configuration);
      }
      configuration.setManagementEnabled(enabled);
    }
  }