Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store http headers through an interceptors invocation chain #550

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -217,4 +218,15 @@ public ProxyContext exposeHeaders() {

return this;
}
}

public String getRequestHeader(String name) {
String value = request.getHeader(name);
if (value != null) {
return value;
}
return Optional.ofNullable(apiKeyData)
.map(ApiKeyData::getHttpHeaders)
.map(h -> h.get(name))
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.epam.aidial.core.server.data;

import com.epam.aidial.core.config.Key;
import com.epam.aidial.core.server.Proxy;
import com.epam.aidial.core.server.ProxyContext;
import com.epam.aidial.core.server.security.ExtractedClaims;
import com.fasterxml.jackson.annotation.JsonIgnore;
Expand All @@ -10,6 +11,9 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The container keeps data associated with API key.
Expand All @@ -23,6 +27,10 @@
*/
@Data
public class ApiKeyData {
private static final List<String> HTTP_HEADERS_TO_STORE = List.of(
Proxy.HEADER_JOB_TITLE,
Proxy.HEADER_CONVERSATION_ID
);
// per request key is available with during the request lifetime. It's generated in runtime
private String perRequestKey;
// the key of root request initiator
Expand All @@ -47,6 +55,8 @@ public class ApiKeyData {
// deployment triggers interceptors
private String initialDeployment;
private String initialDeploymentApi;
// Original HTTP headers to be stored during an interceptor invocation chain
private Map<String, String> httpHeaders = new HashMap<>();

public ApiKeyData() {
}
Expand All @@ -57,6 +67,7 @@ public static void initFromContext(ApiKeyData proxyApiKeyData, ProxyContext cont
proxyApiKeyData.setInterceptors(context.getInterceptors());
proxyApiKeyData.setInitialDeployment(context.getInitialDeployment());
proxyApiKeyData.setInitialDeploymentApi(context.getInitialDeploymentApi());
proxyApiKeyData.setHttpHeaders(collectHttpHeaders(context));

if (apiKeyData.getPerRequestKey() == null) {
proxyApiKeyData.setOriginalKey(context.getKey());
Expand All @@ -80,4 +91,13 @@ public static void initFromContext(ApiKeyData proxyApiKeyData, ProxyContext cont
public boolean isInterceptor() {
return perRequestKey != null && interceptors != null && interceptorIndex >= 0 && interceptorIndex < interceptors.size();
}

private static Map<String, String> collectHttpHeaders(ProxyContext context) {
if (!context.getApiKeyData().getHttpHeaders().isEmpty()) {
return context.getApiKeyData().getHttpHeaders();
}
return context.getRequest().headers().entries().stream()
.filter(h -> HTTP_HEADERS_TO_STORE.contains(h.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private void append(ProxyContext context, LogEntry entry) throws JsonProcessingE
HttpServerResponse response = context.getResponse();

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

append(entry, "\"},\"project\":{\"id\":\"", false);
append(entry, context.getProject(), true);
Expand All @@ -78,7 +78,7 @@ private void append(ProxyContext context, LogEntry entry) throws JsonProcessingE
append(entry, context.getUserHash(), true);

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

TokenUsage tokenUsage = context.getTokenUsage();
Expand Down
Loading