Skip to main content

Overview

The Secure MCP Gateway implements structured logging with contextual information for comprehensive debugging, auditing, and monitoring. Logs are exported via OpenTelemetry to Loki for aggregation and analysis in Grafana.

Logging Architecture

Log Levels

The gateway supports standard Python logging levels:
Log Level
Detailed diagnostic information for troubleshooting. Use sparingly in production.Examples:
  • Cache lookups
  • Configuration loading
  • Detailed request/response data
Log Level
General operational events. Default level for production.Examples:
  • Tool execution started/completed
  • Authentication success
  • Server discovery
Log Level
Unexpected but handled situations that may require attention.Examples:
  • Cache misses
  • Slow operations (approaching timeout)
  • Deprecated API usage
Log Level
Error conditions that prevented an operation from completing.Examples:
  • Tool execution failures
  • Authentication failures
  • Guardrail API errors
Log Level
Severe errors requiring immediate attention.Examples:
  • Gateway initialization failure
  • Critical system resource exhaustion
  • Security breach attempts

Configuring Log Level

Set the log level in enkrypt_mcp_config.json:
Options: DEBUG, INFO, WARNING, ERROR, CRITICAL

Structured Logging

Lazy Logger Pattern

The gateway uses a lazy logger to avoid circular imports during initialization: Location: src/secure_mcp_gateway/utils.py:63

Using the Logger

Basic Usage:

Log Context Structure

The gateway uses the build_log_extra() function to create structured context: Location: src/secure_mcp_gateway/utils.py:352
Example Usage:

Standard Context Fields

Logs include these standard fields when available:

Log Aggregation with Loki

Loki Configuration

Location: infra/loki/loki-config.yaml

Collector Export to Loki

Location: infra/otel_collector/otel-collector-config.yaml

Accessing Loki

Querying Logs in Grafana

LogQL Basics

Loki uses LogQL for querying logs:

Advanced Queries

Accessing Grafana Explore

  1. Open Grafana: http://localhost:3000
  2. Navigate to Explore (compass icon)
  3. Select Loki datasource
  4. Enter LogQL query
  5. Click “Run query”

Live Tail

View logs in real-time:
  1. Grafana → Explore
  2. Select Loki
  3. Click “Live” button
  4. Enter query: {service_name="secure-mcp-gateway"}
  5. Logs stream in real-time

Log Format Examples

Tool Execution Log

Guardrail Violation Log

Error Log

Logging Best Practices

Use structured logging with contextual fields:
Structured logs enable powerful filtering and analysis.
  • DEBUG: Detailed diagnostic info (cache lookups, config loading)
  • INFO: Normal operations (tool execution, auth success)
  • WARNING: Unexpected but handled (cache miss, slow operation)
  • ERROR: Operation failures (tool error, auth failure)
  • CRITICAL: Severe errors (gateway crash, security breach)
Always mask sensitive information:
The mask_sensitive_data function masks keys like: token, key, secret, password, auth, etc.
Always include custom_id for request tracing:
This enables tracking requests across all logs.
Log important decisions and branches:
Log operation durations:

Log Retention and Management

Retention Configuration

Configure retention in Loki:

Compaction

Loki automatically compacts old chunks to save space. Configure in loki-config.yaml:

Log Volume Management

Reduce log volume:
  1. Increase log level: Use WARNING or ERROR in production
  2. Sample logs: Log only a percentage of requests
  3. Filter before export: Use collector processors to filter low-value logs

Troubleshooting

Logs Not Appearing in Loki

  1. Check Loki is running:
  2. Verify collector exports to Loki:
  3. Check gateway logs are being exported:
  4. Test Loki API:

Logs Not Structured

Symptom: Logs appear as plain text instead of JSON Cause: Not using extra parameter Solution:

High Log Volume

Symptom: Excessive disk usage, slow queries Solutions:
  1. Increase log level to WARNING
  2. Reduce DEBUG logs in production
  3. Configure log sampling
  4. Reduce retention period

Cannot Query by Field

Symptom: LogQL queries by field don’t work Cause: Need to parse JSON Solution:

Next Steps

Metrics

Explore Prometheus metrics and Grafana dashboards

OpenTelemetry Setup

Configure OTLP export and distributed tracing

Overview

Return to observability overview

Troubleshooting

Common issues and solutions