Code Monkey home page Code Monkey logo

swift-distributed-tracing-extras's Introduction

Swift Distributed Tracing Extras

Modules

Swift Distributed Tracing Extras ships the following extra modules:

OpenTelemetry Semantic Conventions

Getting Started

This module is complementary to swift-distributed-tracing so you will want to depend on it (the API package).

Then, you can depend on the extras package:

.package(url: "https://github.com/apple/swift-distributed-tracing.git", from: "..."),
.package(url: "https://github.com/apple/swift-distributed-tracing-extras.git", from: "..."),

If you are writing an application, rather than a library, you'll also want to depend on a specific tracer implementation. For available implementations refer to the swift-distributed-tracing README.md.

Usage

The OpenTelemetry Semantic Conventions package provides span attributes to work with OpenTelemetry's semantic conventions.

Semantic attributes enable users of swift-distributed-tracing to set attributes on spans using a type-safe pre-defined and well known attributes, like this:

import Tracing
import TracingOpenTelemetrySemanticConventions

// Example framework code, which handles an incoming request and starts a span for handling a request:
func wrapHandleRequest(exampleRequest: ExampleHTTPRequest) async throws -> ExampleHTTPResponse {
    try await InstrumentationSystem.tracer.withSpan(named: "test") { span in

        // Set semantic HTTP attributes before
        span.attributes.http.method = exampleRequest.method
        span.attributes.http.url = exampleRequest.url
        // ...

        // Hand off to user code to handle the request;
        // The current span already has all important semantic attributes set.
        let response = try await actualHandleRequest(exampleRequest: exampleRequest)

        // ... set any response attributes ... 
        
        return response
    }
}

You can also read attributes that are set on a span using the same property syntax:

assert(span.attributes.http.method == "GET")
// etc.

Without the semantic attributes package, one would be able to set the attributes using a type-unsafe approach, like this:

span.attributes["http.method"] = "\(exampleRequest.method)"
span.attributes["http.url"] = "\(exampleRequest.url)"

however this approach is error-prone as one can accidentally record not quite the right type of value, or accidentally use a wrong, unexpected, key that would then not be understood by Otel tracing backends.

Supported versions

This module supports: Swift 5.4+, on all platforms Swift is available.

swift-distributed-tracing-extras's People

Contributors

ktoso avatar slashmo avatar yim-lee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.