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

Procedure

  1. Log on to your Kubernetes cluster.
  2. Install cert-manager.
    kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.6.1/cert-manager.yaml
  3. Deploy OpenTelemetry Operator.
    1. Download the opentelemetry-operator.yaml file.
      wget https://github.com/open-telemetry/opentelemetry-operator/releases/latest/download/opentelemetry-operator.yaml
    2. Open the opentelemetry-operator.yaml file and replace the image information in the file.
      Replace ghcr.io/open-telemetry/opentelemetry-operator/opentelemetry-operator in the opentelemetry-operator.yaml file with sls-registry.cn-beijing.cr.aliyuncs.com/opentelemetry-operator/opentelemetry-operator. Collect Ingress trace data
    3. Deploy OpenTelemetry Operator.
      kubectl apply -f opentelemetry-operator.yaml
  4. Deploy OpenTelemetry Collector.
    1. Create a YAML file.
      vim collector.yaml
    2. Enter the following script in the YAML file and configure the parameters based on your business requirements:
      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]
                                      
      Parameter Description
      endpoint The 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, 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 Log Service. For more information, see AccessKey pair.
      access_key_secret The AccessKey secret that is used to access Log Service. For more information, see AccessKey pair.
    3. Run the following command to apply the configuration:
      kubectl apply -f collector.yaml
  5. Deploy OpenTelemetry Auto-Instrumentation.
    1. Create a YAML file.
      vim instrumentation.yaml
    2. Enter the following script in the YAML file:
      apiVersion: opentelemetry.io/v1alpha1
      kind: Instrumentation
      metadata:
        name: my-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
    3. Configure an environment variable to pass the service name and module name to the container.
      OTEL_RESOURCE_ATTRIBUTES=service.name=your_service,service.namespace=your_service_namespace
      Parameter Description
      service.name The name of the service. Replace your_service with the actual value.
      service.namespace The name of the module. Replace your_service_namespace with the actual value.
    4. Run the following command to apply the configuration:
      kubectl apply -f instrumentation.yaml
  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, and Java applications are supported.

    Instrumentation
    • Java
      instrumentation.opentelemetry.io/inject-java: 'true'
    • Python
      instrumentation.opentelemetry.io/inject-python: "true"
    • Node.js
      instrumentation.opentelemetry.io/inject-nodejs: "true"