Before you can view the trace data of your application in the Tracing Analysis console, you must submit the trace data to Tracing Analysis. This topic describes how to instrument a Swift application and submit trace data.


To obtain an endpoint of OpenTelemetry, perform the following steps:
  1. Log on to the Tracing Analysis console.
  2. In the left-side navigation pane, click Cluster Configurations. Then, click the Access point information tab.
  3. In the top navigation bar, select a region. In the Cluster Information section, turn on Show Token.
  4. In the Client section, click OpenTelemetry.
    Obtain an endpoint of OpenTelemetry in the Related Information column of the table in the lower part. Endpoint of OpenTelemetry
    Note If your application is deployed in an Alibaba Cloud production environment, use a private endpoint. Otherwise, use a public endpoint.

Sample code

In this example, OpenTelemetry is used to submit the trace data of a macOS command-line application written in Swift. The method used in this example also applies to iOS applications.

To download the sample code, click opentelemetry-swift-demo.

Step 1: Create an application and add dependencies

  1. Select the type of the application that you want to create. In this example, select macOS and Command Line Tool.
    Command Line Tool
  2. In XCode, choose File > Add Packages..., enter in the search box, and then select version 1.4.1.
    Note For more information about versions, see opentelemetry-swift releases.
  3. Select the required package products.
    The following figure shows the package products that are used in this example.Package Products

Step 2: Initialize OpenTelemetry

  1. Create an exporter to export monitoring data.
    Select one of the following methods to submit trace data.
    • Method 1: Submit trace data by using the gRPC protocol
      • Replace <gRPC-endpoint> and <gRPC-port> with the endpoint obtained in the Prerequisites section. In this example, host: "", port:8090 is used.
      • Replace <your-token> with the authentication token obtained in the Prerequisites section.
      let grpcChannel = ClientConnection.usingPlatformAppropriateTLS(for: MultiThreadedEventLoopGroup(numberOfThreads:1))
      .connect(host: "<gRPC-endpoint>", port:<gRPC-port>)
      let otlpGrpcConfiguration = OtlpConfiguration(
          timeout: OtlpConfiguration.DefaultTimeoutInterval,
          headers: [
      let otlpGrpcTraceExporter = OtlpTraceExporter(channel: grpcChannel, config: otlpGrpcConfiguration)
    • Method 2: Submit trace data by using the HTTP protocol

      Replace <HTTP-endpoint> with the endpoint obtained in the Prerequisites section. In this example, is used.

      let url = URL(string: "<HTTP-endpoint>")
      let otlpHttpTraceExporter = OtlpHttpTraceExporter(endpoint: url!)
    • Method 3: Obtain trace data in the command line
      let consoleTraceExporter = StdoutExporter(isDebug: true)
  2. Obtain the tracer that is used to create a span.
    • Replace <your-service-name> with the name of the application and <your-host-name> with the hostname.
    • Replace <trace-exporter> with different values based on the method that you use to submit trace data in Step 1.
      • Method 1: Replace <trace-exporter> with otlpGrpcTraceExporter.
      • Method 2: Replace <trace-exporter> with otlpHttpTraceExporter.
      • Method 3: Replace <trace-exporter> with consoleTraceExporter.
    // Configure the application name and hostname.
    let resource = Resource(attributes: [
        ResourceAttributes.serviceName.rawValue: AttributeValue.string("<your-service-name>"),
        ResourceAttributes.hostName.rawValue: AttributeValue.string("<your-host-name>")
    // Configure the TracerProvider.
    OpenTelemetry.registerTracerProvider(tracerProvider: TracerProviderBuilder()
                                         .add(spanProcessor: BatchSpanProcessor(spanExporter: <trace-exporter>)) // Submit trace data to Tracing Analysis.
                                         .with(resource: resource)
    // Obtain the tracer that is used to create a span.
    let tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "instrumentation-library-name", instrumentationVersion: "1.0.0")

Step 3: Create a span to track trace data

  1. Create a span, and configure attributes and an event to the span, and then obtain the trace ID of the span.
    let span = tracer.spanBuilder(spanName: "first span").startSpan()
    // Configure attributes.
    span.setAttribute(key: "http.method", value: "GET")
    span.setAttribute(key: "http.url", value: "")
    let attributes = [
        "key": AttributeValue.string("value"),
    // your code...
    // Configure the event.
    span.addEvent(name: "computation complete", attributes: attributes)
    // Print the trace ID.
    // your code...
    // End the current span.
  2. Create a nested span.
    let parentSpan = tracer.spanBuilder(spanName: "parent span").startSpan()
    // your code...
    let childSpan = tracer.spanBuilder(spanName: "child span").setParent(parentSpan).startSpan()
    // your code...
    // your code...
  3. Start the application.

    On the Applications page in the Tracing Analysis console, click the name of the application. On the page that appears, view the trace data.