All Products
Search
Document Center

Simple Log Service:How do I implement OpenTelemetry automatic instrumentation in a Kubernetes cluster?

Last Updated:Jan 17, 2024

This topic describes how to implement OpenTelemetry automatic instrumentation in a Kubernetes cluster to upload trace data to Simple Log Service.

Procedure

  1. Log on to your Kubernetes cluster.

  2. Run the following command to install cert-manager:

    kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.9.1/cert-manager.yaml
  3. Run the following command to install OpenTelemetry Operator:

    kubectl apply -f https://github.com/open-telemetry/opentelemetry-operator/releases/latest/download/opentelemetry-operator.yaml

    The opentelemetry-operator.yaml configuration file specifies image addresses at ghcr.io. If you visit the addresses in China, slow access or errors may occur. To accelerate image access, you can change the addresses in the opentelemetry-operator.yaml configuration file. For example, you can use Simple Log Service-provided addresses to access the images.

    Replace the image address at ghcr.io with otel-image-mirror-registry.cn-beijing.cr.aliyuncs.com.

    Note

    Simple Log Service supports the following addresses of the China (Beijing) and China (Hangzhou) regions to accelerate images: otel-image-mirror-registry.cn-beijing.cr.aliyuncs.com and otel-image-mirror-registry.cn-hangzhou.cr.aliyuncs.com.

  4. Run the following command to install OpenTelemetry Collector:

    kubectl apply -f - <<EOF
    apiVersion: opentelemetry.io/v1alpha1
    kind: OpenTelemetryCollector
    metadata:
      name: otel
    spec:
      image: otel/opentelemetry-collector-contrib:latest
      config: |
        receivers:
          otlp:
            protocols:
              grpc:
              http:
        exporters:
          alibabacloud_logservice/logs:
            endpoint: "cn-hangzhou.log.aliyuncs.com"
            project: "demo-project"
            logstore: "store-logs"
            access_key_id: "access-key-id"
            access_key_secret: "access-key-secret"
          alibabacloud_logservice/metrics:
            endpoint: "cn-hangzhou.log.aliyuncs.com"
            project: "demo-project"
            logstore: "store-traces-metrics"
            access_key_id: "access-key-id"
            access_key_secret: "access-key-secret"
          alibabacloud_logservice/traces:
            endpoint: "cn-hangzhou.log.aliyuncs.com"
            project: "demo-project"
            logstore: "store-traces"
            access_key_id: "access-key-id"
            access_key_secret: "access-key-secret"
    
        service:
          pipelines:
            traces:
              receivers: [otlp]
              exporters: [alibabacloud_logservice/traces]
            logs:
              receivers: [otlp]
              exporters: [alibabacloud_logservice/logs]
            metrics:
              receivers: [otlp]
              exporters: [alibabacloud_logservice/metrics]
    EOF

    The following table describes the parameters in the command. You can modify the parameters based on your business requirements.

    Parameter

    Description

    endpoint

    The Simple Log Service endpoint. Example: cn-hangzhou.log.aliyuncs.com. For more information, see Endpoints.

    project

    The name of the project that you specify when you create a trace instance. For more information, see Create a trace instance.

    logstore

    The name of the Logstore. After you create a trace instance, Simple Log Service generates three Logstores in the specified project to store log data, metric data, and trace data. Specify the Logstore name based on your business requirements.

    • trace_instance_id-logs

    • trace_instance_id-traces-metrics

    • trace_instance_id-traces

    trace_instance_id specifies the ID of the trace instance. You can replace trace_instance_id based on your business requirements. For more information, see Create a trace instance.

    access_key_id

    The AccessKey ID that is used to access Simple Log Service. For more information, see AccessKey pair.

    access_key_secret

    The AccessKey secret that is used to access Simple Log Service. For more information, see AccessKey pair.

  5. Run the following command to install OpenTelemetry Auto-Instrumentation:

    kubectl apply -f - <<EOF
    apiVersion: opentelemetry.io/v1alpha1
    kind: Instrumentation
    metadata:
      name: my-java-instrumentation
    spec:
      exporter:
        endpoint: http://otel-collector:4317
      propagators:
        - tracecontext
        - baggage
        - b3
      java:
        image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-java:latest
      nodejs:
        image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-nodejs:latest
      python:
        image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-python:latest
        env:
          - name: OTEL_EXPORTER_OTLP_ENDPOINT
            value: http://otel-collector:4318
      dotnet:
        image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-dotnet:latest
        env:
          - name: OTEL_EXPORTER_OTLP_ENDPOINT
            value: http://otel-collector:4318
      apacheHttpd:
        image: ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-apache-httpd:latest
    EOF
    • For applications in different programming languages such as Java, Node.js, Python, and .NET, configure Auto-Instrumentation based on your business requirements.

    • If you replace the image address at ghcr with a Simple Log Service-provided image address and visit the addresses in China, slow access or errors may occur. For example, you can accelerate images by replacing the image address at ghcr.io with otel-image-mirror-registry.cn-beijing.cr.aliyuncs.com.

    Note

    Simple Log Service supports the following addresses of the China (Beijing) and China (Hangzhou) regions to accelerate images: otel-image-mirror-registry.cn-beijing.cr.aliyuncs.com and otel-image-mirror-registry.cn-hangzhou.cr.aliyuncs.com.

  6. Add the configuration for automatic instrumentation to your configuration file.

    Add the configuration script to the configuration file of your application based on your business requirements. Only Python, Node.js, Java, and .NET applications are supported.

    Important

    In the following code, my-java-instrumentation is the name of the Auto-Instrumentation that you install in the preceding step. The Auto-Instrumentation name is the value of the metadata.name parameter. You can change the name based on your business requirements.

    image.png

    • Java

      instrumentation.opentelemetry.io/inject-java: "my-java-instrumentation"
    • Python

      instrumentation.opentelemetry.io/inject-python: "my-java-instrumentation"
    • Node.js

      instrumentation.opentelemetry.io/inject-nodejs: "my-java-instrumentation"
    • dotNET

      instrumentation.opentelemetry.io/inject-dotnet: "my-java-instrumentation"