Java Code Examples for reactor.util.context.Context#put()

The following examples show how to use reactor.util.context.Context#put() . 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: Operators.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
/**
 * Return a wrapped {@link RejectedExecutionException} which can be thrown by the
 * operator. This exception denotes that an execution was rejected by a
 * {@link reactor.core.scheduler.Scheduler}, notably when it was already disposed.
 * <p>
 * Wrapping is done by calling both {@link Exceptions#failWithRejected(Throwable)} and
 * {@link #onOperatorError(Subscription, Throwable, Object, Context)} (with the passed
 * {@link Subscription}).
 *
 * @param original the original execution error
 * @param subscription the subscription to pass to onOperatorError.
 * @param suppressed a Throwable to be suppressed by the {@link RejectedExecutionException} (or null if not relevant)
 * @param dataSignal a value to be passed to {@link #onOperatorError(Subscription, Throwable, Object, Context)} (or null if not relevant)
 * @param context a context that might hold a local error consumer
 */
public static RuntimeException onRejectedExecution(Throwable original,
		@Nullable Subscription subscription,
		@Nullable Throwable suppressed,
		@Nullable Object dataSignal,
		Context context) {
	//we "cheat" to apply the special key for onRejectedExecution in onOperatorError
	if (context.hasKey(Hooks.KEY_ON_REJECTED_EXECUTION)) {
		context = context.put(Hooks.KEY_ON_OPERATOR_ERROR, context.get(Hooks.KEY_ON_REJECTED_EXECUTION));
	}

	//don't create REE if original is a reactor-produced REE (not including singletons)
	RejectedExecutionException ree = Exceptions.failWithRejected(original);
	if (suppressed != null) {
		ree.addSuppressed(suppressed);
	}
	if (dataSignal != null) {
		return Exceptions.propagate(Operators.onOperatorError(subscription, ree,
				dataSignal, context));
	}
	return Exceptions.propagate(Operators.onOperatorError(subscription, ree, context));
}
 
Example 2
Source File: TracingClientResponseSubscriber.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
TracingClientResponseSubscriber(
    final CoreSubscriber<? super ClientResponse> subscriber,
    final ClientRequest clientRequest,
    final Context context,
    final Span span,
    final List<WebClientSpanDecorator> spanDecorators
) {
  this.subscriber = subscriber;
  this.clientRequest = clientRequest;
  this.context = context.put(Span.class, span);
  this.span = span;
  this.spanDecorators = spanDecorators;
}
 
Example 3
Source File: TracingSubscriber.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
TracingSubscriber(
    final CoreSubscriber<? super Void> subscriber,
    final ServerWebExchange exchange,
    final Context context,
    final Span span,
    final List<WebFluxSpanDecorator> spanDecorators
) {
  this.subscriber = subscriber;
  this.exchange = exchange;
  this.context = context.put(Span.class, span);
  this.span = span;
  this.spanDecorators = spanDecorators;
}
 
Example 4
Source File: SpanSubscriber.java    From rsocket-rpc-java with Apache License 2.0 5 votes vote down vote up
SpanSubscriber(
    Subscriber<? super T> subscriber,
    Context ctx,
    Tracer tracer,
    Map<String, String> tracingMetadata,
    SpanContext spanContext,
    String name,
    Tag... tags) {
  this.subscriber = subscriber;
  this.tracer = tracer;
  this.rootSpan = null;

  Tracer.SpanBuilder spanBuilder = this.tracer.buildSpan(name).asChildOf(spanContext);
  if (tags != null && tags.length > 0) {
    for (Tag tag : tags) {
      spanBuilder.withTag(tag.getKey(), tag.getValue());
    }
  }

  this.span = spanBuilder.start();

  if (tracingMetadata != null) {
    TextMapInjectAdapter adapter = new TextMapInjectAdapter(tracingMetadata);
    tracer.inject(span.context(), Format.Builtin.TEXT_MAP, adapter);
  }

  if (log.isTraceEnabled()) {
    log.trace(
        "Created span [{}], with name [{}], child of [{}]",
        this.span,
        name,
        spanContext.toString());
  }

  this.context = ctx.put(Span.class, this.span);
}
 
