88import com .alibaba .dashscope .utils .StringUtils ;
99import java .util .HashMap ;
1010import java .util .Map ;
11+ import java .util .UUID ;
1112
1213public final class DashScopeHeaders {
14+ private static final String SDK_CLIENT = "java-sdk" ;
15+ private static final String SDK_SESSION_ID = UUID .randomUUID ().toString ();
16+
1317 public static String userAgent () {
1418 return userAgent (null );
1519 }
@@ -29,6 +33,26 @@ public static String userAgent(String customUserAgent) {
2933 return userAgent ;
3034 }
3135
36+ /**
37+ * Check if SDK tracking headers are disabled via DASHSCOPE_DISABLE_SDK_HEADERS env var.
38+ */
39+ private static boolean isSdkHeadersDisabled () {
40+ String disable = System .getenv ("DASHSCOPE_DISABLE_SDK_HEADERS" );
41+ return "1" .equals (disable ) || "true" .equalsIgnoreCase (disable );
42+ }
43+
44+ /**
45+ * Add SDK tracking headers to the given map.
46+ * These headers are set first so that user-supplied customHeaders can override them.
47+ */
48+ private static void addSdkTrackingHeaders (Map <String , String > headers ) {
49+ if (!isSdkHeadersDisabled ()) {
50+ headers .put ("x-dashscope-sdk-client" , SDK_CLIENT );
51+ headers .put ("x-dashscope-sdk-version" , Version .version );
52+ headers .put ("x-dashscope-sdk-session-id" , SDK_SESSION_ID );
53+ }
54+ }
55+
3256 public static Map <String , String > buildWebSocketHeaders (
3357 String apiKey , boolean isSecurityCheck , String workspace , Map <String , String > customHeaders )
3458 throws NoApiKeyException {
@@ -46,6 +70,7 @@ public static Map<String, String> buildWebSocketHeaders(
4670 Map <String , String > headers = new HashMap <>();
4771 headers .put ("Authorization" , "Bearer " + ApiKey .getApiKey (apiKey ));
4872 headers .put ("user-agent" , userAgent (customUserAgent ));
73+ addSdkTrackingHeaders (headers );
4974 if (workspace != null && !workspace .isEmpty ()) {
5075 headers .put ("X-DashScope-WorkSpace" , workspace );
5176 }
@@ -85,6 +110,7 @@ public static Map<String, String> buildHttpHeaders(
85110 Map <String , String > headers = new HashMap <>();
86111 headers .put ("Authorization" , "Bearer " + ApiKey .getApiKey (apiKey ));
87112 headers .put ("user-agent" , userAgent (customUserAgent ));
113+ addSdkTrackingHeaders (headers );
88114 if (isSecurityCheck ) {
89115 headers .put ("X-DashScope-DataInspection" , "enable" );
90116 }
0 commit comments