All Products
Search
Document Center

Simple Log Service:Import trace data from Java applications to Simple Log Service by using OpenTelemetry SDK for Java

Last Updated:Oct 16, 2023

This topic describes how to import trace data from Java applications to Simple Log Service by using OpenTelemetry SDK for Java.

Prerequisites

  • A trace instance is created. For more information, see Create a trace instance.

  • A Java development environment is set up. The Java version is 8 or later.

    Note

    We recommend that you use Java Development Kit (JDK) 8u252 or a later version.

Method 1: (Recommended) Use a Java agent to automatically upload trace data

You can use a Java agent to automatically upload trace data to Simple Log Service in dozens of Java frameworks. For more information, see Supported libraries, frameworks, application servers, and JVMs.

Important

You cannot use a Java agent together with a SkyWalking agent or a Zipkin agent. If you use a Java agent together with a SkyWalking agent or a Zipkin agent, undefined behavior may occur.

  1. Download the latest version of a Java agent. For more information, see opentelemetry-java-instrumentation.

  2. Configure the Java agent.

    The following code provides an example on how to configure the environment variables for the -javaagent parameter of a Java Virtual Machine (JVM). For more information, see opentelemetry-java-instrumentation. You must replace the variables such as ${endpoint} and ${project} in the code with the actual values.

    export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
    export OTEL_EXPORTER_OTLP_ENDPOINT=https://${endpoint}
    export OTEL_EXPORTER_OTLP_COMPRESSION=gzip
    export OTEL_EXPORTER_OTLP_HEADERS=x-sls-otel-project=${project},x-sls-otel-instance-id=${instance},x-sls-otel-ak-id=${access-key-id},x-sls-otel-ak-secret=${access-key-secret}
    java -javaagent:/path/to/opentelemetry-javaagent-all.jar  -Dotel.resource.attributes=service.namespace=${service.namespace},service.name=${service},service.version=${version},host.name=${host},deployment.environment=${environment} -jar /path/to/your/app.jar
    Table 1. Variables

    Variable

    Description

    Example

    ${endpoint}

    The endpoint of the Simple Log Service project. Format: ${project}.${region-endpoint}:Port.

    • ${project}: the name of the Simple Log Service project.

    • ${region-endpoint}: the Simple Log Service endpoint for the region where the project resides. You can access Simple Log Service by using an internal or public endpoint. An internal endpoint can be accessed over the classic network or a virtual private cloud (VPC). A public endpoint can be accessed over the Internet. For more information, see Endpoints.

    • Port: the port number. The value is fixed as 10010.

    test-project.cn-hangzhou.log.aliyuncs.com:10010

    ${project}

    The name of the Simple Log Service project.

    test-project

    ${instance}

    The ID of the trace instance. For more information, see Create a trace instance.

    test-traces

    ${access-key-id}

    The AccessKey ID of your Alibaba Cloud account.

    We recommend that you use the AccessKey pair of a Resource Access Management (RAM) user that has only the write permissions on the Simple Log Service project. An AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to grant the write permissions on a specified project to a RAM user, see Use custom policies to grant permissions to a RAM user. For more information about how to obtain an AccessKey pair, see AccessKey pair.

    None

    ${access-key-secret}

    The AccessKey secret of your Alibaba Cloud account.

    We recommend that you use the AccessKey pair of a RAM user that has only the write permissions on the Simple Log Service project.

    None

    ${service.namespace}

    The namespace to which the service belongs.

    order

    ${service}

    The name of the service. Specify the value based on your business requirements.

    payment

    ${version}

    The version of the service. We recommend that you specify a version in the va.b.c format.

    v0.1.2

    ${host}

    The hostname.

    localhost

    ${environment}

    The deployment environment. Example: test environment or production environment. Specify the value based on your business requirements.

    pre

Method 2: Manually generate and upload trace data

If you use a self-managed framework or have special requirements, you can manually generate trace data and upload the data to Simple Log Service. In this example, Maven is used. For more information, see Manual Instrumentation.

  1. Add Maven dependencies.

    <dependencies>
        <dependency>
          <groupId>io.opentelemetry</groupId>
          <artifactId>opentelemetry-api</artifactId>
        </dependency>
        <dependency>
          <groupId>io.opentelemetry</groupId>
          <artifactId>opentelemetry-sdk</artifactId>
        </dependency>
        <dependency>
          <groupId>io.opentelemetry</groupId>
          <artifactId>opentelemetry-exporter-otlp</artifactId>
        </dependency>
        <dependency>
          <groupId>io.opentelemetry</groupId>
          <artifactId>opentelemetry-semconv</artifactId>
          <version>1.20.1-alpha</version>
        </dependency>
      </dependencies>
      <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-bom</artifactId>
            <version>1.20.1</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
        </dependencies>
      </dependencyManagement>
  2. Add initialization code.

    You must replace the variables such as ${endpoint} and ${project} in the following code with the actual values. For more information about the variables, see Variables.

      Resource resource = Resource.getDefault()
                    .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "service-name")))
                    .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAMESPACE, "namespace")))
                    .merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_VERSION, "1.0.0")))
                    .merge(Resource.create(Attributes.of(ResourceAttributes.HOST_NAME, "host-name")));
    
            SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
                    .addSpanProcessor(
                            BatchSpanProcessor.builder(OtlpGrpcSpanExporter.builder().setEndpoint("https://${endpoint}") // Set the .setEndpoint parameter to a value that starts with https://. Example: https://test-project.cn-hangzhou.log.aliyuncs.com:10010. 
                                    .addHeader("x-sls-otel-project", "${project}")
                                    .addHeader("x-sls-otel-instance-id", "${instance}")
                                    .addHeader("x-sls-otel-ak-id", "${access-key-id}")
                                    .addHeader("x-sls-otel-ak-secret", "${access-key-secret}").build()).build())
                    .setResource(resource).build();
    
            OpenTelemetry openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider)
                    .buildAndRegisterGlobal();
    
            Tracer tracer = openTelemetry.getTracer("instrumentation-library-name", "1.0.0");
            Span parentSpan = tracer.spanBuilder("parent").startSpan();
    
            try {
                Span childSpan = tracer.spanBuilder("child").setParent(Context.current().with(parentSpan)).startSpan();
                childSpan.setAttribute("test", "vllelel");
                childSpan.end();
            } finally {
                parentSpan.end();
            }

FAQ

What do I do if the "Could not find TLS ALPN provider" error message is returned by the Java agent when the JDK version is earlier than 8u252?Counld not find TLS ALPN provider

To resolve this issue, perform the following steps:

  1. Download a package.

  2. Run the following command to add the required JAR files.

    ${youpath} specifies the path to a JAR file. Replace each ${youpath} variable with the actual value.

    java -Xbootclasspath/p:${youpath}/netty-tcnative-boringssl-static-2.0.25.Final.jar -javaagent:${youpath}/opentelemetry-javaagent-all.jar -jar ${youpath}/demo2-0.0.1-SNAPSHOT.jar

What to do next