io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration Java Examples

The following examples show how to use io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StackdriverMetrics.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
private void ensureStackdriver(Gradle gradle) {
  // make sure we only initialize stackdriver once as gradle daemon is not guaranteed to restart
  // across gradle invocations.
  if (!STACKDRIVER_INITIALIZED.compareAndSet(false, true)) {
    logger.lifecycle("Stackdriver exporter already initialized.");
    return;
  }
  logger.lifecycle("Initializing Stackdriver exporter.");

  try {
    StackdriverStatsExporter.createAndRegister(
        StackdriverStatsConfiguration.builder()
            .setExportInterval(Duration.fromMillis(STACKDRIVER_UPLOAD_PERIOD_MS))
            .build());

    // make sure gradle does not exit before metrics get uploaded to stackdriver.
    gradle.addBuildListener(new DrainingBuildListener(STACKDRIVER_UPLOAD_PERIOD_MS, logger));
  } catch (IOException e) {
    throw new GradleException("Could not configure metrics exporter", e);
  }
}
 
Example #2
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
public static boolean setupStackdriverStatsExporter() {
  if (enableTelemetry()) {
    try {
      registerAllViews();
      URL url = Resources.getResource(CREDENTIALS_JSON);
      StackdriverStatsExporter.createAndRegister(
          StackdriverStatsConfiguration.builder()
              .setProjectId(PROJECT_ID)
              .setCredentials(ServiceAccountCredentials.fromStream(url.openStream()))
              .build());
      log.info("enable stackdriver stats exporter");
      return true;
    } catch (Throwable e) {
      log.warn("{}", e.getMessage());
    }
  }
  return false;
}
 
Example #3
Source File: OpenCensusPluginSink.java    From ffwd with Apache License 2.0 6 votes vote down vote up
public AsyncFuture<Void> start() {
  // NB using Application Default Credentials here:
  // See https://developers.google.com/identity/protocols/application-default-credentials
  try {
    StackdriverStatsConfiguration.Builder builder = StackdriverStatsConfiguration.builder();

    // We clear out the constant labels because otherwise they are populated with
    // values from this VM which is unlikely to be what we want.
    builder.setConstantLabels(Collections.emptyMap());

    // This can also be done by setting enviroment variables but it'll be frequently
    // be used so let's make it easy.
    if (gcpProject.isPresent()) {
      builder.setProjectId(gcpProject.get());
    }

    StackdriverStatsExporter.createAndRegister(builder.build());
  } catch (IOException ex) {
    log.error("Couldn't connect to Stackdriver");
    return async.failed(ex);
  }
  return async.resolved();
}
 
Example #4
Source File: GoogleDtpInternalMetricRecorder.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
private GoogleDtpInternalMetricRecorder(GoogleCredentials credentials, String projectId)
    throws IOException {
  // Enable OpenCensus exporters to export metrics to Stackdriver Monitoring.
  // Exporters use Application Default Credentials to authenticate.
  // See https://developers.google.com/identity/protocols/application-default-credentials
  // for more details.
  StackdriverStatsConfiguration configuration = StackdriverStatsConfiguration.builder()
      .setCredentials(credentials)
      .setProjectId(projectId)
      .setExportInterval(io.opencensus.common.Duration.create(EXPORT_INTERVAL_SECONDS, 0))
      .build();
  StackdriverStatsExporter.createAndRegister(configuration);
  this.viewManager = Stats.getViewManager();
  setupViews();
}
 
Example #5
Source File: HelloWorldServer.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Main launches the server from the command line. */
public static void main(String[] args) throws IOException, InterruptedException {
  // Add final keyword to pass checkStyle.
  final int serverPort = getPortOrDefaultFromArgs(args, 0, 50051);
  final String cloudProjectId = getStringOrDefaultFromArgs(args, 1, null);
  final int zPagePort = getPortOrDefaultFromArgs(args, 2, 3000);
  final int prometheusPort = getPortOrDefaultFromArgs(args, 3, 9090);

  // Registers all RPC views. For demonstration all views are registered. You may want to
  // start with registering basic views and register other views as needed for your application.
  RpcViews.registerAllViews();

  // Registers logging trace exporter.
  LoggingTraceExporter.register();

  // Starts a HTTP server and registers all Zpages to it.
  ZPageHandlers.startHttpServerAndRegisterAll(zPagePort);
  logger.info("ZPages server starts at localhost:" + zPagePort);

  // Registers Stackdriver exporters.
  if (cloudProjectId != null) {
    StackdriverTraceExporter.createAndRegister(
        StackdriverTraceConfiguration.builder().setProjectId(cloudProjectId).build());
    StackdriverStatsExporter.createAndRegister(
        StackdriverStatsConfiguration.builder()
            .setProjectId(cloudProjectId)
            .setExportInterval(Duration.create(60, 0))
            .build());
  }

  // Register Prometheus exporters and export metrics to a Prometheus HTTPServer.
  PrometheusStatsCollector.createAndRegister();
  HTTPServer prometheusServer = new HTTPServer(prometheusPort, true);

  // Start the RPC server. You shouldn't see any output from gRPC before this.
  logger.info("gRPC starting.");
  final HelloWorldServer server = new HelloWorldServer(serverPort);
  server.start();
  server.blockUntilShutdown();
}
 
