Use a custom domain name to report data
Real User Monitoring (RUM) allows you to report data through a custom domain name. The process involves four steps: uploading a certificate, verifying data reporting locally, modifying DNS resolution, and switching to the custom domain name as your reporting endpoint.
Prerequisites
-
Your application is integrated with RUM and the corresponding SLS project is created.
To find the SLS project for RUM, navigate to Application Name > Application Settings > Basic Information > Application Information > Log Information.
-
Java SDK 0.6.78 or later is required to upload certificates. Maven dependency:
<dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>aliyun-log</artifactId> <version>0.6.142</version> </dependency>
Procedure
Step 1: Upload a certificate
Permissions
Use the AccessKey pair of a RAM user with the required permissions. Manage RAM user permissions.
Grant the log:PutCname, log:DeleteCname, and log:ListCname permissions on the RUM project resource. Minimum policy:
{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": ["log:PutCname", "log:DeleteCname", "log:ListCname"],
"Resource": "acs:log:*:*:project/${your_rum_sls_project_name}/*"
}
]
}
Sample code for uploading a certificate
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.CertificateConfiguration;
import com.aliyun.openservices.log.request.SetProjectCnameRequest;
import com.aliyun.openservices.log.response.ListProjectCnameResponse;
import org.junit.Test;
import java.io.*;
import java.nio.file.Files;
public class CnameSample {
public static String readStreamAsString(InputStream in, String charset) throws IOException {
if (in == null) {
return "";
}
Reader reader = null;
Writer writer = new StringWriter();
String result;
char[] buffer = new char[1024];
try {
int n = -1;
reader = new BufferedReader(new InputStreamReader(in, charset));
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
result = writer.toString();
} finally {
in.close();
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
}
return result;
}
private static String readFileAsString(String filename) throws Exception {
File file = new File(filename);
return readStreamAsString(Files.newInputStream(file.toPath()), "utf-8");
}
@Test
public void testCname() throws Exception {
// The public key file for the custom domain name certificate.
String pubKey = readFileAsString("public.pem");
// The private key file for the custom domain name certificate.
String priKey = readFileAsString("private.key");
// The project that corresponds to Real User Monitoring.
String project = "${your_rum_sls_project_name}";
// The custom domain name, for example, xx.abc.com.
String cname = "your_cname";
// The endpoint for Simple Log Service. This example uses Beijing. Replace it with the endpoint for your region.
String endpoint = "cn-beijing.log.aliyuncs.com";
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
SetProjectCnameRequest request1 = new SetProjectCnameRequest(project, cname);
request1.setCertificateConfiguration(new CertificateConfiguration()
.withPublicKey(pubKey)
.withPrivateKey(priKey));
Client client = new Client(endpoint,
accessKeyId,
accessKeySecret);
client.setProjectCname(request1);
}
}
Parameters for the client object:
|
Parameter name |
Description |
|
endpoint |
Use the public network endpoint for your region. Endpoints. |
|
accessKeyId |
The AccessKey ID of a RAM user. View the AccessKey information of a RAM user. |
|
accessKeySecret |
The AccessKey secret of a RAM user. View the AccessKey information of a RAM user. |
Disassociate and query certificates
Step 2: Verify data reporting locally
Before switching DNS, verify that data reporting works on your local machine to confirm the certificate was uploaded correctly.
-
Run dig to obtain an IP address for ${your_rum_sls_project}.${sls_public_network_endpoint_for_the_region}. Public network endpoints are listed in Step 1: Upload a certificate.
-
In your local hosts file, map the custom domain name to the IP address that you obtained.
-
In the RUM integration configuration, replace the domain name part of the endpoint with your custom domain name and test data reporting locally. Step 4: Use a custom domain name to report data.
Step 3: Modify the DNS record for the custom domain name
After local verification succeeds, create a CNAME record that points your custom domain name to ${your_rum_sls_project}.${sls_public_network_endpoint_for_the_region}.
Step 4: Use the custom domain name to report data
-
For Web, miniapp, mini-game, Uniapp, Flutter, and ReactNative applications, replace the domain name in the endpoint with your custom domain name. The endpoint is at Application Settings > Basic Information > Integrate Probe.
-
Example: a web application endpoint
https://proj-xtrace-xxxx-cn-hangzhou.cn-hangzhou.log.aliyuncs.com/rum/web/v2?workspace=default-cms-xxxx-cn-hangzhou&service_id=xxx@yyyybecomeshttps://your.custom.domain/rum/web/v2?workspace=default-cms-xxxx-cn-hangzhou&service_id=xxx@yyyyafter replacement.
-
-
For Android, iOS, and HarmonyOS applications: if the SDK version is earlier than 2.0.0, replace the domain name in ConfigAddress; if 2.0.0 or later, replace the domain name in Endpoint.
-
For an Android application, for example, if your SDK version is < 2.0.0, change ConfigAddress to
https://your.custom.domain. If your SDK version is ≥ 2.0.0, change Endpoint tohttps://your.custom.domain.
-