Java Code Examples for io.opentracing.Tracer.SpanBuilder#startActive()

The following examples show how to use io.opentracing.Tracer.SpanBuilder#startActive() . 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: OrderManager.java    From problematic-microservices with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Customer validateUser(Long customerId, SpanContext parent) throws ValidationException {
	SpanBuilder spanBuilder = GlobalTracer.get().buildSpan("validateUser")
			.withTag(Customer.KEY_CUSTOMER_ID, String.valueOf(customerId)).asChildOf(parent);
	try (Scope scope = spanBuilder.startActive(true)) {
		Request req = new Request.Builder().url(CUSTOMER_SERVICE_LOCATION + "/customers/" + customerId).get()
				//.tag(new TagWrapper(parentSpan.context()))
				.build();

		Response res = null;
		try {
			res = httpClient.newCall(req).execute();
			if (res.code() == Status.NOT_FOUND.getStatusCode()) {
				throw new ValidationException("Could not find customer " + customerId);
			}
			return Customer.fromJSon(res.body().string());
		} catch (IOException exc) {
			throw new ValidationException("Failed to validate customer");
		}
	}
}
 
Example 2
Source File: TracingHttpRequesterFilter.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private ScopeTracker newTracker(final HttpRequestMetaData request) {
    SpanBuilder spanBuilder = tracer.buildSpan(componentName)
            .withTag(SPAN_KIND.getKey(), SPAN_KIND_CLIENT)
            .withTag(HTTP_METHOD.getKey(), request.method().name())
            .withTag(HTTP_URL.getKey(), request.path());
    final Span activeSpan = tracer.activeSpan();
    if (activeSpan != null) {
        spanBuilder = spanBuilder.asChildOf(activeSpan);
    }
    Scope scope = spanBuilder.startActive(true);
    tracer.inject(scope.span().context(), formatter, request.headers());
    return new ScopeTracker(scope);
}
 
Example 3
Source File: OrderCompletionMonitor.java    From problematic-microservices with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void run() {
	if (isDone) {
		scheduledFuture.cancel(false);
		return;
	}

	SpanBuilder buildSpan = GlobalTracer.get().buildSpan("pickupOrderAttempt");
	buildSpan.withTag(RobotOrder.KEY_ORDER_ID, String.valueOf(order.getOrderId()));
	buildSpan.addReference(References.FOLLOWS_FROM, parent);

	try (Scope scope = buildSpan.startActive(true)) {
		okhttp3.HttpUrl.Builder httpBuilder = HttpUrl.parse(pickupLocation).newBuilder();
		httpBuilder.addQueryParameter(RobotOrder.KEY_ORDER_ID, String.valueOf(order.getOrderId()));
		Request request = new Request.Builder().url(httpBuilder.build()).build();
		try {
			Response response = httpClient.newCall(request).execute();
			String body = response.body().string();
			if (response.isSuccessful() && !body.isEmpty()) {
				isDone = true;
				RealizedOrder realizedOrder = RealizedOrder.fromJSon(body);
				future.complete(realizedOrder);
				scheduledFuture.cancel(false);
			}
		} catch (IOException e) {
			e.printStackTrace();
			scheduledFuture.cancel(false);
		}
	}
}
 
Example 4
Source File: Factory.java    From problematic-microservices with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Robot paintRobot(Robot robotToPaint, Color paint, SpanContext spanContext) {
	SpanBuilder spanBuilder = GlobalTracer.get().buildSpan("paintingRobot");
	spanBuilder.asChildOf(spanContext);
	spanBuilder.withTag(Robot.KEY_COLOR, paint.toString());
	try (Scope scope = spanBuilder.startActive(true)) {
		Logger.log("Painting robot!");
		// Takes 50 ms to paint a robot. Yep, it's a kick ass robot factory.
		Utils.sleep(50);
		return new Robot(robotToPaint.getSerialNumber(), robotToPaint.getRobotType(), paint);
	}
}
 
Example 5
Source File: Factory.java    From problematic-microservices with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Robot createChassis(long serialNumber, String robotTypeId, SpanContext spanContext) {
	SpanBuilder spanBuilder = GlobalTracer.get().buildSpan("creatingChassis");
	spanBuilder.asChildOf(spanContext);
	spanBuilder.withTag(RobotType.KEY_ROBOT_TYPE, robotTypeId);
	try (Scope scope = spanBuilder.startActive(true)) {
		Logger.log("Creating robot chassis!");
		// Takes 70 ms to create a robot chassis. Yep, it's a kick ass robot factory.
		Utils.sleep(70);
		return new Robot(serialNumber, robotTypeId, null);
	}
}