Skip to content

Commit cc6e5ea

Browse files
fix: identify Ark runtime Java SDK in User-Agent
Sync-Source-Commit: 76833ebf1d7bc380e0bf48397dd28113e14eb29f Hand-Written-Reason: No Ark-APIs provenance marker; treated as a hand-written source commit. Release-Version: 0.5.0
1 parent eda73ce commit cc6e5ea

5 files changed

Lines changed: 104 additions & 2 deletions

File tree

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@
125125

126126
<build>
127127
<resources>
128+
<resource>
129+
<directory>src/main/resources</directory>
130+
<filtering>true</filtering>
131+
<includes>
132+
<include>com/volcengine/ark/runtime/sdk-version.properties</include>
133+
</includes>
134+
</resource>
128135
<resource>
129136
<directory>${project.basedir}</directory>
130137
<targetPath>META-INF</targetPath>

src/main/java/com/volcengine/ark/runtime/interceptor/RequestIdInterceptor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.volcengine.ark.runtime.Const;
77
import com.volcengine.ark.runtime.exception.ArkAPIError;
88
import com.volcengine.ark.runtime.exception.ArkHttpException;
9-
import com.volcengine.version.Version;
109
import java.io.IOException;
1110
import java.text.SimpleDateFormat;
1211
import java.util.Date;
@@ -52,6 +51,6 @@ private static String getUserAgent() {
5251
String jdkInfo = "java-" + System.getProperty("java.version");
5352
String arch = System.getProperty("os.arch");
5453

55-
return String.format(format, Version.SDK_NAME, Version.SDK_VERSION, jdkInfo, osInfo, arch);
54+
return String.format(format, "ark-runtime-java", SdkVersion.VERSION, jdkInfo, osInfo, arch);
5655
}
5756
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2026 ByteDance Ltd. and/or its affiliates.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.volcengine.ark.runtime.interceptor;
5+
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.util.Properties;
9+
10+
final class SdkVersion {
11+
static final String VERSION = loadVersion();
12+
13+
private SdkVersion() {}
14+
15+
private static String loadVersion() {
16+
// Maven filters this resource from project.version and includes it in the JAR.
17+
try (InputStream input = SdkVersion.class.getResourceAsStream(
18+
"/com/volcengine/ark/runtime/sdk-version.properties")) {
19+
if (input != null) {
20+
Properties properties = new Properties();
21+
properties.load(input);
22+
String version = properties.getProperty("version", "").trim();
23+
if (!version.isEmpty() && !version.contains("${")) {
24+
return version;
25+
}
26+
}
27+
} catch (IOException ignored) {
28+
// Source-only builds may omit the generated metadata; requests still work.
29+
}
30+
return "unknown";
31+
}
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version=${project.version}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2026 ByteDance Ltd. and/or its affiliates.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.volcengine.ark.runtime.interceptor;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertNotNull;
8+
import static org.junit.Assert.assertTrue;
9+
10+
import com.volcengine.ark.runtime.Const;
11+
import java.io.File;
12+
import javax.xml.parsers.DocumentBuilderFactory;
13+
import javax.xml.xpath.XPathFactory;
14+
import okhttp3.OkHttpClient;
15+
import okhttp3.Protocol;
16+
import okhttp3.Request;
17+
import okhttp3.Response;
18+
import okhttp3.ResponseBody;
19+
import org.junit.Test;
20+
21+
public class RequestIdInterceptorTest {
22+
@Test
23+
public void outgoingRequestUsesProjectVersionAndPreservesRuntimeSuffix() throws Exception {
24+
Request request = new Request.Builder()
25+
.url("https://example.com/api/v3/chat/completions")
26+
.header("User-Agent", "custom-agent")
27+
.header(Const.CLIENT_REQUEST_HEADER, "provided-request-id")
28+
.build();
29+
Request outgoing = intercept(request);
30+
String version = XPathFactory.newInstance().newXPath().evaluate("/project/version",
31+
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("pom.xml")));
32+
assertTrue(!version.isEmpty());
33+
assertEquals("ark-runtime-java/" + version + "/(java-" + System.getProperty("java.version")
34+
+ ";" + System.getProperty("os.name") + "-" + System.getProperty("os.version")
35+
+ ";" + System.getProperty("os.arch") + ")", outgoing.header("User-Agent"));
36+
assertEquals(1, outgoing.headers("User-Agent").size());
37+
assertEquals("provided-request-id", outgoing.header(Const.CLIENT_REQUEST_HEADER));
38+
}
39+
40+
@Test
41+
public void outgoingRequestStillGeneratesClientRequestId() throws Exception {
42+
Request outgoing = intercept(new Request.Builder().url("https://example.com").build());
43+
assertNotNull(outgoing.header(Const.CLIENT_REQUEST_HEADER));
44+
assertEquals(34, outgoing.header(Const.CLIENT_REQUEST_HEADER).length());
45+
assertTrue(outgoing.header("User-Agent").startsWith("ark-runtime-java/"));
46+
}
47+
48+
private Request intercept(Request request) throws Exception {
49+
OkHttpClient client = new OkHttpClient.Builder()
50+
.addInterceptor(new RequestIdInterceptor())
51+
.addInterceptor(chain -> new Response.Builder()
52+
.request(chain.request())
53+
.protocol(Protocol.HTTP_1_1)
54+
.code(200)
55+
.message("OK")
56+
.body(ResponseBody.create(null, "{}"))
57+
.build())
58+
.build();
59+
try (Response response = client.newCall(request).execute()) {
60+
return response.request();
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)