Skip to content

Commit f2ff5a6

Browse files
committed
[SLS gateway]feat: support protobuf data
1 parent cf81112 commit f2ff5a6

File tree

22 files changed

+5038
-16
lines changed

22 files changed

+5038
-16
lines changed

alibabacloud-gateway-sls/java/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<dependency>
5555
<groupId>com.aliyun</groupId>
5656
<artifactId>credentials-java</artifactId>
57-
<version>0.3.6</version>
57+
<version>0.3.10</version>
5858
</dependency>
5959
<dependency>
6060
<groupId>com.aliyun</groupId>

alibabacloud-gateway-sls/java/src/main/java/com/aliyun/gateway/sls/Client.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ public void modifyRequest(com.aliyun.gateway.spi.models.InterceptorContext conte
7373
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "binary")) {
7474
// content-type: application/octet-stream
7575
bodyBytes = com.aliyun.teautil.Common.assertAsBytes(request.body);
76+
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "protobuf")) {
77+
byte[] pbBytes = com.aliyun.gateway.sls.util.Client.serializeToPbBytes(request.body);
78+
request.stream = Tea.toReadable(pbBytes);
79+
request.headers.put("content-type", "application/x-protobuf");
7680
}
7781

7882
}
@@ -199,17 +203,19 @@ public String makeContentHash(byte[] content, String signatureVersion) throws Ex
199203
public void modifyResponse(com.aliyun.gateway.spi.models.InterceptorContext context, com.aliyun.gateway.spi.models.AttributeMap attributeMap) throws Exception {
200204
com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextRequest request = context.request;
201205
com.aliyun.gateway.spi.models.InterceptorContext.InterceptorContextResponse response = context.response;
202-
if (com.aliyun.teautil.Common.is4xx(response.statusCode) || com.aliyun.teautil.Common.is5xx(response.statusCode)) {
206+
Number statusCode = response.statusCode;
207+
String requestId = response.headers.get("x-log-requestid");
208+
if (com.aliyun.teautil.Common.is4xx(statusCode) || com.aliyun.teautil.Common.is5xx(statusCode)) {
203209
Object error = com.aliyun.teautil.Common.readAsJSON(response.body);
204210
java.util.Map<String, Object> resMap = com.aliyun.teautil.Common.assertAsMap(error);
205211
throw new TeaException(TeaConverter.buildMap(
206212
new TeaPair("code", resMap.get("errorCode")),
207213
new TeaPair("message", resMap.get("errorMessage")),
208214
new TeaPair("accessDeniedDetail", resMap.get("accessDeniedDetail")),
209215
new TeaPair("data", TeaConverter.buildMap(
210-
new TeaPair("httpCode", response.statusCode),
211-
new TeaPair("requestId", response.headers.get("x-log-requestid")),
212-
new TeaPair("statusCode", response.statusCode)
216+
new TeaPair("httpCode", statusCode),
217+
new TeaPair("requestId", requestId),
218+
new TeaPair("statusCode", statusCode)
213219
))
214220
));
215221
}
@@ -235,6 +241,9 @@ public void modifyResponse(com.aliyun.gateway.spi.models.InterceptorContext cont
235241
response.deserializedBody = obj;
236242
} else if (com.aliyun.teautil.Common.equalString(request.bodyType, "array")) {
237243
response.deserializedBody = com.aliyun.teautil.Common.readAsJSON(uncompressedData);
244+
} else if (com.aliyun.teautil.Common.equalString(request.bodyType, "protobuf")) {
245+
byte[] pbBytes = com.aliyun.teautil.Common.readAsBytes(uncompressedData);
246+
response.deserializedBody = com.aliyun.gateway.sls.util.Client.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
238247
} else {
239248
response.deserializedBody = com.aliyun.teautil.Common.readAsString(uncompressedData);
240249
}

alibabacloud-gateway-sls/main.tea

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ async function modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.
6464
} else if (String.equals(request.reqBodyType, 'binary')) {
6565
// content-type: application/octet-stream
6666
bodyBytes = Util.assertAsBytes(request.body);
67+
} else if (String.equals(request.reqBodyType, 'protobuf')) {
68+
var pbBytes = SLS_Util.serializeToPbBytes(request.body);
69+
request.stream = pbBytes;
70+
request.headers['content-type'] = 'application/x-protobuf';
6771
}
6872
}
6973

@@ -72,7 +76,7 @@ async function modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.
7276
var rawSizeRef = request.headers['x-log-bodyrawsize']; // for php bug, Argument #1 ($value) could not be passed by reference
7377
if (!Util.isUnset(rawSizeRef)) {
7478
bodyRawSize = rawSizeRef;
75-
} else if (!Util.isUnset(request.body)) {
79+
} else if (!Util.isUnset(bodyBytes)) {
7680
bodyRawSize = `${SLS_Util.bytesLength(bodyBytes)}`;
7781
}
7882

