public static void executeNoCatch(String projectId) throws Exception { try (TraceServiceClient client = TraceServiceClient.create()) { ProjectName name = ProjectName.of(projectId); List<Span> spans = new ArrayList<>(); client.batchWriteSpans(name, spans); } }
/** * Sends new spans to new or existing traces. You cannot update existing spans. * * <p>Sample code: * * <pre><code> * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) { * ProjectName name = ProjectName.of("[PROJECT]"); * List<Span> spans = new ArrayList<>(); * traceServiceClient.batchWriteSpans(name.toString(), spans); * } * </code></pre> * * @param name Required. The name of the project where the spans belong. The format is * `projects/[PROJECT_ID]`. * @param spans A list of new spans. The span names must not match existing spans, or the results * are undefined. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final void batchWriteSpans(String name, List<Span> spans) { BatchWriteSpansRequest request = BatchWriteSpansRequest.newBuilder().setName(name).addAllSpans(spans).build(); batchWriteSpans(request); }
/** * Sends new spans to new or existing traces. You cannot update existing spans. * * <p>Sample code: * * <pre><code> * try (TraceServiceClient traceServiceClient = TraceServiceClient.create()) { * ProjectName name = ProjectName.of("[PROJECT]"); * List<Span> spans = new ArrayList<>(); * traceServiceClient.batchWriteSpans(name, spans); * } * </code></pre> * * @param name Required. The name of the project where the spans belong. The format is * `projects/[PROJECT_ID]`. * @param spans A list of new spans. The span names must not match existing spans, or the results * are undefined. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final void batchWriteSpans(ProjectName name, List<Span> spans) { BatchWriteSpansRequest request = BatchWriteSpansRequest.newBuilder() .setName(name == null ? null : name.toString()) .addAllSpans(spans) .build(); batchWriteSpans(request); }
@Test @SuppressWarnings("all") public void batchWriteSpansExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockTraceService.addException(exception); try { ProjectName name = ProjectName.of("[PROJECT]"); List<Span> spans = new ArrayList<>(); client.batchWriteSpans(name, spans); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } } }
@Test @SuppressWarnings("all") public void batchWriteSpansTest() { Empty expectedResponse = Empty.newBuilder().build(); mockTraceService.addResponse(expectedResponse); ProjectName name = ProjectName.of("[PROJECT]"); List<Span> spans = new ArrayList<>(); client.batchWriteSpans(name, spans); List<GeneratedMessageV3> actualRequests = mockTraceService.getRequests(); Assert.assertEquals(1, actualRequests.size()); BatchWriteSpansRequest actualRequest = (BatchWriteSpansRequest) actualRequests.get(0); Assert.assertEquals(name, ProjectName.parse(actualRequest.getName())); Assert.assertEquals(spans, actualRequest.getSpansList()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Override public void export(Collection<SpanData> spanDataList) { // Start a new span with explicit 1/10000 sampling probability to avoid the case when user // sets the default sampler to always sample and we get the gRPC span of the stackdriver // export call always sampled and go to an infinite loop. io.opencensus.trace.Span span = tracer .spanBuilder(EXPORT_STACKDRIVER_TRACES) .setSampler(probabilitySampler) .setRecordEvents(true) .startSpan(); Scope scope = tracer.withSpan(span); try { List<Span> spans = new ArrayList<>(spanDataList.size()); for (SpanData spanData : spanDataList) { spans.add(generateSpan(spanData, RESOURCE_LABELS, fixedAttributes)); } // Sync call because it is already called for a batch of data, and on a separate thread. // TODO(bdrutu): Consider to make this async in the future. traceServiceClient.batchWriteSpans(projectName, spans); } finally { scope.close(); span.end(END_SPAN_OPTIONS); } } }
@Override public void export(Collection<SpanData> spanDataList) { // Start a new span with explicit 1/10000 sampling probability to avoid the case when user // sets the default sampler to always sample and we get the gRPC span of the stackdriver // export call always sampled and go to an infinite loop. io.opencensus.trace.Span span = tracer .spanBuilder(EXPORT_STACKDRIVER_TRACES) .setSampler(probabilitySampler) .setRecordEvents(true) .startSpan(); Scope scope = tracer.withSpan(span); try { List<Span> spans = new ArrayList<>(spanDataList.size()); for (SpanData spanData : spanDataList) { spans.add(generateSpan(spanData, RESOURCE_LABELS, fixedAttributes)); } // Sync call because it is already called for a batch of data, and on a separate thread. // TODO(bdrutu): Consider to make this async in the future. traceServiceClient.batchWriteSpans(projectName, spans); } finally { scope.close(); span.end(END_SPAN_OPTIONS); } } }