Repository navigation
fix(py/google-cloud): export metrics, honor credentials=, and add trace IDs to structlog logs - #62
Merged
Merged
Conversation
…ient forwarding, and structlog trace processor
…ock/credentials objects in resolve_project
- structlog: insert the trace processor before the last processor, not before the first class named *Renderer. stdlib's ProcessorFormatter.wrap_for_formatter is a function, so the class-name scan appended after it and trace fields were still dropped. - metrics: keep UpDownCounter and ObservableUpDownCounter CUMULATIVE. Cloud Monitoring writes non-monotonic sums as GAUGE points; as DELTA a queue going 5 -> 6 charted as 5 then 1. - credentials: parse the dict once in initialize() and share one _build_exporter_kwargs(client_cls) across trace, metrics, and logs. Drop the Credentials-object branches in resolve_project and initialize(); credentials= is dict-only, so they were unreachable. - tests: real PeriodicExportingMetricReader and CloudMonitoringMetricsExporter instead of hasattr checks; clear GOOGLE_CLOUD_PROJECT in the credentials test so it does not depend on the developer's shell.
A credentials= dict that google-auth can't load used to log a warning and
fall back to ADC, so telemetry exported under a different identity and
project than the one passed. enable() now raises
GenkitError(INVALID_ARGUMENT) naming the missing fields, before the
once-per-process latch, so a fixed call can be retried. The message never
echoes values, and the google-auth/cryptography error is dropped from the
chain since the dict holds a private key. {} now raises too instead of
meaning ADC.
huangjeff5
force-pushed
the
fix/py-gcp-telemetry-exporter
branch
from
November 10, 2026 22:14
f50d21c to
2ab7f89
Compare
…ials, and structlog re-init
huangjeff5
enabled auto-merge (squash)
November 11, 2026 16:38
This was referenced Nov 15, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
enable_google_cloud_telemetry()had three bugs. Each one was caught by atry/exceptduring setup and logged as a warning, so the app kept running and the missing telemetry was easy to miss.How broken, by scenario:
credentials={...}. With ADC (nocredentials=), traces and logs already exported. With a service-account dict, trace export failed to start, and the OTel log exporter ignored the dict and used ADC.structlogtrace correlation: broken for anystructlogconfig that ends in a renderer, which includes structlog's default config. Log lines never carried the Cloud Logging trace and span IDs, so Cloud Logging could not link them to traces.Changes
GenkitMetricExporternever calledsuper().__init__(), so OpenTelemetry'sPeriodicExportingMetricReaderraisedAttributeErrorwhen it was built and metrics were never exported. It now initializes its base class. Up/down counters stay cumulative, since Cloud Monitoring writes them as current-value gauge points (as deltas, a queue going 5 -> 6 charted as 5 then 1). A flush shorter than 1ms no longer sends anend_timebeforestart_time.credentials={...}reaches all three exporters: The Cloud Trace, Monitoring, and Logging exporters take a pre-built GCPclient=, not acredentials=dict. The dict is now loaded once into service-account credentials and passed asTraceServiceClient,MetricServiceClient, andLoggingServiceV2Clientinstances.credentials=raises instead of falling back to ADC: Before, a dict google-auth couldn't load logged a warning and telemetry went out under the machine's identity and project. Nowenable_google_cloud_telemetry()raisesGenkitError(INVALID_ARGUMENT)naming only the missing fields, never values, since the dict holds a private key. It raises before the once-per-process guard, so a corrected call can be retried.credentials={}also raises now; before, it silently meant ADC.structlogrenderer: The trace processor was appended after the renderer, which had already turned the event into a string, so the fields were dropped. It now goes right before the last processor.Testing
Unit tests in
tracing_test.pybuild a real OpenTelemetryPeriodicExportingMetricReaderandCloudMonitoringMetricsExporterand mock only the GCP API client.Verified live against a GCP test project,
mainvs this PR, with local wheels in fresh venvs:mainlogs'GenkitMetricExporter' object has no attribute '_preferred_aggregation'and writes 0 series. This PR writes the probe counter with the expected values, and onegenerateproducesgen_ai.client.operation.durationandgen_ai.client.token.usage, which had never appeared in the project before.generate, and model spans arrive.credentials=:mainraisesTypeErrorin the trace and metric exporters and the log exporter silently uses ADC. This PR builds all three clients from the given service-account dict; with an unregistered key every API returns 401 and nothing falls back to ADC. Not yet run with a real service-account key.JSONRenderer, log lines now carrylogging.googleapis.com/traceandspanId, and they match the flow span in Cloud Trace.Breaking changes
None.