Before you can view the trace data of your application in the Managed Service for OpenTelemetry console, you must report the trace data to Managed Service for OpenTelemetry. This topic describes how to instrument a Swift application and report the trace data of the Swift application.
Prerequisites
Sample code
In this example, OpenTelemetry is used to report the trace data of a macOS command-line application written in Swift. The method used in this example also applies to iOS applications.
Download the sample code from opentelemetry-swift-demo.
Step 1: Create an application and add dependencies
Select the type of the application that you want to create. For example, choose macOS > Command Line Tool.
In XCode, choose
https://github.com/open-telemetry/opentelemetry-swift
in the search box, and then select version 1.4.1. , enterNoteFor more information about versions, visit the opentelemetry-swift page on GitHub.
Select the required packages.
The following figure shows the packages that are used in this example.
Step 2: Initialize OpenTelemetry
Create an exporter to export monitoring data.
Select one of the following methods to report trace data:
Method 1: Report trace data by using the gRPC protocol
Replace
<gRPC-endpoint>
and<gRPC-port>
with the endpoint and port number obtained in Prerequisites. In this example,host: "http://tracing-analysis-dc-hz.aliyuncs.com", port:8090
is used.Replace
<your-token>
with the authentication token obtained in Prerequisites.
let grpcChannel = ClientConnection( configuration: ClientConnection.Configuration.default( target: .hostAndPort("<gRPC-endpoint>", 8090), // Specify the endpoint that does not contain the http:// prefix. In this example, tracing-analysis-dc-hz.aliyuncs.com is used. eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1) ) ) let otlpGrpcConfiguration = OtlpConfiguration( timeout: OtlpConfiguration.DefaultTimeoutInterval, headers: [ ("Authentication","xxxxxx") ] ) let otlpGrpcTraceExporter = OtlpTraceExporter(channel: grpcChannel, config: otlpGrpcConfiguration)
Method 2: Report trace data by using the HTTP protocol
Replace
<HTTP-endpoint>
with the endpoint obtained in Prerequisites. In this example,http://tracing-analysis-dc-hz.aliyuncs.com/adapt_xxxx@xxxx_xxxx@xxxx/api/otlp/traces
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)
Obtain the tracer that is used to create a span.
Replace
<your-service-name>
with the name of the application whose data you want to report and<your-host-name>
with the hostname.Replace
<trace-exporter>
with different values based on the method that you use to report trace data in Step 1.Method 1: Replace
<trace-exporter>
withotlpGrpcTraceExporter
.Method 2: Replace
<trace-exporter>
withotlpHttpTraceExporter
.Method 3: Replace
<trace-exporter>
withconsoleTraceExporter
.
// Specify the application name and the 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>)) // Report data to Managed Service for OpenTelemetry. .with(resource: resource) .build()) // 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
Create a span, configure attributes and an event for 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: "www.aliyun.com") let attributes = [ "key": AttributeValue.string("value"), "result": AttributeValue.int(100) ] // your code... // Configure an event. span.addEvent(name: "computation complete", attributes: attributes) // Display the trace ID. print(span.context.traceId.hexString) // your code... // End the current span. span.end()
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... childSpan.end() // your code... parentSpan.end()
Start the application.
On the Applications page of the Managed Service for OpenTelemetry console, click the name of the application. On the page that appears, view the trace data.