Skip to content
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
4 changes: 2 additions & 2 deletions alibabacloud-gateway-sls/Teafile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scope": "alibabacloud",
"name": "GatewaySLS",
"version": "0.1.11",
"version": "0.1.12",
"main": "./main.tea",
"maintainers": [
{
Expand Down Expand Up @@ -93,7 +93,7 @@
},
"releaselog": {
"changelog": [
"feat: sls support auto compress."
"[java]feat: sls java sdk support PutLogs with protobuf."
],
"compatible": true
}
Expand Down
14 changes: 7 additions & 7 deletions alibabacloud-gateway-sls/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-gateway-sls</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<packaging>jar</packaging>
<name>alibabacloud-gateway-sls</name>
<description>
Expand Down Expand Up @@ -49,12 +49,12 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-gateway-spi</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>credentials-java</artifactId>
<version>0.3.6</version>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
Expand All @@ -64,7 +64,7 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>openapiutil</artifactId>
<version>0.2.1</version>
<version>0.2.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
Expand All @@ -84,12 +84,12 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>darabonba-encode-util</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>darabonba-signature-util</artifactId>
<version>0.0.4</version>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
Expand All @@ -99,7 +99,7 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-gateway-sls-util</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ public void modifyRequest(com.aliyun.gateway.spi.models.InterceptorContext conte
// get body bytes
byte[] bodyBytes = null;
if (!com.aliyun.teautil.Common.isUnset(request.body)) {
if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "json") || com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "formData")) {
// PutLogs
if (com.aliyun.darabonbastring.Client.equals(request.action, "PutLogs")) {
bodyBytes = com.aliyun.gateway.sls.util.Client.serializeLogGroupToPB(request.body);
request.headers.put("content-type", "application/x-protobuf");
} else if (com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "json") || com.aliyun.darabonbastring.Client.equals(request.reqBodyType, "formData")) {
request.headers.put("content-type", "application/json");
String bodyStr = com.aliyun.teautil.Common.toJSONString(request.body);
bodyBytes = com.aliyun.teautil.Common.toBytes(bodyStr);
Expand Down
7 changes: 6 additions & 1 deletion alibabacloud-gateway-sls/main.tea
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ async function modifyRequest(context: SPI.InterceptorContext, attributeMap: SPI.
// get body bytes
var bodyBytes : bytes = null;
if (!Util.isUnset(request.body)) {
if (String.equals(request.reqBodyType, 'json') || String.equals(request.reqBodyType, 'formData')) {

// PutLogs
if (String.equals(request.action, 'PutLogs')) {
bodyBytes = SLS_Util.serializeLogGroupToPB(request.body);
request.headers['content-type'] = 'application/x-protobuf';
} else if (String.equals(request.reqBodyType, 'json') || String.equals(request.reqBodyType, 'formData')) {
request.headers['content-type'] = 'application/json';
var bodyStr = Util.toJSONString(request.body);
bodyBytes = Util.toBytes(bodyStr);
Expand Down
2 changes: 1 addition & 1 deletion alibabacloud-gateway-sls/util/Teafile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"scope": "alibabacloud",
"name": "GatewaySLS_Util",
"version": "0.0.4",
"version": "0.0.5",
"main": "./main.tea",
"maintainers": [
{
Expand Down
49 changes: 46 additions & 3 deletions alibabacloud-gateway-sls/util/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-gateway-sls-util</artifactId>
<version>0.3.0</version>
<version>0.4.0</version>
<packaging>jar</packaging>
<name>alibabacloud-gateway-sls-util</name>
<description>
Expand Down Expand Up @@ -54,12 +54,12 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
<version>1.2.3</version>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-util</artifactId>
<version>0.2.14</version>
<version>0.2.23</version>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
Expand All @@ -72,6 +72,11 @@
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -123,6 +128,44 @@
<waitUntil>published</waitUntil>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<artifactSet>
<includes>
<include>com.google.protobuf:protobuf-java</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.google.protobuf</pattern>
<shadedPattern>com.aliyun.gateway.sls.thirdparty.com.google.protobuf
</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>**/pom.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ public static Boolean isDecompressorAvailable(String compressType) throws Except
public static Long bytesLength(byte[] src) throws Exception {
return (long) src.length;
}

public static byte[] serializeLogGroupToPB(Object logGroup) throws Exception {
return LogGroupSerializer.serializeLogGroupToPB(logGroup);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package com.aliyun.gateway.sls.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;

public class LogGroupSerializer {
@SuppressWarnings("unchecked")
public static byte[] serializeLogGroupToPB(Object logGroup) throws Exception {
if (!(logGroup instanceof HashMap)) {
throw new IllegalArgumentException("Invalid body type " + logGroup.getClass());
}

Logs.LogGroup.Builder logs = Logs.LogGroup.newBuilder();
HashMap<String, Object> body = (HashMap<String, Object>) logGroup;

String topic = (String) body.get("Topic");
if (topic != null) {
logs.setTopic(topic);
}

String source = (String) body.get("Source");
if (source == null || source.isEmpty()) {
source = getSourceIP();
}
if (source != null && !source.isEmpty()) {
logs.setSource(source);
}

serializeLogTags(logs, body);

serializeLogs(logs, body);
return logs.build().toByteArray();
}

@SuppressWarnings("unchecked")
private static void serializeLogs(Logs.LogGroup.Builder logs, HashMap<String, Object> body) {
ArrayList<Object> logItems = (ArrayList<Object>) body.get("LogItems");
if (logItems == null) {
return;
}
for (Object obj : logItems) {
Logs.Log.Builder logsBuilder = logs.addLogsBuilder();
HashMap<String, Object> logItem = (HashMap<String, Object>) obj;
if (logItem == null) {
continue;
}
logsBuilder.setTime((Integer) logItem.get("Time"));
ArrayList<Object> contents = (ArrayList<Object>) logItem.get("Contents");
for (Object content : contents) {
HashMap<String, String> realContent = (HashMap<String, String>) content;
Logs.Log.Content.Builder contentBuilder = logsBuilder.addContentsBuilder();
contentBuilder.setKey(realContent.get("Key"));
contentBuilder.setValue(realContent.get("Value"));
}
Integer nanoTime = (Integer) logItem.get("TimeNs");
if (nanoTime != null) {
logsBuilder.setTimeNs(nanoTime);
}
}
}

@SuppressWarnings("unchecked")
private static void serializeLogTags(Logs.LogGroup.Builder logs, HashMap<String, Object> body) {
ArrayList<Object> logTags = (ArrayList<Object>) body.get("LogTags");
if (logTags == null) {
return;
}
for (Object obj : logTags) {
HashMap<String, String> tag = (HashMap<String, String>) obj;
Logs.LogTag.Builder tagBuilder = logs.addLogTagsBuilder();
tagBuilder.setKey(tag.get("Key"));
tagBuilder.setValue(tag.get("Value"));
}
}


private static volatile String localMachineIP = null;
private static String getSourceIP() {
if (localMachineIP != null) {
return localMachineIP;
}
String ip = getLocalMachineIP();
synchronized (LogGroupSerializer.class) {
localMachineIP = ip; // it's ok to overwrite
}
return localMachineIP;
}

private static String getLocalMachineIP() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface ni = networkInterfaces.nextElement();
if (!ni.isUp()) {
continue;
}
Enumeration<InetAddress> addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
final InetAddress address = addresses.nextElement();
if (!address.isLinkLocalAddress() && address.getHostAddress() != null) {
String ipAddress = address.getHostAddress();
if (ipAddress.equals(CONST_LOCAL_IP)) {
continue;
}
if (isIPV4Addr(ipAddress)) {
return ipAddress;
}
}
}
}
} catch (SocketException ex) {
// swallow it
} catch (Exception ex) {
// swallow it
}
return "";
}

private static boolean isIPV4Addr(final String ipAddress) {
if (ipAddress == null || ipAddress.isEmpty()) {
return false;
}
try {
final String[] tokens = ipAddress.split("\\.");
if (tokens.length != 4) {
return false;
}
for (String token : tokens) {
int i = Integer.parseInt(token);
if (i < 0 || i > 255) {
return false;
}
}
return true;
} catch (Exception ex) {
return false;
}
}

private static final String CONST_LOCAL_IP = "127.0.0.1";
}
Loading
Loading