Example 5
Source File: SpanSubscriber.java    From rsocket-rpc-java with Apache License 2.0 5 votes vote down vote up
SpanSubscriber(
    Subscriber<? super T> subscriber,
    Context ctx,
    Tracer tracer,
    Map<String, String> tracingMetadata,
    SpanContext spanContext,
    String name,
    Tag... tags) {
  this.subscriber = subscriber;
  this.tracer = tracer;
  this.rootSpan = null;

  Tracer.SpanBuilder spanBuilder = this.tracer.buildSpan(name).asChildOf(spanContext);
  if (tags != null && tags.length > 0) {
    for (Tag tag : tags) {
      spanBuilder.withTag(tag.getKey(), tag.getValue());
    }
  }

  this.span = spanBuilder.start();

  if (tracingMetadata != null) {
    TextMapInjectAdapter adapter = new TextMapInjectAdapter(tracingMetadata);
    tracer.inject(span.context(), Format.Builtin.TEXT_MAP, adapter);
  }

  if (log.isTraceEnabled()) {
    log.trace(
        "Created span [{}], with name [{}], child of [{}]",
        this.span,
        name,
        spanContext.toString());
  }

  this.context = ctx.put(Span.class, this.span);
}
 
Example 6
Source File: TracingSubscriber.java    From java-spring-web with Apache License 2.0 5 votes vote down vote up
TracingSubscriber(
        final CoreSubscriber<? super Void> subscriber,
        final ServerWebExchange exchange,
        final Context context,
        final Span span,
        final List<WebFluxSpanDecorator> spanDecorators
) {
    this.subscriber = subscriber;
    this.exchange = exchange;
    this.context = context.put(Span.class, span);
    this.span = span;
    this.spanDecorators = spanDecorators;
}
 
Example 7
Source File: TracingClientResponseSubscriber.java    From java-spring-web with Apache License 2.0 5 votes vote down vote up
TracingClientResponseSubscriber(
        final CoreSubscriber<? super ClientResponse> subscriber,
        final ClientRequest clientRequest,
        final Context context,
        final Span span,
        final List<WebClientSpanDecorator> spanDecorators
) {
    this.subscriber = subscriber;
    this.clientRequest = clientRequest;
    this.context = context.put(Span.class, span);
    this.span = span;
    this.spanDecorators = spanDecorators;
}
 
Example 8
Source File: TraceWebFilter.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
WebFilterTraceSubscriber(CoreSubscriber<? super Void> actual, Context context,
		Span span, MonoWebFilterTrace parent) {
	this.actual = actual;
	this.span = span;
	this.context = context.put(TraceContext.class, span.context());
	this.exchange = parent.exchange;
	this.handler = parent.handler;
}
 
Example 9
Source File: TraceWebClientBeanPostProcessor.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
TraceWebClientSubscriber(CoreSubscriber<? super ClientResponse> actual,
		Context ctx, Span clientSpan, MonoWebClientTrace mono) {
	this.actual = actual;
	this.parent = mono.parent;
	this.handler = mono.handler;
	this.currentTraceContext = mono.currentTraceContext;
	this.scopePassingTransformer = mono.scopePassingTransformer;
	this.context = parent != null
			&& !parent.equals(ctx.getOrDefault(TraceContext.class, null))
					? ctx.put(TraceContext.class, parent) : ctx;
	set(clientSpan);
}
 
Example 10
Source File: ScopePassingSpanSubscriber.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
ScopePassingSpanSubscriber(Subscriber<? super T> subscriber, Context ctx,
		CurrentTraceContext currentTraceContext, @Nullable TraceContext parent) {
	this.subscriber = subscriber;
	this.currentTraceContext = currentTraceContext;
	this.parent = parent;
	this.context = parent != null
			&& !parent.equals(ctx.getOrDefault(TraceContext.class, null))
					? ctx.put(TraceContext.class, parent) : ctx;
	if (log.isTraceEnabled()) {
		log.trace("Parent span [" + parent + "], context [" + this.context + "]");
	}
}
 