@@ -175,17 +179,19 @@ async function makeContentHash(content: bytes, signatureVersion: string) : strin
175179
async function modifyResponse(context: SPI.InterceptorContext, attributeMap: SPI.AttributeMap): void {
176180
var request = context.request;
177181
var response = context.response;
178-
if (Util.is4xx(response.statusCode) || Util.is5xx(response.statusCode)) {
182+
var statusCode = response.statusCode;
183+
var requestId = response.headers['x-log-requestid'];
184+
if (Util.is4xx(statusCode) || Util.is5xx(statusCode)) {
179185
var error = Util.readAsJSON(response.body);
180186
var resMap = Util.assertAsMap(error);
181187
throw {
182188
code = resMap['errorCode'],
183189
message = resMap['errorMessage'],
184190
accessDeniedDetail = resMap['accessDeniedDetail'],
185191
data = {
186-
httpCode = response.statusCode,
187-
requestId = response.headers['x-log-requestid'],
188-
statusCode = response.statusCode,
192+
httpCode = statusCode,
193+
requestId = requestId,
194+
statusCode = statusCode,
189195
}
190196
};
191197
}
@@ -209,6 +215,9 @@ async function modifyResponse(context: SPI.InterceptorContext, attributeMap: SPI
209215
response.deserializedBody = obj;
210216
} else if (Util.equalString(request.bodyType, 'array')) {
211217
response.deserializedBody = Util.readAsJSON(uncompressedData);
218+
} else if (Util.equalString(request.bodyType, 'protobuf')) {
219+
var pbBytes = Util.readAsBytes(uncompressedData);
220+
response.deserializedBody = SLS_Util.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
212221
} else {
213222
response.deserializedBody = Util.readAsString(uncompressedData);
214223
}

alibabacloud-gateway-sls/ts/src/client.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ export default class Client extends SPI {
8484
} else if (String.equals(request.reqBodyType, "binary")) {
8585
// content-type: application/octet-stream
8686
bodyBytes = Util.assertAsBytes(request.body);
87+
} else if (String.equals(request.reqBodyType, "protobuf")) {
88+
let pbBytes = SLS_Util.serializeToPbBytes(request.body);
89+
request.stream = new $tea.BytesReadable(pbBytes);
90+
request.headers["content-type"] = "application/x-protobuf";
8791
}
8892

8993
}
@@ -208,17 +212,19 @@ export default class Client extends SPI {
208212
async modifyResponse(context: $SPI.InterceptorContext, attributeMap: $SPI.AttributeMap): Promise<void> {
209213
let request = context.request;
210214
let response = context.response;
211-
if (Util.is4xx(response.statusCode) || Util.is5xx(response.statusCode)) {
215+
let statusCode = response.statusCode;
216+
let requestId = response.headers["x-log-requestid"];
217+
if (Util.is4xx(statusCode) || Util.is5xx(statusCode)) {
212218
let error = await Util.readAsJSON(response.body);
213219
let resMap = Util.assertAsMap(error);
214220
throw $tea.newError({
215221
code: resMap["errorCode"],
216222
message: resMap["errorMessage"],
217223
accessDeniedDetail: resMap["accessDeniedDetail"],
218224
data: {
219-
httpCode: response.statusCode,
220-
requestId: response.headers["x-log-requestid"],
221-
statusCode: response.statusCode,
225+
httpCode: statusCode,
226+
requestId: requestId,
227+
statusCode: statusCode,
222228
},
223229
});
224230
}
@@ -244,6 +250,9 @@ export default class Client extends SPI {
244250
response.deserializedBody = obj;
245251
} else if (Util.equalString(request.bodyType, "array")) {
246252
response.deserializedBody = await Util.readAsJSON(uncompressedData);
253+
} else if (Util.equalString(request.bodyType, "protobuf")) {
254+
let pbBytes = await Util.readAsBytes(uncompressedData);
255+
response.deserializedBody = SLS_Util.deserializeFromPbBytes(pbBytes, statusCode, response.headers);
247256
} else {
248257
response.deserializedBody = await Util.readAsString(uncompressedData);
249258
}

alibabacloud-gateway-sls/util/java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<artifactId>lz4</artifactId>
5252
<version>1.3.0</version>
5353
</dependency>
54+
<dependency>
55+
<groupId>com.google.protobuf</groupId>
56+
<artifactId>protobuf-java</artifactId>
57+
<version>3.25.4</version>
58+
</dependency>
5459
<dependency>
5560
<groupId>com.aliyun</groupId>
5661
<artifactId>tea</artifactId>

alibabacloud-gateway-sls/util/java/src/main/java/com/aliyun/gateway/sls/util/Client.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// This file is auto-generated, don't edit it. Thanks.
22
package com.aliyun.gateway.sls.util;
33

4+
import com.aliyun.gateway.sls.util.model.ILog;
5+
import com.aliyun.gateway.sls.util.model.LogsResponseBody;
6+
47
import java.io.ByteArrayInputStream;
58
import java.io.InputStream;
9+
import java.util.Map;
610

711
public class Client {
812

@@ -49,4 +53,32 @@ public static Boolean isDecompressorAvailable(String compressType) throws Except
4953
public static Long bytesLength(byte[] src) throws Exception {
5054
return (long) src.length;
5155
}
56+
}
57+
public static byte[] serializeToPbBytes(Object request) {
58+
if (request instanceof ILog) {
59+
return ((ILog) request).serializeToPbBytes();
60+
}
61+
return null;
62+
}
63+
64+
public static LogsResponseBody deserializeFromPbBytes(byte[] uncompressedData, Integer statusCode, Map<String, String> headers) {
65+
return new LogsResponseBody(uncompressedData, statusCode, headers);
66+
}
67+
68+
public static int[] decodeVarInt32(byte[] dataBytes, int pos, int maxPos) {
69+
int value[] = {0, 0, 0};
70+
int shift = 0;
71+
int b;
72+
for (int i = pos; i < maxPos; ++i) {
73+
b = dataBytes[i] & 0xff;
74+
value[1] |= (b & 127) << shift;
75+
shift += 7;
76+
if ((b & 128) == 0) {
77+
value[2] = i + 1;
78+
value[0] = 1;
79+
break;
80+
}
81+
}
82+
return value;
83+
}
5284
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.aliyun.gateway.sls.util.exception;
2+
3+
import com.aliyun.tea.TeaConverter;
4+
import com.aliyun.tea.TeaException;
5+
import com.aliyun.tea.TeaPair;
6+
7+
public class LogException extends TeaException {
8+
public LogException(String code, String message, String requestId, int statusCode) {
9+
super(TeaConverter.buildMap(new TeaPair("code", code),
10+
new TeaPair("message", message),
11+
new TeaPair("data", TeaConverter.buildMap(
12+
new TeaPair("httpCode", statusCode),
13+
new TeaPair("requestId", requestId),
14+
new TeaPair("statusCode", statusCode)))));
15+
}
16+
17+
public LogException(String code, String message, String requestId) {
18+
this(code, message, requestId, -1);
19+
}
20+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.aliyun.gateway.sls.util.model;
2+
3+
import com.aliyun.gateway.sls.util.Client;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class FastLog {
9+
10+
private final byte[] rawBytes;
11+
// [beginOffset, endOffset)
12+
private final int beginOffset;
13+
private final int endOffset;
14+
private final List<FastLogContent> contents;
15+
private int time = -1;
16+
private int timeNsPart = 0;
17+
18+
public FastLog(byte[] rawBytes, int offset, int length) {
19+
this.rawBytes = rawBytes;
20+
this.beginOffset = offset;
21+
this.endOffset = offset + length;
22+
this.contents = new ArrayList<FastLogContent>();
23+
if (!parse()) {
24+
this.contents.clear();
25+
}
26+
}
27+
28+
private boolean parse() {
29+
int pos = this.beginOffset;
30+
int mode, index;
31+
boolean findTime = false;
32+
while (pos < this.endOffset) {
33+
int[] value = Client.decodeVarInt32(this.rawBytes, pos, this.endOffset);
34+
if (value[0] == 0) {
35+
return false;
36+
}
37+
mode = value[1] & 0x7;
38+
index = value[1] >> 3;
39+
if (mode == 0) {
40+
pos = value[2];
41+
value = Client.decodeVarInt32(this.rawBytes, pos, this.endOffset);
42+
if (value[0] == 0) {
43+
return false;
44+
}
45+
pos = value[2];
46+
if (index == 1) {
47+
this.time = value[1];
48+
findTime = true;
49+
}
50+
} else if (mode == 1) {
51+
pos = value[2] + 8;
52+
} else if (mode == 2) {
53+
pos = value[2];
54+
value = Client.decodeVarInt32(this.rawBytes, pos, this.endOffset);
55+
if (value[0] == 0) {
56+
return false;
57+
}
58+
pos = value[2] + value[1];
59+
if (index == 2) {
60+
this.contents.add(new FastLogContent(this.rawBytes, value[2], value[1]));
61+
}
62+
} else if (mode == 5) {
63+
if (index == 4) {
64+
timeNsPart = this.rawBytes[value[2]] & 255 | (this.rawBytes[value[2] + 1] & 255) << 8 | (this.rawBytes[value[2] + 2] & 255) << 16 | (this.rawBytes[value[2] + 3] & 255) << 24;
65+
}
66+
pos = value[2] + 4;
67+
} else {
68+
return false;
69+
}
70+
}
71+
return findTime && (pos == this.endOffset);
72+
}
73+
74+
public int getTime() {
75+
return this.time;
76+
}
77+
78+
public int getTimeNsPart() {
79+
return this.timeNsPart;
80+
}
81+
82+
public int getContentsCount() {
83+
return this.contents.size();
84+
}
85+
86+
public List<FastLogContent> getContents() {
87+
return contents;
88+
}
89+
90+
public FastLogContent getContents(int i) {
91+
if (i < this.contents.size()) {
92+
return this.contents.get(i);
93+
} else {
94+
return null;
95+
}
96+
}
97+
98+
}

0 commit comments

Comments
 (0)