Skip to content

feat(recipes): add observability recipes for logging and metrics#36

Open
dishant-gupta-glean wants to merge 1 commit into
feature/v0-workstreamfrom
recipes/observability
Open

feat(recipes): add observability recipes for logging and metrics#36
dishant-gupta-glean wants to merge 1 commit into
feature/v0-workstreamfrom
recipes/observability

Conversation

@dishant-gupta-glean

@dishant-gupta-glean dishant-gupta-glean commented Jun 5, 2026

Copy link
Copy Markdown

Summary

Add comprehensive observability recipes on top of pull-layer-recipes-pr1 showing all logging, metrics, and cloud provider integration capabilities from PRs #32, #33, #34, #37, #38, and #39.

Depends on: pull-layer-recipes-pr1

Recipe Files

Core Observability

1. logging_setup.py - Logging Configuration

  • setup_basic_logging() - Human-readable console logging
  • setup_structured_logging() - JSON structured logging for production
  • setup_compact_structured_logging() - Compact JSON (omits empty fields)
  • setup_custom_format_logging() - Custom log format strings
  • setup_multi_handler_logging() - Console + file logging
  • setup_custom_structured_fields() - Extra fields in every log

2. lifecycle_events.py - Crawl Phase Tracking

  • track_basic_execution() - Start/end execution
  • track_execution_with_context() - With run_id/datasource/crawl_mode
  • track_failed_execution() - Error handling
  • track_data_fetch_phase() - Data fetching from source
  • track_transform_phase() - Data transformation
  • track_batch_upload_phase() - Batch uploads to Glean
  • track_full_crawl_lifecycle() - Complete crawl example

3. metrics_providers.py - Custom Provider Implementations

  • PrometheusMetricsProvider - prometheus_client integration
  • DataDogMetricsProvider - datadog library integration
  • CloudWatchMetricsProvider - boto3 CloudWatch integration
  • use_in_memory_metrics() - Default provider example

4. decorators.py - Automatic Logging Decorators

  • @with_observability() - Auto-log all public methods
  • @with_observability(exclude_methods=[...]) - Exclude specific methods
  • @with_observability(include_args=True, include_return=True) - Verbose logging
  • @track_crawl_progress - Auto-track item counts

5. performance_tracking.py - Context Managers

  • PerformanceTracker - Track operation duration
  • PerformanceTracker with observability integration
  • ProgressCallback - Track progress with known/unknown totals
  • Nested operation tracking
  • Error handling in tracked operations

6. metrics_recording.py - Built-in Metrics

  • record_upload_metrics() - Batch size and throughput
  • record_api_metrics() - Latency, count, errors
  • record_retry_metrics() - Retry attempts
  • record_crawl_outcome_metrics() - Success/failure
  • record_custom_metrics() - Custom counters and timers
  • record_with_labels() - Metrics with custom labels

Cloud Provider Integration

7. cloud_providers_aws.py - AWS CloudWatch Integration (5 examples)

  • use_cloudwatch_metrics() - CloudWatch Metrics only
  • use_cloudwatch_logs() - CloudWatch Logs only
  • use_cloudwatch_full_observability() - Metrics + logs together
  • use_cloudwatch_with_custom_buffer() - High-volume metrics buffering
  • use_cloudwatch_error_tracking() - Error monitoring and tracking

Installation: uv add glean-indexing-sdk[aws]

8. cloud_providers_gcp.py - GCP Cloud Logging/Monitoring (6 examples)

  • use_cloud_monitoring() - Cloud Monitoring only
  • use_cloud_logging() - Cloud Logging only
  • use_gcp_full_observability() - Metrics + logs with k8s resources
  • use_cloud_monitoring_with_custom_buffer() - High-volume metrics buffering
  • use_gcp_error_tracking() - Error monitoring with generic_task resources
  • use_gce_instance_monitoring() - GCE instance-specific monitoring

Installation: uv add glean-indexing-sdk[gcp]

Recipe Pattern

All recipes follow the established pattern:

  • Clean code without explanatory comments
  • Self-documenting through clear function/class names
  • Working examples that can be copy-pasted
  • Module docstrings for context
  • Runnable if __name__ == "__main__" sections

Coverage

Covers all observability features from:

Example Usage

AWS CloudWatch

from glean.indexing.observability import ConnectorObservability, setup_connector_logging
from glean.indexing.plugins.aws import CloudWatchMetricsProvider, CloudWatchLogsProvider

