io.opencensus.trace.SpanBuilder Java Examples
The following examples show how to use
io.opencensus.trace.SpanBuilder.
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: HelloWorldClient.java From opencensus-java with Apache License 2.0 | 6 votes |
/** Say hello to server. */ public void greet(String name) { logger.info("Will try to greet " + name + " ..."); HelloRequest request = HelloRequest.newBuilder().setName(name).build(); HelloReply response; SpanBuilder spanBuilder = tracer.spanBuilder("client").setRecordEvents(true).setSampler(Samplers.alwaysSample()); try (Scope scope = spanBuilder.startScopedSpan()) { tracer.getCurrentSpan().addAnnotation("Saying Hello to Server."); response = blockingStub.sayHello(request); tracer.getCurrentSpan().addAnnotation("Received response from Server."); } catch (StatusRuntimeException e) { tracer .getCurrentSpan() .setStatus( CanonicalCode.valueOf(e.getStatus().getCode().name()) .toStatus() .withDescription(e.getMessage())); logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus()); return; } logger.info("Greeting: " + response.getMessage()); }
Example #2
Source File: HttpClientHandler.java From opencensus-java with Apache License 2.0 | 6 votes |
/** * Instrument a request for tracing and stats before it is sent. * * <p>This method will create a span in current context to represent the HTTP call. The created * span will be serialized and propagated to the server. * * <p>The generated span will NOT be set as current context. User can control when to enter the * scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span. * * @param parent the parent {@link Span}. {@code null} indicates using current span. * @param carrier the entity that holds the HTTP information. * @param request the request entity. * @return the {@link HttpRequestContext} that contains stats and trace data associated with the * request. * @since 0.19 */ public HttpRequestContext handleStart(@Nullable Span parent, C carrier, Q request) { checkNotNull(carrier, "carrier"); checkNotNull(request, "request"); if (parent == null) { parent = tracer.getCurrentSpan(); } String spanName = getSpanName(request, extractor); SpanBuilder builder = tracer.spanBuilderWithExplicitParent(spanName, parent); Span span = builder.setSpanKind(Kind.CLIENT).startSpan(); if (span.getOptions().contains(Options.RECORD_EVENTS)) { addSpanRequestAttributes(span, request, extractor); } // inject propagation header SpanContext spanContext = span.getContext(); if (!spanContext.equals(SpanContext.INVALID)) { textFormat.inject(spanContext, carrier, setter); } return getNewContext(span, tagger.getCurrentTagContext()); }
Example #3
Source File: TracingProxy.java From styx with Apache License 2.0 | 5 votes |
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { final String operation = method.getName(); final SpanBuilder spanBuilder = tracer.spanBuilder(delegateName + "." + operation); try (Scope ignored = spanBuilder.startScopedSpan()) { try { return method.invoke(delegate, args); } catch (InvocationTargetException e) { tracer.getCurrentSpan().addAnnotation("Exception thrown"); tracer.getCurrentSpan().setStatus(Status.UNKNOWN); throw e.getTargetException(); } } }
Example #4
Source File: QuickStart.java From opencensus-java with Apache License 2.0 | 5 votes |
/** Main launcher for the QuickStart example. */ public static void main(String[] args) throws InterruptedException { TagContextBuilder tagContextBuilder = tagger.currentBuilder().put(FRONTEND_KEY, TagValue.create("mobile-ios9.3.5")); SpanBuilder spanBuilder = tracer .spanBuilder("my.org/ProcessVideo") .setRecordEvents(true) .setSampler(Samplers.alwaysSample()); viewManager.registerView(VIDEO_SIZE_VIEW); LoggingTraceExporter.register(); // Process video. // Record the processed video size. try (Scope scopedTags = tagContextBuilder.buildScoped(); Scope scopedSpan = spanBuilder.startScopedSpan()) { tracer.getCurrentSpan().addAnnotation("Start processing video."); // Sleep for [0,10] milliseconds to fake work. Thread.sleep(new Random().nextInt(10) + 1); statsRecorder.newMeasureMap().put(VIDEO_SIZE, 25 * MiB).record(); tracer.getCurrentSpan().addAnnotation("Finished processing video."); } catch (Exception e) { tracer.getCurrentSpan().addAnnotation("Exception thrown when processing video."); tracer.getCurrentSpan().setStatus(Status.UNKNOWN); logger.severe(e.getMessage()); } logger.info("Wait longer than the reporting duration..."); // Wait for a duration longer than reporting duration (5s) to ensure spans are exported. // TODO(songya): remove the gap once we add a shutdown hook for exporting unflushed spans. Thread.sleep(5100); ViewData viewData = viewManager.getView(VIDEO_SIZE_VIEW_NAME); logger.info( String.format("Recorded stats for %s:\n %s", VIDEO_SIZE_VIEW_NAME.asString(), viewData)); }
Example #5
Source File: HelloWorldServer.java From opencensus-java with Apache License 2.0 | 5 votes |
private static void performWork(Span parent) { SpanBuilder spanBuilder = tracer .spanBuilderWithExplicitParent("internal_work", parent) .setRecordEvents(true) .setSampler(Samplers.alwaysSample()); try (Scope scope = spanBuilder.startScopedSpan()) { Span span = tracer.getCurrentSpan(); span.putAttribute("my_attribute", AttributeValue.stringAttributeValue("blue")); span.addAnnotation("Performing work."); sleepFor(20); // Working hard here. span.addAnnotation("Done work."); } }
Example #6
Source File: BeamFnMapTaskExecutor.java From beam with Apache License 2.0 | 5 votes |
@Override public void execute() throws Exception { Tracer tracer = Tracing.getTracer(); SpanBuilder builder = tracer.spanBuilder("MapTaskExecutor.Span").setRecordEvents(true); // Start the progress tracker before execution (which blocks until execution is finished). try (Scope unused = builder.startScopedSpan(); AutoCloseable unused2 = progressTrackerCloseable(progressTracker)) { tracer.getCurrentSpan().addAnnotation("About to execute"); super.execute(); tracer.getCurrentSpan().addAnnotation("Done with execute"); } }
Example #7
Source File: HttpServerHandler.java From opencensus-java with Apache License 2.0 | 5 votes |
/** * Instrument an incoming request before it is handled. * * <p>This method will create a span under the deserialized propagated parent context. If the * parent context is not present, the span will be created under the current context. * * <p>The generated span will NOT be set as current context. User can control when to enter the * scope of this span. Use {@link AbstractHttpHandler#getSpanFromContext} to retrieve the span. * * @param carrier the entity that holds the HTTP information. * @param request the request entity. * @return the {@link HttpRequestContext} that contains stats and trace data associated with the * request. * @since 0.19 */ public HttpRequestContext handleStart(C carrier, Q request) { checkNotNull(carrier, "carrier"); checkNotNull(request, "request"); SpanBuilder spanBuilder = null; String spanName = getSpanName(request, extractor); // de-serialize the context SpanContext spanContext = null; try { spanContext = textFormat.extract(carrier, getter); } catch (SpanContextParseException e) { // TODO: Currently we cannot distinguish between context parse error and missing context. // Logging would be annoying so we just ignore this error and do not even log a message. } if (spanContext == null || publicEndpoint) { spanBuilder = tracer.spanBuilder(spanName); } else { spanBuilder = tracer.spanBuilderWithRemoteParent(spanName, spanContext); } Span span = spanBuilder.setSpanKind(Kind.SERVER).startSpan(); if (publicEndpoint && spanContext != null) { span.addLink(Link.fromSpanContext(spanContext, Type.PARENT_LINKED_SPAN)); } if (span.getOptions().contains(Options.RECORD_EVENTS)) { addSpanRequestAttributes(span, request, extractor); } return getNewContext(span, tagger.getCurrentTagContext()); }
Example #8
Source File: TracerImpl.java From opencensus-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder spanBuilderWithRemoteParent( String spanName, @Nullable SpanContext remoteParentSpanContext) { return SpanBuilderImpl.createWithRemoteParent( spanName, remoteParentSpanContext, spanBuilderOptions); }
Example #9
Source File: StatsTestUtils.java From grpc-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setRecordEvents(boolean recordEvents) { return this; }
Example #10
Source File: StatsTestUtils.java From grpc-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setParentLinks(List<Span> parentLinks) { return this; }
Example #11
Source File: StatsTestUtils.java From grpc-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setSampler(Sampler sampler) { return this; }
Example #12
Source File: TracerImplTest.java From opencensus-java with Apache License 2.0 | 4 votes |
@Test public void createSpanBuilderWithRemoteParet() { SpanBuilder spanBuilder = tracer.spanBuilderWithRemoteParent(SPAN_NAME, SpanContext.INVALID); assertThat(spanBuilder).isInstanceOf(SpanBuilderImpl.class); }
Example #13
Source File: TracerImplTest.java From opencensus-java with Apache License 2.0 | 4 votes |
@Test public void createSpanBuilder() { SpanBuilder spanBuilder = tracer.spanBuilderWithExplicitParent(SPAN_NAME, BlankSpan.INSTANCE); assertThat(spanBuilder).isInstanceOf(SpanBuilderImpl.class); }
Example #14
Source File: StatsTestUtils.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setSampler(Sampler sampler) { return this; }
Example #15
Source File: TracerImpl.java From opencensus-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder spanBuilderWithExplicitParent(String spanName, @Nullable Span parent) { return SpanBuilderImpl.createWithParent(spanName, parent, spanBuilderOptions); }
Example #16
Source File: ZPagesTester.java From opencensus-java with Apache License 2.0 | 4 votes |
private static void recordExampleData() throws InterruptedException { Tracing.getExportComponent() .getSampledSpanStore() .registerSpanNamesForCollection(Collections.singletonList(SPAN_NAME)); RpcViews.registerAllViews(); // Use old RPC constants to get interval stats. SpanBuilder spanBuilder = tracer.spanBuilder(SPAN_NAME).setRecordEvents(true).setSampler(Samplers.alwaysSample()); try (Scope scope = spanBuilder.startScopedSpan()) { tracer.getCurrentSpan().addAnnotation("Starts recording."); MeasureMap measureMap = statsRecorder .newMeasureMap() // Client measurements. .put(RpcMeasureConstants.RPC_CLIENT_STARTED_COUNT, 1) .put(RpcMeasureConstants.RPC_CLIENT_FINISHED_COUNT, 1) .put(RpcMeasureConstants.RPC_CLIENT_ROUNDTRIP_LATENCY, 1.0) .put(RpcMeasureConstants.RPC_CLIENT_REQUEST_COUNT, 1) .put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_COUNT, 1) .put(RpcMeasureConstants.RPC_CLIENT_REQUEST_BYTES, 1e5) .put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_BYTES, 1e5) .put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, 1e5) .put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, 1e5) // Server measurements. .put(RpcMeasureConstants.RPC_SERVER_STARTED_COUNT, 1) .put(RpcMeasureConstants.RPC_SERVER_FINISHED_COUNT, 1) .put(RpcMeasureConstants.RPC_SERVER_SERVER_LATENCY, 1.0) .put(RpcMeasureConstants.RPC_SERVER_REQUEST_COUNT, 1) .put(RpcMeasureConstants.RPC_SERVER_RESPONSE_COUNT, 1) .put(RpcMeasureConstants.RPC_SERVER_REQUEST_BYTES, 1e5) .put(RpcMeasureConstants.RPC_SERVER_RESPONSE_BYTES, 1e5) .put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, 1e5) .put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, 1e5); measureMap.record( tagger .currentBuilder() .put(RpcMeasureConstants.RPC_STATUS, TagValue.create("OK")) .put(RpcMeasureConstants.RPC_METHOD, METHOD) .build()); MeasureMap measureMapErrors = statsRecorder .newMeasureMap() .put(RpcMeasureConstants.RPC_CLIENT_ERROR_COUNT, 1) .put(RpcMeasureConstants.RPC_SERVER_ERROR_COUNT, 1); measureMapErrors.record( tagger .currentBuilder() .put(RpcMeasureConstants.RPC_STATUS, TagValue.create("UNKNOWN")) .put(RpcMeasureConstants.RPC_METHOD, METHOD) .build()); Thread.sleep(200); // sleep for fake work. tracer.getCurrentSpan().addAnnotation("Finish recording."); } }
Example #17
Source File: StatsTestUtils.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setRecordEvents(boolean recordEvents) { return this; }
Example #18
Source File: StatsTestUtils.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Override public SpanBuilder setParentLinks(List<Span> parentLinks) { return this; }