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");
}
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);
}
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();
}
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();
}
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());
}
Aggregations