Search in sources :

Example 1 with BytesParam

use of core.framework.impl.log.filter.BytesParam in project core-ng-project by neowu.

the class KafkaMessageListenerThread method handle.

private <T> void handle(String topic, KafkaMessageListener.MessageHandlerHolder<T> holder, List<ConsumerRecord<String, byte[]>> records, double longProcessThresholdInNano) {
    for (ConsumerRecord<String, byte[]> record : records) {
        ActionLog actionLog = logManager.begin("=== message handling begin ===");
        try {
            actionLog.action("topic:" + topic);
            actionLog.context("topic", topic);
            actionLog.context("handler", holder.handler.getClass().getCanonicalName());
            actionLog.context("key", record.key());
            logger.debug("message={}", new BytesParam(record.value()));
            T message = holder.reader.fromJSON(record.value());
            Headers headers = record.headers();
            actionLog.refId(header(headers, KafkaHeaders.HEADER_REF_ID));
            String client = header(headers, KafkaHeaders.HEADER_CLIENT);
            if (client != null)
                actionLog.context("client", client);
            String clientIP = header(headers, KafkaHeaders.HEADER_CLIENT_IP);
            if (clientIP != null)
                actionLog.context("clientIP", clientIP);
            if ("true".equals(header(headers, KafkaHeaders.HEADER_TRACE))) {
                actionLog.trace = true;
            }
            holder.validator.validate(message);
            holder.handler.handle(record.key(), message);
        } catch (Throwable e) {
            logManager.logError(e);
        } finally {
            long elapsedTime = actionLog.elapsedTime();
            if (elapsedTime > longProcessThresholdInNano) {
                logger.warn(Markers.errorCode("LONG_PROCESS"), "took too long to process message, elapsedTime={}", elapsedTime);
            }
            logManager.end("=== message handling end ===");
        }
    }
}
Also used : BytesParam(core.framework.impl.log.filter.BytesParam) Headers(org.apache.kafka.common.header.Headers) ActionLog(core.framework.impl.log.ActionLog)

Example 2 with BytesParam

use of core.framework.impl.log.filter.BytesParam in project core-ng-project by neowu.

the class TextBody method send.

@Override
public void send(Sender sender, ResponseHandlerContext context) {
    byte[] bytes = Strings.bytes(text);
    logger.debug("[response] body={}", new BytesParam(bytes));
    sender.send(ByteBuffer.wrap(bytes));
}
Also used : BytesParam(core.framework.impl.log.filter.BytesParam)

Example 3 with BytesParam

use of core.framework.impl.log.filter.BytesParam in project core-ng-project by neowu.

the class RequestParser method logRequestBody.

private void logRequestBody(RequestImpl request) {
    ContentType contentType = request.contentType;
    if (contentType == null)
        return;
    Object bodyParam = null;
    if (ContentType.APPLICATION_JSON.mediaType().equals(contentType.mediaType())) {
        bodyParam = new JSONParam(request.body, contentType.charset().orElse(Charsets.UTF_8));
    } else if (ContentType.TEXT_XML.mediaType().equals(contentType.mediaType())) {
        bodyParam = new BytesParam(request.body, contentType.charset().orElse(Charsets.UTF_8));
    }
    if (bodyParam != null)
        logger.debug("[request] body={}", bodyParam);
}
Also used : JSONParam(core.framework.impl.log.filter.JSONParam) ContentType(core.framework.http.ContentType) BytesParam(core.framework.impl.log.filter.BytesParam)

Example 4 with BytesParam

use of core.framework.impl.log.filter.BytesParam in project core-ng-project by neowu.

the class HTTPClientImpl method logRequestBody.

private void logRequestBody(HTTPRequest request, ContentType contentType) {
    Object bodyParam;
    if (ContentType.APPLICATION_JSON.mediaType().equals(contentType.mediaType())) {
        bodyParam = new JSONParam(request.body(), contentType.charset().orElse(Charsets.UTF_8));
    } else {
        bodyParam = new BytesParam(request.body());
    }
    logger.debug("[request] contentType={}, body={}", contentType, bodyParam);
}
Also used : JSONParam(core.framework.impl.log.filter.JSONParam) BytesParam(core.framework.impl.log.filter.BytesParam)

Example 5 with BytesParam

use of core.framework.impl.log.filter.BytesParam in project core-ng-project by neowu.

the class RedisImpl method set.

public void set(String key, byte[] value, Duration expiration) {
    StopWatch watch = new StopWatch();
    PoolItem<RedisConnection> item = pool.borrowItem();
    try {
        RedisConnection connection = item.resource;
        connection.write(Protocol.Command.SETEX, encode(key), encode(expiration.getSeconds()), value);
        connection.readSimpleString();
    } catch (IOException e) {
        item.broken = true;
        throw new UncheckedIOException(e);
    } finally {
        pool.returnItem(item);
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("redis", elapsedTime, 0, 1);
        logger.debug("set, key={}, value={}, expiration={}, elapsedTime={}", key, new BytesParam(value), expiration, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : BytesParam(core.framework.impl.log.filter.BytesParam) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Aggregations

BytesParam (core.framework.impl.log.filter.BytesParam)7 JSONParam (core.framework.impl.log.filter.JSONParam)2 StopWatch (core.framework.util.StopWatch)2 Headers (org.apache.kafka.common.header.Headers)2 ContentType (core.framework.http.ContentType)1 ActionLog (core.framework.impl.log.ActionLog)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1