Search in sources :

Example 6 with TraceConfig

use of io.opencensus.trace.config.TraceConfig in project instrumentation-java by census-instrumentation.

the class HelloWorldServer method initTracing.

private static void initTracing() {
    TraceConfig traceConfig = Tracing.getTraceConfig();
    // default sampler is set to Samplers.alwaysSample() for demonstration. In production
    // or in high QPS environment please use default sampler.
    traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
    // Register LoggingTraceExporter to see traces in logs.
    LoggingTraceExporter.register();
    // Register Jaeger Tracing. Refer to https://www.jaegertracing.io/docs/1.8/getting-started/ to
    // run Jaeger
    JaegerTraceExporter.createAndRegister("http://localhost:14268/api/traces", "helloworldserver");
}
Also used : TraceConfig(io.opencensus.trace.config.TraceConfig)

Example 7 with TraceConfig

use of io.opencensus.trace.config.TraceConfig in project instrumentation-java by census-instrumentation.

the class MultiSpansScopedTracing method main.

/**
 * Main method.
 *
 * @param args the main arguments.
 */
public static void main(String[] args) {
    // WARNING: Be careful before you set sampler value to always sample, especially in
    // production environment. Trace data is often very large in size and is expensive to
    // collect. This is why rather than collecting traces for every request(i.e. alwaysSample),
    // downsampling is prefered.
    // 
    // By default, OpenCensus provides a probabilistic sampler that will trace once in every
    // 10,000 requests, that's why if default probabilistic sampler is used
    // you might not see trace data printed or exported and this is expected behavior.
    TraceConfig traceConfig = Tracing.getTraceConfig();
    traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
    LoggingTraceExporter.register();
    try (Scope ss = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startScopedSpan()) {
        doWork();
    }
    // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
    // Spans are exported every 5 seconds
    sleep(5100);
}
Also used : Scope(io.opencensus.common.Scope) TraceConfig(io.opencensus.trace.config.TraceConfig)

Example 8 with TraceConfig

use of io.opencensus.trace.config.TraceConfig in project instrumentation-java by census-instrumentation.

the class Application method initTracingAndLoggingExporter.

private static void initTracingAndLoggingExporter() {
    TraceConfig traceConfig = Tracing.getTraceConfig();
    traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
    LoggingTraceExporter.register();
}
Also used : TraceConfig(io.opencensus.trace.config.TraceConfig)

Example 9 with TraceConfig

use of io.opencensus.trace.config.TraceConfig in project instrumentation-java by census-instrumentation.

the class HelloWorldServer method main.

/**
 * 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);
    // For demo purposes, always sample
    TraceConfig traceConfig = Tracing.getTraceConfig();
    traceConfig.updateActiveTraceParams(traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
    // 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(5, 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();
}
Also used : HTTPServer(io.prometheus.client.exporter.HTTPServer) TraceConfig(io.opencensus.trace.config.TraceConfig)

Example 10 with TraceConfig

use of io.opencensus.trace.config.TraceConfig in project instrumentation-java by census-instrumentation.

the class BasicSetup method enableOpenCensus.

/**
 * Enables OpenCensus metric and traces.
 *
 * <p>This will register all basic {@link io.opencensus.stats.View}s. When coupled with an agent,
 * it allows users to monitor application behavior.
 *
 * <p>Example usage for maven:
 *
 * <pre>{@code
 * <dependency>
 *   <groupId>io.opencensus</groupId>
 *   <artifactId>opencensus-contrib-observability-ready-util</artifactId>
 *   <version>${opencensus.version}</version>
 * </dependency>
 * }</pre>
 *
 * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
 *
 * <pre>{@code
 * BasicSetup.enableOpenCensus();
 * }</pre>
 *
 * @param endPoint the end point of OC-Agent.
 * @param probability the desired probability of sampling. Must be within [0.0, 1.0].
 * @since 0.25
 */
public static void enableOpenCensus(String endPoint, double probability) {
    // register basic rpc views
    RpcViews.registerAllGrpcBasicViews();
    // set sampling rate
    TraceConfig traceConfig = Tracing.getTraceConfig();
    TraceParams activeTraceParams = traceConfig.getActiveTraceParams();
    traceConfig.updateActiveTraceParams(activeTraceParams.toBuilder().setSampler(Samplers.probabilitySampler(probability)).build());
    String serviceName = firstNonNull(System.getenv("SERVICE_NAME"), DEAFULT_SERVICE_NAME);
    // create and register Trace Agent Exporter
    OcAgentTraceExporter.createAndRegister(OcAgentTraceExporterConfiguration.builder().setEndPoint(endPoint).setServiceName(serviceName).setUseInsecure(true).setEnableConfig(false).build());
    // create and register Metrics Agent Exporter
    OcAgentMetricsExporter.createAndRegister(OcAgentMetricsExporterConfiguration.builder().setEndPoint(endPoint).setServiceName(serviceName).setUseInsecure(true).build());
}
Also used : TraceConfig(io.opencensus.trace.config.TraceConfig) TraceParams(io.opencensus.trace.config.TraceParams)

Aggregations

TraceConfig (io.opencensus.trace.config.TraceConfig)10 Scope (io.opencensus.common.Scope)3 TraceParams (io.opencensus.trace.config.TraceParams)3 Span (io.opencensus.trace.Span)2 ByteString (com.google.protobuf.ByteString)1 Node (io.opencensus.proto.agent.common.v1.Node)1 ExportTraceServiceRequest (io.opencensus.proto.agent.trace.v1.ExportTraceServiceRequest)1 HTTPServer (io.prometheus.client.exporter.HTTPServer)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1