Example #6
Source File: HelloWorldClient.java    From opencensus-java with Apache License 2.0 4 votes vote down vote up
/**
 * Greet server. If provided, the first element of {@code args} is the name to use in the
 * greeting.
 */
public static void main(String[] args) throws IOException, InterruptedException {
  // Add final keyword to pass checkStyle.
  final String user = getStringOrDefaultFromArgs(args, 0, "world");
  final String host = getStringOrDefaultFromArgs(args, 1, "localhost");
  final int serverPort = getPortOrDefaultFromArgs(args, 2, 50051);
  final String cloudProjectId = getStringOrDefaultFromArgs(args, 3, null);
  final int zPagePort = getPortOrDefaultFromArgs(args, 4, 3001);

  // Registers all RPC views. For demonstration all views are registered. You may want to
  // start with registering basic views and register other views as needed for your application.
  RpcViews.registerAllViews();

  // Starts a HTTP server and registers all Zpages to it.
  ZPageHandlers.startHttpServerAndRegisterAll(zPagePort);
  logger.info("ZPages server starts at localhost:" + zPagePort);

  // Registers logging trace exporter.
  LoggingTraceExporter.register();

  // Registers Stackdriver exporters.
  if (cloudProjectId != null) {
    StackdriverTraceExporter.createAndRegister(
        StackdriverTraceConfiguration.builder().setProjectId(cloudProjectId).build());
    StackdriverStatsExporter.createAndRegister(
        StackdriverStatsConfiguration.builder()
            .setProjectId(cloudProjectId)
            .setExportInterval(Duration.create(60, 0))
            .build());
  }

  // Register Prometheus exporters and export metrics to a Prometheus HTTPServer.
  PrometheusStatsCollector.createAndRegister();

  HelloWorldClient client = new HelloWorldClient(host, serverPort);
  try {
    client.greet(user);
  } finally {
    client.shutdown();
  }

  logger.info("Client sleeping, ^C to exit. Meanwhile you can view stats and spans on zpages.");
  while (true) {
    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
      logger.info("Exiting HelloWorldClient...");
    }
  }
}
 
Example #7
Source File: HelloWorld.java    From cloud-bigtable-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
  // Consult system properties to get project/instance
  String projectId = requiredProperty("bigtable.projectID");
  String instanceId = requiredProperty("bigtable.instanceID");

  try {
    // Force tracing for every request for demo purposes.  Use the default settings
    // in most cases.
    Tracing.getTraceConfig().updateActiveTraceParams(
        TraceParams.DEFAULT.toBuilder().setSampler(Samplers.probabilitySampler(1)).build());

    StackdriverTraceExporter.createAndRegister(
        StackdriverTraceConfiguration.builder()
            .setProjectId(projectId)
            .build());

    // Enable stats exporter to Stackdriver with a 5 second export time.
    // Production settings may vary.
    StackdriverStatsExporter.createAndRegister(
          StackdriverStatsConfiguration.builder()
              .setProjectId(projectId)
              .setExportInterval(Duration.create(5, 0))
              .build());

    RpcViews.registerAllGrpcViews();

    // HBase Bigtable specific setup for zpages
    HBaseTracingUtilities.setupTracingConfig();

    // Start a web server on port 8080 for tracing data
    ZPageHandlers.startHttpServerAndRegisterAll(8080);

    doHelloWorld(projectId, instanceId);

  } finally {
    System.out.println("Sleeping for 1 minute so that you can view http://localhost:8080/tracez");

    // Sleep for 1 minute.
    TimeUnit.MINUTES.sleep(1);

    // Terminating this program manually as Http-server prevents it from auto shutdown
    System.exit(0);
  }
}