Example 11
Source File: SpanSubscriber.java    From rsocket-rpc-java with Apache License 2.0 4 votes vote down vote up
SpanSubscriber(
    Subscriber<? super T> subscriber,
    Context ctx,
    Tracer tracer,
    Map<String, String> tracingMetadata,
    String name,
    Tag... tags) {
  this.subscriber = subscriber;
  this.tracer = tracer;
  Span root = ctx.getOrDefault(Span.class, this.tracer.activeSpan());
  if (log.isTraceEnabled()) {
    log.trace("Span from context [{}]", root);
  }
  this.rootSpan = root;
  if (log.isTraceEnabled()) {
    log.trace("Stored context root span [{}]", this.rootSpan);
  }

  Tracer.SpanBuilder spanBuilder = this.tracer.buildSpan(name);
  if (tags != null && tags.length > 0) {
    for (Tag tag : tags) {
      spanBuilder.withTag(tag.getKey(), tag.getValue());
    }
  }

  if (root != null) {
    spanBuilder.asChildOf(root);
  }

  this.span = spanBuilder.start();

  if (tracingMetadata != null) {
    TextMapInjectAdapter adapter = new TextMapInjectAdapter(tracingMetadata);
    tracer.inject(span.context(), Format.Builtin.TEXT_MAP, adapter);
  }

  if (log.isTraceEnabled()) {
    log.trace("Created span [{}], with name [{}]", this.span, name);
  }
  this.context = ctx.put(Span.class, this.span);
}
 
Example 12
Source File: SpanSubscriber.java    From rsocket-rpc-java with Apache License 2.0 4 votes vote down vote up
SpanSubscriber(
    Subscriber<? super T> subscriber,
    Context ctx,
    Tracer tracer,
    Map<String, String> tracingMetadata,
    String name,
    Tag... tags) {
  this.subscriber = subscriber;
  this.tracer = tracer;
  Span root = ctx.getOrDefault(Span.class, this.tracer.activeSpan());
  if (log.isTraceEnabled()) {
    log.trace("Span from context [{}]", root);
  }
  this.rootSpan = root;
  if (log.isTraceEnabled()) {
    log.trace("Stored context root span [{}]", this.rootSpan);
  }

  Tracer.SpanBuilder spanBuilder = this.tracer.buildSpan(name);
  if (tags != null && tags.length > 0) {
    for (Tag tag : tags) {
      spanBuilder.withTag(tag.getKey(), tag.getValue());
    }
  }

  if (root != null) {
    spanBuilder.asChildOf(root);
  }

  this.span = spanBuilder.start();

  if (tracingMetadata != null) {
    TextMapInjectAdapter adapter = new TextMapInjectAdapter(tracingMetadata);
    tracer.inject(span.context(), Format.Builtin.TEXT_MAP, adapter);
  }

  if (log.isTraceEnabled()) {
    log.trace("Created span [{}], with name [{}]", this.span, name);
  }
  this.context = ctx.put(Span.class, this.span);
}
 
Example 13
Source File: WingtipsSpringWebfluxUtils.java    From wingtips with Apache License 2.0 3 votes vote down vote up
/**
 * Uses {@link Context#put(Object, Object)} to return a new {@link Context} that contains
 * {@code TracingState.class} as a key, and the given {@link TracingState} as the value for that key.
 *
 * <p>You can use {@link #tracingStateFromContext(Context)} as a convenience helper method for pulling
 * the {@link TracingState} out of a {@link Context}.
 *
 * @param origContext The original {@link Context} that you want tracing state added to.
 * @param tracingState The {@link TracingState} to add to the given {@link Context}.
 * @return A new {@link Context} that matches the original, but also contains a new key/value pair of
 * {@code TracingState.class} -> {@link TracingState}.
 */
public static @NotNull Context subscriberContextWithTracingInfo(
    @NotNull Context origContext,
    @NotNull TracingState tracingState
) {
    return origContext.put(TracingState.class, tracingState);
}
 
Example 14
Source File: Operators.java    From reactor-core with Apache License 2.0 3 votes vote down vote up
/**
 * Utility method to activate the onDiscard feature (see {@link Flux#doOnDiscard(Class, Consumer)})
 * in a target {@link Context}. Prefer using the {@link Flux} API, and reserve this for
 * testing purposes.
 *
 * @param target the original {@link Context}
 * @param discardConsumer the consumer that will be used to cleanup discarded elements
 * @return a new {@link Context} that holds (potentially combined) cleanup {@link Consumer}
 */
public static final Context enableOnDiscard(@Nullable Context target, Consumer<?> discardConsumer) {
	Objects.requireNonNull(discardConsumer, "discardConsumer must be provided");
	if (target == null) {
		return Context.of(Hooks.KEY_ON_DISCARD, discardConsumer);
	}
	return target.put(Hooks.KEY_ON_DISCARD, discardConsumer);
}