metrics = CloudWatchMetricsProvider(namespace="GleanConnectors", region_name="us-west-2")
logs = CloudWatchLogsProvider(log_group="/glean/connectors", log_stream="my-connector")

setup_connector_logging("my_connector", logger_provider=logs, use_structured_logging=True)
obs = ConnectorObservability("my_connector", metrics_provider=metrics)

obs.start_execution()
obs.record_upload_batch_size(100)
obs.end_execution()

GCP Cloud Logging/Monitoring

from glean.indexing.observability import ConnectorObservability, setup_connector_logging
from glean.indexing.plugins.gcp import CloudMonitoringProvider, CloudLoggingProvider

metrics = CloudMonitoringProvider(project_id="my-project")
logs = CloudLoggingProvider(project_id="my-project", log_name="my-connector")

setup_connector_logging("my_connector", logger_provider=logs, use_structured_logging=True)
obs = ConnectorObservability("my_connector", metrics_provider=metrics)

obs.start_execution()
obs.record_upload_batch_size(100)
obs.end_execution()

@dishant-gupta-glean dishant-gupta-glean requested a review from a team as a code owner June 5, 2026 07:49
@dishant-gupta-glean

Copy link
Copy Markdown
Author

Cloud Provider Recipes Added

Added AWS and GCP cloud provider integration recipes on top of existing observability recipes:

cloud_providers_aws.py (5 examples)

  • CloudWatch Metrics only
  • CloudWatch Logs only
  • Full CloudWatch observability (metrics + logs)
  • Custom buffer size for high-volume metrics
  • Error tracking and monitoring

cloud_providers_gcp.py (6 examples)

  • Cloud Monitoring only
  • Cloud Logging only
  • Full GCP observability (metrics + logs)
  • Custom buffer size for high-volume metrics
  • Error tracking and monitoring
  • GCE instance-specific monitoring

These recipes depend on PRs:

All examples are runnable with __main__ sections.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR expands the SDK’s observability surface area by adding structured JSON logging, lifecycle event logging, a pluggable metrics provider interface, and AWS/GCP plugin providers, along with a full set of “recipes” and unit tests to demonstrate and validate usage.

Changes:

  • Added structured JSON logging formatters and enhanced setup_connector_logging() to support structured logging, custom formatters, extra handlers, and pluggable LoggerProviders.
  • Introduced lifecycle event logging and a MetricsProvider interface with an in-memory default provider, plus AWS CloudWatch and GCP Cloud Monitoring/Logging provider implementations.
  • Added a comprehensive set of recipes/observability/* examples and corresponding unit tests for observability, metrics, and cloud plugins.

Reviewed changes

Copilot reviewed 36 out of 36 changed files in this pull request and generated 34 comments.

Show a summary per file
File Description
tests/unit_tests/plugins/gcp/test_cloud_monitoring.py Unit tests for GCP Cloud Monitoring metrics provider
tests/unit_tests/plugins/gcp/test_cloud_logging.py Unit tests for GCP Cloud Logging provider
tests/unit_tests/plugins/gcp/init.py Test package marker for GCP plugins
tests/unit_tests/plugins/aws/test_cloudwatch_metrics.py Unit tests for AWS CloudWatch metrics provider
tests/unit_tests/plugins/aws/test_cloudwatch_logs.py Unit tests for AWS CloudWatch logs provider
tests/unit_tests/plugins/aws/init.py Test package marker for AWS plugins
tests/unit_tests/plugins/init.py Test package marker for plugins tests
tests/unit_tests/observability/test_setup_logging.py Tests for setup_connector_logging() incl. provider integration
tests/unit_tests/observability/test_providers.py Tests for MetricsProvider and InMemoryMetricsProvider
tests/unit_tests/observability/test_metrics_integration.py Integration tests for metrics recording via ConnectorObservability
tests/unit_tests/observability/test_logger_provider.py Tests for LoggerProvider and ConsoleLoggerProvider
tests/unit_tests/observability/test_lifecycle_logging.py Tests for lifecycle event logging behavior/fields
tests/unit_tests/observability/test_formatters.py Tests for StructuredFormatter and CompactStructuredFormatter
tests/unit_tests/observability/init.py Test package marker for observability tests
src/glean/indexing/plugins/gcp/cloud_monitoring.py GCP Cloud Monitoring MetricsProvider implementation
src/glean/indexing/plugins/gcp/cloud_logging.py GCP Cloud Logging LoggerProvider implementation
src/glean/indexing/plugins/gcp/init.py GCP plugin exports + optional dependency warning wrapper
src/glean/indexing/plugins/aws/cloudwatch_metrics.py AWS CloudWatch MetricsProvider implementation
src/glean/indexing/plugins/aws/cloudwatch_logs.py AWS CloudWatch Logs LoggerProvider implementation
src/glean/indexing/plugins/aws/init.py AWS plugin exports + optional dependency warning wrapper
src/glean/indexing/plugins/init.py Plugins package marker/docstring
src/glean/indexing/observability/providers.py MetricType, MetricsProvider ABC, and in-memory provider
src/glean/indexing/observability/observability.py ConnectorObservability lifecycle logging + metrics provider integration
src/glean/indexing/observability/logging.py LoggerProvider ABC + ConsoleLoggerProvider
src/glean/indexing/observability/formatters.py Structured JSON logging formatters
src/glean/indexing/observability/init.py Public exports for new observability APIs
recipes/observability/performance_tracking.py Performance tracker and progress callback recipes
recipes/observability/metrics_recording.py Built-in metrics recording recipes
recipes/observability/metrics_providers.py Custom metrics provider recipes (Prometheus/DataDog/CloudWatch)
recipes/observability/logging_setup.py Logging configuration recipes
recipes/observability/lifecycle_events.py Crawl lifecycle logging recipes
recipes/observability/decorators.py Decorator-based observability recipes
recipes/observability/cloud_providers_gcp.py GCP integration recipes
recipes/observability/cloud_providers_aws.py AWS integration recipes
recipes/observability/init.py Recipes package marker/docstring
pyproject.toml Adds [aws] and [gcp] optional dependency groups

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +36 to +37
obs.log_batch_upload_started(batch_index=0, batch_size=100)
obs.log_batch_upload_completed(batch_index=0)
Comment on lines +99 to +101
obs.log_batch_upload_started(batch_index=0, batch_size=50)
obs.record_batch_upload_latency(350.0)
obs.log_batch_upload_completed(batch_index=0)
Comment on lines +156 to +159
except Exception as e:
obs.fail_execution(e)

obs.record_crawl_outcome("failed", error_message="API rate limit exceeded")
Comment on lines +36 to +37
obs.log_batch_upload_started(batch_index=0, batch_size=100)
obs.log_batch_upload_completed(batch_index=0)
Comment on lines +107 to +109
obs.log_batch_upload_started(batch_index=0, batch_size=50)
obs.record_batch_upload_latency(350.0)
obs.log_batch_upload_completed(batch_index=0)
Comment on lines +1 to +3
"""GCP Cloud Logging and Monitoring integration recipes."""

import logging
region_name: AWS region name
create_log_group: Whether to create log group if it doesn't exist
"""
import watchtower

from unittest.mock import MagicMock, patch

from glean.indexing.observability import MetricType
Comment on lines +3 to +5
try:
from glean.indexing.plugins.aws.cloudwatch_logs import CloudWatchLogsProvider
from glean.indexing.plugins.aws.cloudwatch_metrics import CloudWatchMetricsProvider
Comment on lines +3 to +5
try:
from glean.indexing.plugins.gcp.cloud_logging import CloudLoggingProvider
from glean.indexing.plugins.gcp.cloud_monitoring import CloudMonitoringProvider
Base automatically changed from pull-layer-recipes-pr1 to feature/v0-workstream June 9, 2026 04:21
@@ -0,0 +1,177 @@
"""AWS CloudWatch integration recipes."""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are few lint kind of issues present which are followed in the repo and also enforced by the ruff commands. Can you please resolve these?
Bot has commented regarding a few of these below

self.status_code = response.status_code if response is not None else None


class PullHttpClient:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dishant-gupta-glean is this rebased properly?

"""Generic source-side HTTP client for connector data clients."""

import logging
import random

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase issue?

@dishant-gupta-glean dishant-gupta-glean Jun 17, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting to get all observability PRs merged then will rebase recipes all toughter

Add example recipes for using the observability SDK components:
- Logging setup (structured, plain, custom level)
- Metrics recording patterns
- Lifecycle event patterns
- Decorator patterns for timing/tracking
- Performance tracking examples
- Cloud provider recipes (AWS CloudWatch + GCP Cloud Logging/Monitoring)
- Pull HTTP client recipe

Also add recipes/__init__.py and tests/unit_tests/recipes/__init__.py.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

4 participants