io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter Java Examples

The following examples show how to use io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter. 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: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
public static boolean setupStackdriverTraceExporter() {
  if (enableTelemetry()) {
    try {
      URL url = Resources.getResource(CREDENTIALS_JSON);
      StackdriverTraceExporter.createAndRegister(
          StackdriverTraceConfiguration.builder()
              .setProjectId(PROJECT_ID)
              .setCredentials(ServiceAccountCredentials.fromStream(url.openStream()))
              .build());
      log.info("enable stackdriver trace exporter");
      return true;
    } catch (Throwable e) {
      log.warn("{}", e.getMessage());
    }
  }
  return false;
}
 
Example #2
Source File: StackDriverConfigurator.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public Tracer getTracer() {
  try {
    StackdriverTraceExporter.createAndRegister(StackdriverTraceConfiguration.builder().build());
  } catch (IOException e) {
    logger.warn("Could not setup stackdriver tracer", e);
  }
  return new OpenCensusTracerAdapter(Tracing.getTracer());
}
 
Example #3
Source File: StyxService.java    From styx with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws LoadingException, IOException {
  StackdriverTraceExporter.createAndRegister(
      StackdriverTraceConfiguration.builder().build());

  final AppInit init = (env) -> {
    final MetricsStats stats =
        new MetricsStats(env.resolve(SemanticMetricRegistry.class), Instant::now);
    final StatsFactory statsFactory = (ignored) -> stats;

    final AuthenticatorFactory authenticatorFactory =
        Function1.of(AuthenticatorFactory.DEFAULT::apply).memoized()::apply;

    final StyxScheduler scheduler = StyxScheduler.newBuilder()
        .setServiceName(SERVICE_NAME)
        .setStatsFactory(statsFactory)
        .setAuthenticatorFactory(authenticatorFactory)
        .build();
    final StyxApi api = StyxApi.newBuilder()
        .setServiceName(SERVICE_NAME)
        .setStatsFactory(statsFactory)
        .setAuthenticatorFactory(authenticatorFactory)
        .build();

    scheduler.create(env);
    api.create(env);
  };

  HttpService.boot(init, SERVICE_NAME, args);
}
 
Example #4
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 #5
Source File: TraceSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void createAndRegisterWithToken(String accessToken) throws IOException {
  Date expirationTime = DateTime.now().plusSeconds(60).toDate();

  GoogleCredentials credentials =
      GoogleCredentials.create(new AccessToken(accessToken, expirationTime));
  StackdriverTraceExporter.createAndRegister(
      StackdriverTraceConfiguration.builder()
          .setProjectId("MyStackdriverProjectId")
          .setCredentials(credentials)
          .build());
}
 
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: TraceSample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void createAndRegister() throws IOException {
  StackdriverTraceExporter.createAndRegister(StackdriverTraceConfiguration.builder().build());
}
 
Example #8
Source File: TraceSample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void createAndRegisterGoogleCloudPlatform(String projectId) throws IOException {
  StackdriverTraceExporter.createAndRegister(
      StackdriverTraceConfiguration.builder().setProjectId(projectId).build());
}
 
Example #9
Source File: TraceSampleIT.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
  StackdriverTraceExporter.unregister();
}
 
Example #10
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);
  }
}