Skip to content

fix(py/google-cloud): fix metric exporter aggregation, credentials client forwarding, and structlog trace processor - #7097

Closed
huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-gcp-telemetry-exporter
Closed

huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-gcp-telemetry-exporter

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

Problem

  1. Metric exporter aggregation crash: GenkitMetricExporter.__init__ did not call super().__init__(), leaving _preferred_aggregation unset on the instance. PeriodicExportingMetricReader threw AttributeError: 'GenkitMetricExporter' object has no attribute '_preferred_aggregation'.
  2. Credentials forwarding crash: enable_google_cloud_telemetry(credentials=...) forwarded credentials=... directly into GenkitGCPExporter (CloudTraceSpanExporter), CloudMonitoringMetricsExporter, and CloudLoggingExporter. None of these accept a credentials keyword argument, causing TypeError.
  3. Structlog trace context placement: _genkit_inject_trace_context was appended to processors. Because structlog renderers (ConsoleRenderer, JSONRenderer) transform event dictionaries into strings or bytes, processors appended after them run after rendering or miss log records.

Mechanism

  1. Call super().__init__(): Pass preferred_temporality and the underlying exporter's _preferred_aggregation so MetricExporter properly populates both instance attributes.
  2. Client factory via credentials: Construct typed TraceServiceClient, MetricServiceClient, and LoggingServiceV2Client instances with the credentials object when credentials= is supplied (either as a service account dict or a google.auth.credentials.Credentials instance), and pass them via the client= kwarg. If project is not specified but present in the service account dict, resolve project from credentials['project_id'].
  3. Insert before renderers: Inspect existing structlog processors and insert _genkit_inject_trace_context immediately before the first renderer processor so trace context fields (logging.googleapis.com/trace, spanId) are present in event_dict when rendered.

Verification

  • Added 3 unit tests in tests/tracing_test.py covering GenkitMetricExporter aggregation temporality preservation, client creation/forwarding under credentials=, and structlog processor ordering.
  • Ran pytest packages/genkit-google-cloud (225 passed).
  • Verified ruff check, ruff format, and pyright clean.

…ient forwarding, and structlog trace processor

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors telemetry configuration in genkit-google-cloud to properly resolve Google Cloud credentials and instantiate service clients for tracing, logging, and metrics. It also ensures that the structlog trace context processor is correctly positioned before the final renderer, and that the metric exporter preserves aggregation temporality. Feedback suggests improving project ID resolution by using the new _get_google_credentials() helper to extract the project ID from both dictionary and pre-instantiated credentials objects.

@huangjeff5

Copy link
Copy Markdown
Contributor Author

Closing: migrated to standalone repository genkit-ai/genkit-python (PR #38): genkit-ai/genkit-python#38

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant