Skip to content

Add request, response log for the Portkey PII plugin to the main requ…#1052

Merged
VisargD merged 4 commits into
mainfrom
enhancement/portkey-pii-tracing
Jun 24, 2025
Merged

Add request, response log for the Portkey PII plugin to the main requ…#1052
VisargD merged 4 commits into
mainfrom
enhancement/portkey-pii-tracing

Conversation

@roh26it
Copy link
Copy Markdown
Collaborator

@roh26it roh26it commented Apr 21, 2025

Code Quality new feature

Title: Add request, response log for the Portkey PII plugin to the main request trace

🔄 What Changed

  • Added new interfaces for structured logging (LogObjectRequest, LogObjectResponse, LogObjectMetadata, LogObject)
  • Modified fetchPortkey function to capture request/response data and timing information
  • Updated PII detection flow to include logs in the response
  • Added error handling to capture logs even when errors occur
  • Changed base URL from api.portkey.ai to api.portkeydev.com

🔍 Impact of the Change

  • Enhanced observability with detailed logs of PII detection operations
  • Improved debugging capabilities with request/response timing information
  • Better traceability by including PII detection logs in the main request trace
  • More robust error handling with log capture during failures

📁 Total Files Changed

  • plugins/portkey/globals.ts: Added logging interfaces and updated fetchPortkey function
  • plugins/portkey/pii.ts: Modified PII detection flow to include logs

🧪 Test Added

N/A - No tests were added in this PR

🔒 Security Vulnerabilities

No security vulnerabilities were introduced. The PR improves security observability.

Quality Recommendations

  1. Add proper error handling for the case when both the Cloudflare service binding and regular post request fail

  2. Consider adding type safety for the error object in the catch block

  3. Add validation for the response data before processing it

  4. Consider adding unit tests for the new logging functionality

Comment thread plugins/portkey/globals.ts Outdated
Comment thread plugins/portkey/globals.ts Outdated
@matterai-app
Copy link
Copy Markdown
Contributor

matterai-app Bot commented Jun 24, 2025

Code Quality new feature maintainability

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This Pull Request introduces comprehensive logging for the Portkey PII plugin. New interfaces (LogObjectRequest, LogObjectResponse, LogObjectMetadata, LogObject) have been defined in plugins/portkey/globals.ts to structure the log data. The fetchPortkey function has been significantly refactored to capture detailed request and response information, including URL, method, headers, body, status, response time, and metadata. This function now returns both the PII response and the newly created LogObject. The detectPII and handler functions in plugins/portkey/pii.ts have been updated to receive and propagate this LogObject throughout the call chain. Error handling has been enhanced to ensure that the log object is also captured and returned when an error occurs during the Portkey API call.

🔍 Impact of the Change

This change significantly enhances the observability and debugging capabilities for interactions with the Portkey PII plugin. Developers can now access detailed logs of API requests and responses, including timing and metadata, which will greatly assist in troubleshooting, performance analysis, and understanding the plugin's behavior. The structured logging also lays the groundwork for integration with external monitoring and tracing systems.

📁 Total Files Changed

2 files were changed in this pull request.

🧪 Test Added

No specific unit or integration tests were explicitly detailed or provided in the patch data for this pull request. The changes primarily focus on adding logging capabilities and refactoring existing functions to accommodate the new log object.

🔒Security Vulnerabilities

No new security vulnerabilities were detected in the changes introduced by this pull request. The changes focus on logging and do not introduce new external dependencies or alter existing security-sensitive logic.

Motivation

To improve the observability and debugging experience for the Portkey PII plugin by providing detailed request and response logs for all interactions.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing (Not explicitly detailed in the PR data, but assumed for feature verification)

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

N/A

Tip

Quality Recommendations

  1. Consider using a more robust UUID/ULID generation library for generateSpanId instead of Math.random() to ensure uniqueness and avoid potential collisions in high-volume scenarios, which is crucial for reliable tracing.

  2. The LogObjectMetadata interface includes traceId and parentSpanId fields, but these are not currently populated. To enable full distributed tracing capabilities, integrate with an existing tracing system (e.g., OpenTelemetry) to propagate and populate these IDs.

  3. In fetchPortkey, the catch block uses e: any. While functional, it's generally better practice to narrow the type of e if possible (e.g., e instanceof Error) or at least perform more specific checks on the error object to ensure robustness.

  4. As the logging mechanism expands, consider centralizing log creation and propagation using a dedicated logging utility or context provider to maintain consistency and reduce boilerplate code across different functions.

Sequence Diagram

sequenceDiagram
    participant User as External System/User
    participant H as handler (plugins/portkey/pii.ts)
    participant D as detectPII (plugins/portkey/pii.ts)
    participant F as fetchPortkey (plugins/portkey/globals.ts)
    participant P as Portkey PII API

    User->>H: Call handler(context, eventType, options)
    activate H
    H->>D: detectPII(textArray, parameters, env)
    activate D
    D->>F: fetchPortkey(env, PORTKEY_ENDPOINTS.PII, credentials, data, timeout)
    activate F
    F->>P: POST ${BASE_URL}${endpoint} (data, headers: x-portkey-api-key)
    activate P
    P-->>F: HTTP Response (status, headers, body)
    deactivate P
    alt Successful Response
        F-->>D: { response: PIIResponse[], log: LogObject }
    else Error during API call
        F--x D: Throw { error, log: LogObject }
    end
    deactivate F
    alt Successful PII Detection
        D-->>H: { results: PIIResult[], log: LogObject }
    else Error from fetchPortkey
        D--x H: Propagate Error { error, log: LogObject }
    end
    deactivate D
    alt Successful Handler Execution
        H-->>User: { error: null, verdict, data, transformedData, transformed, log: LogObject }
    else Error in Handler
        H-->>User: { error: Error, verdict, data, transformedData, transformed, log: LogObject }
    end
    deactivate H
Loading

@matterai-app
Copy link
Copy Markdown
Contributor

matterai-app Bot commented Jun 24, 2025

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@matterai-app
Copy link
Copy Markdown
Contributor

matterai-app Bot commented Jun 24, 2025

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@VisargD VisargD merged commit 7073b5d into main Jun 24, 2025
2 checks passed
@VisargD VisargD deleted the enhancement/portkey-pii-tracing branch June 24, 2025 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants