Use a custom domain name to report data

Updated at:

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

Disassociate a certificate

You can remove only the certificate from the RUM server, or remove both the certificate and the CNAME relationship between the project and domain.
// The custom domain name.
String cname = "xx.abc.com";
// The project.
String project = "${your_rum_sls_project_name}";


// Delete only the certificate.
SetProjectCnameRequest request = new SetProjectCnameRequest(project, cname);
request.setCertificateConfiguration(new CertificateConfiguration()
    .withDeleteCertificate(true));
client.setProjectCname(request);

// Delete the entire CNAME relationship.
client.deleteProjectCname(project, cname);

Query certificates

// The SLS project that corresponds to Real User Monitoring.
ListProjectCnameResponse response = client1.listProjectCname(project);
        System.out.println(response.getCnameConfigurations().size());
        System.out.println(response.getCnameConfigurations().get(0).getCertificate().getCreationDate());     System.out.println(response.getCnameConfigurations().get(0).getCertificate().getValidStartDate());
System.out.println(response.getCnameConfigurations().get(0).getCertificate().getValidEndDate());

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.

  1. 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.

  2. In your local hosts file, map the custom domain name to the IP address that you obtained.

  3. 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@yyyy becomes https://your.custom.domain/rum/web/v2?workspace=default-cms-xxxx-cn-hangzhou&service_id=xxx@yyyy after 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 to https://your.custom.domain.