SaFi Bank Space : Observability in Micronaut

For the past 20 years or so, IT teams have relied primarily on APM to monitor and troubleshoot applications. APM periodically samples and aggregates application and system data, called telemetry, that's known to be related to application performance issues. It analyzes the telemetry relative to key performance indicators (KPIs) and assembles the results in a dashboard for alerting operations and support teams to abnormal conditions that should be addressed to resolve or prevent issues

(blue star) Tracing

Requirement

  • Micronaut version: 3.7.0

  • Zipkin

Instructions

  1. Add Tracing dependency in build.gradle.kts

    annotationProcessor("io.micronaut.tracing:micronaut-tracing-opentelemetry-annotation:4.4.0")
    implementation("io.micronaut.tracing:micronaut-tracing-zipkin:4.4.0") 
    implementation("io.micronaut.tracing:micronaut-tracing-opentelemetry-http:4.4.0") // To enable creating span objects on the every HTTP server request, client request, server response and client response
    runtimeOnly("io.opentracing.contrib:opentracing-kafka-client:0.1.15") // To enable creating span object on the every kafka publish and listen
  2. Add tracing configuration in application.yml

    tracing:
      zipkin:
        enabled: true
        http:
          url: https://zipkin-gcp.apps.dev.safibank.online
        exclusions:
          - /health
          - /env/.*
        sampler:
          probability: 1
  3. To disabling trace in integration test you can add this configuration in application-test.yml

    micronaut:
      otel:
        enabled: false
  4. Add these in HTTP request headers

    1. X-B3-TraceId: 32 digits HEX, example: dfe01b238a9c1a2216f32228763e7dcb

    2. X-B3-SpanId: 16 digits HEX, example: 15a713f446e079bc

Result

Request example:

curl --location --request POST 'http://localhost:8080/cards' \
--header 'Customer-ID: 8f7f8a2c-14a3-422d-b1dd-6abc6325597d' \
--header 'X-B3-TraceId: 59548df1b4b393f882763ca5c312f18e' \
--header 'X-B3-SpanId: e2310e7a666dad61' \
--header 'Content-Type: application/json' \
--data-raw '{
    "cardType": "DD1",
    "cardPicture": "DEFAULT_LAYOUT_1",
    "embossName": "Ropiudin",
    "cardDelivery": {
        "address1": "Rappocini",
        "country": "Philiphines",
        "province": "Manila",
        "city": "Manila",
        "barangay": "Manila",
        "zipCode": "90222",
        "phone": "+6287811575611"
    }
}'

Google cloud trace result:

source: https://console.cloud.google.com/traces/list?project=safi-env-dev-apps&start=1664766740870&end=1664770340870&tid=59548df1b4b393f882763ca5c312f18e&pageState=("traceIntervalPicker":("groupValue":"PT1H","customValue":null))

(blue star) Logging (Exposing Trace Id to Log)

Requirement

  • Micronaut version: 3.7.0

Instructions

  1. See instruction in Tracing chapter,

  2. To exposing Trace Id to log we can put %X{X-B3-TraceId:-} pattern to logback.xml, e.g:

    <configuration>
    
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <withJansi>true</withJansi>
            <!-- encoders are assigned the type
                 ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
            <encoder>
                <pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - traceId: %X{X-B3-TraceId:-} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <root level="info">
            <appender-ref ref="STDOUT" />
        </root>
    </configuration>
    

Result

14:26:09.020 [io-executor-thread-2] INFO  p.s.c.service.impl.CardServiceImpl - traceId: 59548df1b4b393f882763ca5c312f211 - cardManager.createCard, customerId: 8f7f8a2c-14a3-422d-b1dd-6abc6325597d
14:26:10.954 [io-executor-thread-2] INFO  p.s.c.s.i.CreateSessionServiceImpl - traceId: 59548df1b4b393f882763ca5c312f211 - get create session by id: 8f7f8a2c-14a3-422d-b1dd-6abc6325597d-2022-10-05
14:26:10.988 [pool-1-thread-1] INFO  p.s.c.service.impl.CardServiceImpl - traceId:  - handle card submitted: {"cardId": "23a79053-09f0-4dbd-912e-eaed249c47f5", "customerId": "8f7f8a2c-14a3-422d-b1dd-6abc6325597d"}
14:26:11.020 [io-executor-thread-2] INFO  p.s.c.s.i.CreateSessionServiceImpl - traceId: 59548df1b4b393f882763ca5c312f211 - id: 8f7f8a2c-14a3-422d-b1dd-6abc6325597d-2022-10-05 is not exist, create new record

(blue star) Metrics (WIP)

Requirement

Instructions

Result

Related articles

https://guides.micronaut.io/latest/micronaut-microservices-distributed-tracing-zipkin-opentelemetry-gradle-java.html