use of com.codahale.metrics.Timer.Context in project scalecube by scalecube.
the class ServiceCall method invoke.
/**
* Invoke a request message and invoke a service by a given service name and method name. expected headers in request:
* ServiceHeaders.SERVICE_REQUEST the logical name of the service. ServiceHeaders.METHOD the method name to invoke.
* Throws Exception in case of an error or TimeoutException if no response if a given duration.
*
* @param request request with given headers.
* @param serviceInstance target instance to invoke.
* @param duration of the response before TimeException is returned.
* @return CompletableFuture with service call dispatching result.
*/
public CompletableFuture<Message> invoke(final Message request, final ServiceInstance serviceInstance, final Duration duration) {
Objects.requireNonNull(serviceInstance);
Messages.validate().serviceRequest(request);
serviceInstance.checkMethodExists(request.header(ServiceHeaders.METHOD));
if (!serviceInstance.isLocal()) {
String cid = IdGenerator.generateId();
final ServiceResponse responseFuture = ServiceResponse.correlationId(cid);
final Context ctx = Metrics.time(latency);
Metrics.mark(this.metrics, ServiceCall.class.getName(), "invoke", "request");
Counter counter = Metrics.counter(metrics, ServiceCall.class.getName(), "invoke-pending");
Metrics.inc(counter);
serviceInstance.invoke(Messages.asRequest(request, cid)).whenComplete((success, error) -> {
Metrics.dec(counter);
Metrics.stop(ctx);
if (error == null) {
Metrics.mark(metrics, ServiceCall.class, "invoke", "response");
responseFuture.withTimeout(duration);
} else {
Metrics.mark(metrics, ServiceCall.class.getName(), "invoke", "error");
responseFuture.completeExceptionally(error);
}
});
return responseFuture.future();
} else {
return serviceInstance.invoke(request);
}
}
use of com.codahale.metrics.Timer.Context in project LogHub by fbacchella.
the class EventsProcessor method run.
@Override
public void run() {
final AtomicReference<Counter> gaugecounter = new AtomicReference<>();
while (true) {
Event event = null;
try {
event = inQueue.take();
} catch (InterruptedException e) {
break;
}
{
// Needed because eventtemp must be final
final Event eventtemp = event;
event.doMetric(() -> {
gaugecounter.set(Properties.metrics.counter("Pipeline." + eventtemp.getCurrentPipeline() + ".inflight"));
gaugecounter.get().inc();
});
}
try (Context cw = event.isTest() ? null : Properties.metrics.timer("Pipeline." + event.getCurrentPipeline() + ".timer").time()) {
{
// Needed because eventtemp must be final
final Event eventtemp = event;
logger.trace("received {} in {}", () -> eventtemp, () -> eventtemp.getCurrentPipeline());
}
Processor processor = event.next();
while (processor != null) {
logger.trace("processing with {}", processor);
if (processor instanceof WrapEvent) {
event = new EventWrapper(event, processor.getPathArray());
} else if (processor instanceof UnwrapEvent) {
event = event.unwrap();
} else {
ProcessingStatus processingstatus = process(event, processor);
if (processingstatus != ProcessingStatus.SUCCESS) {
event.doMetric(() -> {
gaugecounter.get().dec();
gaugecounter.set(null);
});
// Processing status was non null, so the event will not be processed any more
// But it's needed to check why.
String currentPipeline = event.getCurrentPipeline();
switch(processingstatus) {
case DROPED:
{
// It was a drop action
logger.debug("dropped event {}", event);
event.doMetric(() -> {
Properties.metrics.meter("Allevents.dropped");
Properties.metrics.meter("Pipeline." + currentPipeline + ".dropped").mark();
});
event.drop();
break;
}
case FAILED:
{
// Processing failed critically (with an exception) and no recovery was attempted
logger.debug("Failed event {}", event);
event.doMetric(() -> {
Properties.metrics.meter("Allevents.failed");
Properties.metrics.meter("Pipeline." + currentPipeline + ".failed").mark();
});
event.end();
break;
}
case PAUSED:
// It's simply a paused event, nothing to worry
break;
case SUCCESS:
// Unreachable code
break;
}
// It was not a success, end the processing.
event = null;
break;
}
}
processor = event.next();
// If next processor is null, refill the event
while (processor == null && event.getNextPipeline() != null) {
// Send to another pipeline, loop in the main processing queue
Pipeline next = namedPipelines.get(event.getNextPipeline());
event.refill(next);
processor = event.next();
}
}
// Detect if will send to another pipeline, or just wait for a sender to take it
if (event != null) {
event.doMetric(() -> {
gaugecounter.get().dec();
gaugecounter.set(null);
});
if (event.isTest()) {
// A test event, it will not be send an output queue
// Checked after pipeline forwarding, but before output sending
TestEventProcessing.log(event);
event.end();
} else if (event.getCurrentPipeline() != null && outQueues.containsKey(event.getCurrentPipeline())) {
// Put in the output queue, where the wanting output will come to take it
try {
outQueues.get(event.getCurrentPipeline()).put(event);
} catch (InterruptedException e) {
Stats.dropped.incrementAndGet();
event.end();
Thread.currentThread().interrupt();
}
} else {
logger.error("Miss-configured event droped: {}", event);
Properties.metrics.meter("Allevents.failed");
event.end();
}
event = null;
}
}
}
}
Aggregations