private void append()

in server/src/main/java/com/epam/aidial/core/server/log/GfLogStore.java [84:196]


    private void append(ProxyContext context, LogEntry entry, String assembledStreamingResponse) throws JsonProcessingException {
        HttpServerRequest request = context.getRequest();
        HttpServerResponse response = context.getResponse();

        append(entry, "{\"apiType\":\"DialOpenAI\",\"chat\":{\"id\":\"", false);
        append(entry, context.getRequestHeader(Proxy.HEADER_CONVERSATION_ID), true);

        append(entry, "\"},\"project\":{\"id\":\"", false);
        append(entry, context.getProject(), true);

        append(entry, "\"},\"user\":{\"id\":\"", false);
        append(entry, context.getUserHash(), true);

        append(entry, "\",\"title\":\"", false);
        append(entry, context.getRequestHeader(Proxy.HEADER_JOB_TITLE), true);
        append(entry, "\"}", false);

        TokenUsage tokenUsage = context.getTokenUsage();
        if (tokenUsage != null) {
            append(entry, ",\"token_usage\":{", false);
            append(entry, "\"completion_tokens\":", false);
            append(entry, Long.toString(tokenUsage.getCompletionTokens()), true);
            append(entry, ",\"prompt_tokens\":", false);
            append(entry, Long.toString(tokenUsage.getPromptTokens()), true);
            append(entry, ",\"total_tokens\":", false);
            append(entry, Long.toString(tokenUsage.getTotalTokens()), true);
            if (tokenUsage.getCost() != null) {
                append(entry, ",\"deployment_price\":", false);
                append(entry, tokenUsage.getCost().toString(), true);
            }
            if (tokenUsage.getAggCost() != null) {
                append(entry, ",\"price\":", false);
                append(entry, tokenUsage.getAggCost().toString(), true);
            }
            append(entry, "}", false);
        }

        Deployment deployment = context.getDeployment();
        if (deployment != null) {
            append(entry, ",\"deployment\":\"", false);
            append(entry, deployment.getName(), true);
            append(entry, "\"", false);
        }

        String parentDeployment = getParentDeployment(context);
        if (parentDeployment != null) {
            append(entry, ",\"parent_deployment\":\"", false);
            append(entry, parentDeployment, true);
            append(entry, "\"", false);
        }

        List<String> executionPath = context.getExecutionPath();
        if (executionPath != null) {
            append(entry, ",\"execution_path\":", false);
            append(entry, ProxyUtil.MAPPER.writeValueAsString(executionPath), false);
        }

        if (!context.isSecuredApiKey()) {
            append(entry, ",\"assembled_response\":\"", false);
            if (assembledStreamingResponse != null) {
                append(entry, assembledStreamingResponse, true);
            } else {
                append(entry, context.getResponseBody());
            }
            append(entry, "\"", false);
        }

        append(entry, ",\"trace\":{\"trace_id\":\"", false);
        append(entry, context.getTraceId(), true);

        append(entry, "\",\"core_span_id\":\"", false);
        append(entry, context.getSpanId(), true);

        String parentSpanId = context.getParentSpanId();
        if (parentSpanId != null) {
            append(entry, "\",\"core_parent_span_id\":\"", false);
            append(entry, context.getParentSpanId(), true);
        }

        append(entry, "\"},\"request\":{\"protocol\":\"", false);
        append(entry, request.version().alpnName().toUpperCase(), true);

        append(entry, "\",\"method\":\"", false);
        append(entry, request.method().name(), true);

        append(entry, "\",\"uri\":\"", false);
        append(entry, request.uri(), true);

        append(entry, "\",\"time\":\"", false);
        append(entry, formatTimestamp(context.getRequestTimestamp()), true);

        if (!context.isSecuredApiKey()) {
            append(entry, "\",\"body\":\"", false);
            append(entry, context.getRequestBody());
        }

        append(entry, "\"},\"response\":{\"status\":\"", false);
        append(entry, Integer.toString(response.getStatusCode()), true);

        Optional<String> upstreamEndpoint = Optional.ofNullable(context.getUpstreamRoute())
                .map(UpstreamRoute::get).map(Upstream::getEndpoint);
        if (upstreamEndpoint.isPresent()) {
            append(entry, "\",\"upstream_uri\":\"", false);
            append(entry, upstreamEndpoint.get(), true);
        }

        if (!context.isSecuredApiKey()) {
            append(entry, "\",\"body\":\"", false);
            append(entry, context.getResponseBody());
        }

        append(entry, "\"}}", false);
    }