Skip to content

Commit

Permalink
Tracer SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Mar 2, 2019
1 parent f679110 commit 82b94ad
Show file tree
Hide file tree
Showing 25 changed files with 1,271 additions and 166 deletions.
19 changes: 19 additions & 0 deletions src/main/asciidoc/dataobjects.adoc
Expand Up @@ -1931,6 +1931,24 @@ Set the ALPN usage.
+++
|===

[[TracingOptions]]
== TracingOptions

++++
Vert.x tracing base configuration, this class can be extended by provider implementations to configure
those specific implementations.
++++
'''

[cols=">25%,25%,50%"]
[frame="topbot"]
|===
^|Name | Type ^| Description
|[[enabled]]`@enabled`|`Boolean`|+++
Set whether tracing will be enabled on the Vert.x instance.
+++
|===

[[VertxOptions]]
== VertxOptions

Expand Down Expand Up @@ -2022,6 +2040,7 @@ Set wether to prefer the native transport to the JDK transport.
|[[quorumSize]]`@quorumSize`|`Number (int)`|+++
Set the quorum size to be used when HA is enabled.
+++
|[[tracingOptions]]`@tracingOptions`|`link:dataobjects.html#TracingOptions[TracingOptions]`|-
|[[warningExceptionTime]]`@warningExceptionTime`|`Number (long)`|+++
Set the threshold value above this, the blocked warning contains a stack trace. in link.
The default value of link is
Expand Down
8 changes: 8 additions & 0 deletions src/main/generated/io/vertx/core/VertxOptionsConverter.java
Expand Up @@ -129,6 +129,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, VertxOp
obj.setQuorumSize(((Number)member.getValue()).intValue());
}
break;
case "tracingOptions":
if (member.getValue() instanceof JsonObject) {
obj.setTracingOptions(new io.vertx.core.tracing.TracingOptions((JsonObject)member.getValue()));
}
break;
case "warningExceptionTime":
if (member.getValue() instanceof Number) {
obj.setWarningExceptionTime(((Number)member.getValue()).longValue());
Expand Down Expand Up @@ -196,6 +201,9 @@ static void toJson(VertxOptions obj, java.util.Map<String, Object> json) {
}
json.put("preferNativeTransport", obj.getPreferNativeTransport());
json.put("quorumSize", obj.getQuorumSize());
if (obj.getTracingOptions() != null) {
json.put("tracingOptions", obj.getTracingOptions().toJson());
}
json.put("warningExceptionTime", obj.getWarningExceptionTime());
if (obj.getWarningExceptionTimeUnit() != null) {
json.put("warningExceptionTimeUnit", obj.getWarningExceptionTimeUnit().name());
Expand Down
@@ -0,0 +1,33 @@
package io.vertx.core.tracing;

import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import java.time.Instant;
import java.time.format.DateTimeFormatter;

/**
* Converter for {@link io.vertx.core.tracing.TracingOptions}.
* NOTE: This class has been automatically generated from the {@link io.vertx.core.tracing.TracingOptions} original class using Vert.x codegen.
*/
class TracingOptionsConverter {

static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, TracingOptions obj) {
for (java.util.Map.Entry<String, Object> member : json) {
switch (member.getKey()) {
case "enabled":
if (member.getValue() instanceof Boolean) {
obj.setEnabled((Boolean)member.getValue());
}
break;
}
}
}

static void toJson(TracingOptions obj, JsonObject json) {
toJson(obj, json.getMap());
}

static void toJson(TracingOptions obj, java.util.Map<String, Object> json) {
json.put("enabled", obj.isEnabled());
}
}
27 changes: 27 additions & 0 deletions src/main/java/io/vertx/core/Context.java
Expand Up @@ -204,6 +204,33 @@ static boolean isOnVertxThread() {
*/
boolean remove(String key);

/**
* Get some local data from the context.
*
* @param key the key of the data
* @param <T> the type of the data
* @return the data
*/
<T> T getLocal(String key);

/**
* Put some local data in the context.
* <p>
* This can be used to share data between different handlers that share a context
*
* @param key the key of the data
* @param value the data
*/
void putLocal(String key, Object value);

/**
* Remove some local data from the context.
*
* @param key the key to remove
* @return true if removed successfully, false otherwise
*/
boolean removeLocal(String key);

/**
* @return The Vertx instance that created the context
*/
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/io/vertx/core/VertxOptions.java
Expand Up @@ -19,6 +19,7 @@
import io.vertx.core.json.JsonObject;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.core.tracing.TracingOptions;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -156,6 +157,7 @@ public class VertxOptions {
private int quorumSize = DEFAULT_QUORUM_SIZE;
private String haGroup = DEFAULT_HA_GROUP;
private MetricsOptions metricsOptions = new MetricsOptions();
private TracingOptions tracingOptions = new TracingOptions();
private FileSystemOptions fileSystemOptions = new FileSystemOptions();
private long warningExceptionTime = DEFAULT_WARNING_EXCEPTION_TIME;
private EventBusOptions eventBusOptions = new EventBusOptions();
Expand Down Expand Up @@ -197,6 +199,7 @@ public VertxOptions(VertxOptions other) {
this.maxWorkerExecuteTimeUnit = other.maxWorkerExecuteTimeUnit;
this.warningExceptionTimeUnit = other.warningExceptionTimeUnit;
this.blockedThreadCheckIntervalUnit = other.blockedThreadCheckIntervalUnit;
this.tracingOptions = other.tracingOptions != null ? other.tracingOptions.copy() : null;
}

/**
Expand Down Expand Up @@ -805,6 +808,15 @@ public VertxOptions setBlockedThreadCheckIntervalUnit(TimeUnit blockedThreadChec
return this;
}

public TracingOptions getTracingOptions() {
return tracingOptions;
}

public VertxOptions setTracingOptions(TracingOptions tracingOptions) {
this.tracingOptions = tracingOptions;
return this;
}

public JsonObject toJson() {
JsonObject json = new JsonObject();
VertxOptionsConverter.toJson(this, json);
Expand Down
160 changes: 160 additions & 0 deletions src/main/java/io/vertx/core/impl/AbstractContext.java
@@ -0,0 +1,160 @@
/*
* Copyright (c) 2011-2017 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Starter;
import io.vertx.core.impl.launcher.VertxCommandLauncher;
import io.vertx.core.spi.tracing.VertxTracer;

import java.util.List;
import java.util.concurrent.RejectedExecutionException;

/**
* A context implementation that does not hold any specific state.
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
* @author <a href="http://tfox.org">Tim Fox</a>
*/
abstract class AbstractContext implements ContextInternal {

private static final String THREAD_CHECKS_PROP_NAME = "vertx.threadChecks";
private static final boolean THREAD_CHECKS = Boolean.getBoolean(THREAD_CHECKS_PROP_NAME);

abstract void executeAsync(Handler<Void> task);

abstract <T> void execute(T value, Handler<T> task);

@Override
public abstract boolean isEventLoopContext();

@Override
public boolean isWorkerContext() {
return !isEventLoopContext();
}

static boolean isOnVertxThread(boolean worker) {
Thread t = Thread.currentThread();
if (t instanceof VertxThread) {
VertxThread vt = (VertxThread) t;
return vt.isWorker() == worker;
}
return false;
}

// This is called to execute code where the origin is IO (from Netty probably).
// In such a case we should already be on an event loop thread (as Netty manages the event loops)
// but check this anyway, then execute directly
@Override
public final void executeFromIO(Handler<Void> task) {
executeFromIO(null, task);
}

@Override
public final void schedule(Handler<Void> task) {
schedule(null, task);
}

@Override
public final void dispatch(Handler<Void> task) {
dispatch(null, task);
}

@Override
public final <T> void dispatch(T arg, Handler<T> task) {
VertxThread currentThread = ContextInternal.beginDispatch(this);
try {
task.handle(arg);
} catch (Throwable t) {
reportException(t);
} finally {
ContextInternal.endDispatch(currentThread);
}
}

@Override
public final <T> void executeFromIO(T value, Handler<T> task) {
if (THREAD_CHECKS) {
checkEventLoopThread();
}
execute(value, task);
}

private void checkEventLoopThread() {
Thread current = Thread.currentThread();
if (!(current instanceof VertxThread)) {
throw new IllegalStateException("Expected to be on Vert.x thread, but actually on: " + current);
} else if (((VertxThread) current).isWorker()) {
throw new IllegalStateException("Event delivered on unexpected worker thread " + current);
}
}

// Run the task asynchronously on this same context
@Override
public final void runOnContext(Handler<Void> task) {
try {
executeAsync(task);
} catch (RejectedExecutionException ignore) {
// Pool is already shut down
}
}

@Override
public final List<String> processArgs() {
// As we are maintaining the launcher and starter class, choose the right one.
List<String> processArgument = VertxCommandLauncher.getProcessArguments();
return processArgument != null ? processArgument : Starter.PROCESS_ARGS;
}

@Override
public final <T> void executeBlocking(Handler<Future<T>> blockingCodeHandler, Handler<AsyncResult<T>> resultHandler) {
executeBlocking(blockingCodeHandler, true, resultHandler);
}

@Override
public final ContextInternal duplicate() {
return duplicate(null);
}

@SuppressWarnings("unchecked")
@Override
public final <T> T get(String key) {
return (T) contextData().get(key);
}

@Override
public final void put(String key, Object value) {
contextData().put(key, value);
}

@Override
public final boolean remove(String key) {
return contextData().remove(key) != null;
}

@SuppressWarnings("unchecked")
@Override
public final <T> T getLocal(String key) {
return (T) localContextData().get(key);
}

@Override
public final void putLocal(String key, Object value) {
localContextData().put(key, value);
}

@Override
public final boolean removeLocal(String key) {
return localContextData().remove(key) != null;
}
}

0 comments on commit 82b94ad

Please sign in to comment.