All Projects → mjstrasser → ktor-features-zipkin

mjstrasser / ktor-features-zipkin

Licence: Apache-2.0 license
A Ktor feature that handles Open Zipkin tracing IDs

Programming Languages

kotlin
9241 projects

Labels

Projects that are alternatives of or similar to ktor-features-zipkin

Duga
Stack Exchange Chat bot
Stars: ✭ 20 (+33.33%)
Mutual labels:  ktor
ktor-health-check
Simple, opinionated health checks Kubernetes-style health checks for ktor
Stars: ✭ 26 (+73.33%)
Mutual labels:  ktor
github-profile
See Your Github Profile Summary
Stars: ✭ 21 (+40%)
Mutual labels:  ktor
tmdb-api
This Kotlin Multiplatform library is for accessing the TMDB API to get movie and TV show content. Using for Android, iOS, and JS projects.
Stars: ✭ 31 (+106.67%)
Mutual labels:  ktor
kompendium
Ktor OpenAPI Spec Generator
Stars: ✭ 46 (+206.67%)
Mutual labels:  ktor
kinsight-multiplatform
Kotlin Multiplatform Concept - iOS, MacOS, WatchOS (SwiftUI), Android (phone and Wear OS), JVM Web Server - Alpha Capture
Stars: ✭ 38 (+153.33%)
Mutual labels:  ktor
Lastik
Kotlin Multiplatform + Jetpack Compose pet project, based on www.last.fm/api (in development)
Stars: ✭ 37 (+146.67%)
Mutual labels:  ktor
Lavalink.kt
Coroutine based client for Lavalink (Kotlin and Java)
Stars: ✭ 31 (+106.67%)
Mutual labels:  ktor
ktor-auth-jwt-sample
A simple but slightly more elaborate example of how to include JWT in the Ktor application flow.
Stars: ✭ 100 (+566.67%)
Mutual labels:  ktor
api-service
The REST API backend server for the Jalgaon CoHelp application. Built with Kotlin Ktor.
Stars: ✭ 26 (+73.33%)
Mutual labels:  ktor
xodus-entity-browser
Web UI entity browser for xodus database
Stars: ✭ 56 (+273.33%)
Mutual labels:  ktor
ktor-hexagonal-multimodule
Template project to build ktor-based multi-module web service with Kotlin using Hexagonal architecture
Stars: ✭ 30 (+100%)
Mutual labels:  ktor
Kodein Di
Painless Kotlin Dependency Injection
Stars: ✭ 2,692 (+17846.67%)
Mutual labels:  ktor
meu kumbu
💵Meu Kumbu 💰 (Means My Money) is a template app that people can use to build their owns Wallet 🤑 or a mobile 🏦 banking app.
Stars: ✭ 55 (+266.67%)
Mutual labels:  ktor
sample-kotlin-ktor-microservices
sample microservices written in Kotlin that demonstrates usage of Ktor framework woth Consul server
Stars: ✭ 37 (+146.67%)
Mutual labels:  ktor
intro-kotlin-mutliplatform
Kotlin Multiplatform project (MPP), JVM and JS
Stars: ✭ 21 (+40%)
Mutual labels:  ktor
Saga
Saga pattern implementation in Kotlin build in top of Kotlin's Coroutines.
Stars: ✭ 24 (+60%)
Mutual labels:  ktor
ThinkRchive
An app showing all details for various Lenovo Thinkpad models. Made to try out Jepack Compose for Android.
Stars: ✭ 84 (+460%)
Mutual labels:  ktor
Scout
Scout is a kotlin multiplatform application that allows users to search and save games to lists to be browsed later.
Stars: ✭ 28 (+86.67%)
Mutual labels:  ktor
CleanArchitecture
Kotlin backend based on the Clean Architecture principles. Ktor, JWT, Exposed, Flyway, OpenAPI/REST & KGraphQL/GraphQL generated endpoints, Gradle.
Stars: ✭ 271 (+1706.67%)
Mutual labels:  ktor

Ktor: ZipkinIds feature

Build and Publish Maven Central

A Ktor installable feature for particiapting in Zipkin distributed tracing.

Why might you want this?

You are building microservices in Ktor and want to instrument them for tracing using Zipkin.

What does it do?

Incoming headers

The feature reads incoming HTTP tracing headers, either multiple headers:

  • X-B3-TraceId
  • X-B3-SpanId
  • X-B3-ParentSpanId
  • X-B3-Sampled or X-B3-Flags

or a single b3 header. See zipkin-b3-propagation for details.

Initiating tracing

The feature initiates tracing if configured, optionally only for specified paths.

Response headers

The feature returns tracing headers in the response that match those received or initiated.

Client requests

The feature propagates tracing into downstream client requests where installed into Ktor clients.

Logging MDC items

Optionally you can install the current tracing information into Slf4j mapped diagnostic context (MDC).

How do you use it?

There are two parts: a server feature and a client feature.

Server feature

An example is:

fun Application.module() {

    install(ZipkinIds) {
        initiateTracePathPrefixes = arrayOf("/api")
        b3Header = true
    }

    // Other feature installations

    routing {
        get("/health") {
            call.respond(mapOf("status" to "UP"))
        }

        post("/api/v1/service") {
            try {
                val message = call.receive<Message>()
                val result = call.processMessage(message)
                call.respond(HttpStatusCode.OK, result)
            } catch (e: Exception) {
                call.respond(HttpStatusCode.BadRequest, e.message ?: "There was an error processing your request")
            }
        }
    }
}

In this example, the feature is installed so it will initiate tracing only for requests starting with "/api".

  • Requests to /health without any tracing headers do not initiate tracing.
  • Requests to /api/v1/service without tracing headers initiate tracing.
  • Tracing initiated by the service respond with b3 headers and use them in client calls.
  • If requests contain tracing headers, those headers and their type (either b3 or X-B3-*) will be maintained.
  • Tracing information is stored in the ApplicationCall instance.

Client feature

To propagate tracing information into client calls, define extension functions on ApplicationCall, for example:

suspend fun ApplicationCall.processMessage(message: Message) {

    val url = "https://provider.com/api/v1/other-service"
    try {
        val client = HttpClient(CIO) {
            tracingParts?.let { it ->
                install(ClientIds) {
                    tracingParts = it
                }
            }
        }
        val response = client.post<String>(url) {
            body = TextContent(
                json.writeValueAsString(OutgoingMessage(message)),
                contentType = ContentType.Application.Json
            )
        }
    } catch (e: Exception) {
        logger.error("Client request failure", e)
    }
}

In this example:

  • When the client is being constructed, if the ApplicationCall contains a tracingParts attribute, install the ClientIds feature with that attribute.

Logging MDC

Install the keys into logging MDC in the application as part of call logging:

    install(CallLogging) {
        level = Level.INFO
        zipkinMdc()
        filter { call -> call.request.path().startsWith("/") }
    }

Keys available are:

  • b3Id
  • traceId
  • spanId
  • parentSpanId

They can be set in logback.xml like so:

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] [%X{b3Id}] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

Gradle dependency

repositories {
    // ...
    maven { url 'https://kotlin.bintray.com/ktor' }
    mavenCentral()
}

dependencies {
    // ...
    implementation 'com.michaelstrasser:ktor-features-zipkin:0.2.12'
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].