KtorMonitor

repository·main·Indexed 42 days ago

https://github.com/cosminmihumdc/ktormonitor

A network monitoring tool for Ktor Client, OkHttp, and http4k providing real-time interception, logging, and rich previews of network traffic. Supports Android, iOS, JVM, Wasm, and JS. Features include built-in viewers for JSON, XML, HTML, and images, support for SSE and WebSockets, and no-op variants for production safety. Version 1.16.0.

Tokens
7K
Snippets
22
Records
31
Agent score
54%

What's inside KtorMonitor

  1. Overview of KtorMonitor

    main

    KtorMonitor is a powerful tool designed to monitor, intercept, and log network traffic for Ktor Client, OkHttp, and http4k. It facilitates easier debugging and analysis of requests and responses in real-time.

    Key features include:

    • Multiplatform Support: Works on Android, iOS, Desktop (JVM), Wasm, and JS.
    • Rich Previews: Built-in viewers for JSON, XML, HTML, CSS, YAML, Markdown, Form Data, and various image formats (JPG, PNG, SVG, GIF, WEBP).
    • Security: Ability to redact sensitive headers like Authorization.
    • Advanced Protocols: Supports tracking SSE (Server-Sent Events) and WebSockets.
    • Production Safety: Provides a no-op variant to ensure monitoring code is excluded from production builds.
  2. Overview of KtorMonitor features

    main

    KtorMonitor is a powerful tool designed to monitor, intercept, and log network traffic for Ktor Client, OkHttp, and http4k. It is built with a focus on debugging and analyzing network communication across multiple platforms.

    Key Capabilities

    • Supported Clients:
      • Ktor Client: Real-time interception and logging.
      • OkHttp: Real-time interception and logging.
      • http4k: Real-time interception and logging.
    • Platform Support (Kotlin Multiplatform):
      • Android, iOS, Desktop (JVM), Wasm, and JS.
    • Advanced Monitoring:
      • Streaming: Tracks one-way streams (SSE) and bidirectional traffic (WebSockets).
      • Rich Previews: Built-in viewers for JSON, XML, HTML, CSS, YAML, MARKDOWN, Form Data, and various image formats (JPG, PNG, SVG, GIF, WEBP).
    • Developer Experience:
      • Highly Configurable: Customize retention periods, content length limits, and notification behavior.
      • Security: Ability to redact sensitive headers (e.g., Authorization).
      • Data Export: Save request/response details to local files for sharing or debugging.
      • Production Safety: Includes a No-Op version to ensure monitoring code is excluded from production builds.
  3. Add KtorMonitor to Android

    main

    To use KtorMonitor on Android:

    • Use the KtorMonitor() Composable directly within your Composable code.
    • Notifications: If showNotifcation = true and the android.permission.POST_NOTIFICATIONS permission is granted, the library displays a notification summarizing ongoing KTOR activity. Tapping the notification launches the full KtorMonitor UI.
    • Compatibility: For apps targeting Android minSdk < 26, you must enable Core Library Desugaring.
  4. Setup KtorMonitor for Kotlin Multiplatform (Ktor Client)

    main

    To use KtorMonitor in a Kotlin Multiplatform project with Ktor Client, add the dependency to your commonMain source set.

    To ensure monitoring code is not included in your production artifacts, use the ktor-monitor-logging-no-op variant for release builds.

    // Standard implementation
    kotlin {
        sourceSets {
            commonMain.dependencies {
                implementation("ro.cosminmihu.ktor:ktor-monitor-logging:1.16.0")
            }
        }
    }
    
    // For Release Builds (No-Op)
    kotlin {
        sourceSets {
            commonMain.dependencies {
                implementation("ro.cosminmihu.ktor:ktor-monitor-logging-no-op:1.16.0")
            }
        }
    }
  5. Integrate KtorMonitor in Wasm / Js

    main

    Web targets (Wasm/JS) require additional Webpack configuration to handle dependencies like sql.js and its WASM binary.

    1. Add dependency to Gradle:
    kotlin {
        sourceSets {
            webMain.dependencies {
                implementation(devNpm("copy-webpack-plugin", "9.1.0"))
            }
        }
    }
    1. Configure Webpack: Create a file at {project}/webpack.config.d/sqljs.js to handle module resolution and copy the .wasm file:
    // {project}/webpack.config.d/sqljs.js
    config.resolve = {
        fallback: {
            fs: false,
            path: false,
            crypto: false,
        }
    };
    
    const CopyWebpackPlugin = require('copy-webpack-plugin');
    config.plugins.push(
        new CopyWebpackPlugin({
            patterns: [
                '../../node_modules/sql.js/dist/sql-wasm.wasm'
            ]
        })
    );
    1. Render in Compose:
    ComposeViewport {
        App()
    }
  6. Setup KtorMonitor for Android (Ktor Client)

    main

    For Android projects using Ktor Client, use debugImplementation for the standard library and releaseImplementation for the no-op variant to isolate monitoring from production.

    Note: For Android minSdk < 26, Core Library Desugaring is required.

    dependencies {
        debugImplementation("ro.cosminmihu.ktor:ktor-monitor-logging:1.16.0")
        releaseImplementation("ro.cosminmihu.ktor:ktor-monitor-logging-no-op:1.16.0")
    }
  7. Integrate KtorMonitor in Android

    main

    For Android applications, you can use the KtorMonitor() Composable directly in your Composable code.

    Key Behaviors:

    • If showNotification = true and the android.permission.POST_NOTIFICATIONS permission is granted, the library displays a notification summarizing ongoing KTOR activity. Tapping this notification launches the full KtorMonitor UI.
    • Compatibility: For apps targeting Android minSdk < 26, you must enable Core Library Desugaring.
  8. How to ask a question or report a bug

    main

    To ensure a quick and effective response when asking questions or reporting bugs on GitHub, please provide the following information:

    1. KtorMonitor version and the specific artifact being used (e.g., ktor-monitor-logging, ktor-monitor-okhttp-interceptor).
    2. Platform / target (e.g., Android, iOS, JVM Desktop, JS, Wasm).
    3. Kotlin version and Compose Multiplatform version.
    4. A minimal reproducible example (the smallest possible code snippet demonstrating the issue).
    5. Expected behaviour vs actual behaviour.
    6. Relevant logs or stack traces (ensure you redact sensitive headers or credentials).

    Note on Security: Do not report security vulnerabilities via public GitHub issues. Follow the process in SECURITY.md instead.

  9. Setup Ktor Monitor for Android

    main

    For Android projects, use debugImplementation for the standard library and releaseImplementation with the -no-op variant to isolate monitoring from production builds.

    Note: For Android projects with minSdk < 26, Core Library Desugaring is required.

    dependencies {
        debugImplementation("ro.cosminmihu.ktor:ktor-monitor-logging:1.16.0")
        releaseImplementation("ro.cosminmihu.ktor:ktor-monitor-logging-no-op:1.16.0")
    }