genkit-plugin-google-cloud 0.3.2__py3-none-any.whl → 0.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,132 @@
15
15
  # SPDX-License-Identifier: Apache-2.0
16
16
 
17
17
 
18
- """Google Cloud Plugin for Genkit."""
18
+ """Google Cloud Plugin for Genkit.
19
+
20
+ This plugin provides Google Cloud observability integration for Genkit,
21
+ enabling telemetry export to Cloud Trace and Cloud Monitoring.
22
+
23
+ Key Concepts (ELI5)::
24
+
25
+ ┌─────────────────────┬────────────────────────────────────────────────────┐
26
+ │ Concept │ ELI5 Explanation │
27
+ ├─────────────────────┼────────────────────────────────────────────────────┤
28
+ │ Telemetry │ Data about how your app is running. Like a │
29
+ │ │ fitness tracker for your code. │
30
+ ├─────────────────────┼────────────────────────────────────────────────────┤
31
+ │ Cloud Trace │ Shows the path requests take through your app. │
32
+ │ │ Like GPS tracking for your API calls. │
33
+ ├─────────────────────┼────────────────────────────────────────────────────┤
34
+ │ Cloud Monitoring │ Graphs and alerts for your app's health. │
35
+ │ │ Like a heart rate monitor dashboard. │
36
+ ├─────────────────────┼────────────────────────────────────────────────────┤
37
+ │ Span │ One step in a request's journey. Like one │
38
+ │ │ leg of a relay race. │
39
+ ├─────────────────────┼────────────────────────────────────────────────────┤
40
+ │ Trace │ All spans for one request connected together. │
41
+ │ │ The complete story of one API call. │
42
+ ├─────────────────────┼────────────────────────────────────────────────────┤
43
+ │ Metrics │ Numbers that describe your app (requests/sec, │
44
+ │ │ error rate, latency). Like a report card. │
45
+ ├─────────────────────┼────────────────────────────────────────────────────┤
46
+ │ PII Redaction │ Hiding sensitive data in traces. Like blurring │
47
+ │ │ faces in photos before sharing. │
48
+ └─────────────────────┴────────────────────────────────────────────────────┘
49
+
50
+ Data Flow::
51
+
52
+ ┌─────────────────────────────────────────────────────────────────────────┐
53
+ │ HOW TELEMETRY FLOWS TO GOOGLE CLOUD │
54
+ │ │
55
+ │ Your Genkit App │
56
+ │ │ │
57
+ │ │ (1) App runs flows, calls models, uses tools │
58
+ │ ▼ │
59
+ │ ┌─────────────────┐ │
60
+ │ │ OpenTelemetry │ Automatically creates spans for each │
61
+ │ │ SDK │ operation (you don't write this code!) │
62
+ │ └────────┬────────┘ │
63
+ │ │ │
64
+ │ │ (2) Spans collected and processed │
65
+ │ ▼ │
66
+ │ ┌─────────────────┐ │
67
+ │ │ GCP Exporters │ • Redact PII (input/output) │
68
+ │ │ │ • Add error markers │
69
+ │ │ │ • Batch for efficiency │
70
+ │ └────────┬────────┘ │
71
+ │ │ │
72
+ │ │ (3) HTTPS to Google Cloud │
73
+ │ ▼ │
74
+ │ ════════════════════════════════════════════════════ │
75
+ │ │ Internet │
76
+ │ ▼ │
77
+ │ ┌─────────────────────────────────────────────────────┐ │
78
+ │ │ Google Cloud Console │ │
79
+ │ │ ┌──────────────┐ ┌──────────────┐ │ │
80
+ │ │ │ Cloud Trace │ │ Cloud │ │ │
81
+ │ │ │ (waterfall │ │ Monitoring │ │ │
82
+ │ │ │ diagrams) │ │ (dashboards) │ │ │
83
+ │ │ └──────────────┘ └──────────────┘ │ │
84
+ │ └─────────────────────────────────────────────────────┘ │
85
+ └─────────────────────────────────────────────────────────────────────────┘
86
+
87
+ Architecture Overview::
88
+
89
+ ┌─────────────────────────────────────────────────────────────────────────┐
90
+ │ Google Cloud Plugin │
91
+ ├─────────────────────────────────────────────────────────────────────────┤
92
+ │ Plugin Entry Point (__init__.py) │
93
+ │ └── add_gcp_telemetry() - Enable Cloud Trace/Monitoring export │
94
+ ├─────────────────────────────────────────────────────────────────────────┤
95
+ │ telemetry/__init__.py - Telemetry Module │
96
+ │ └── Re-exports from submodules │
97
+ ├─────────────────────────────────────────────────────────────────────────┤
98
+ │ telemetry/tracing.py - Distributed Tracing │
99
+ │ ├── Cloud Trace exporter configuration │
100
+ │ └── OpenTelemetry integration │
101
+ ├─────────────────────────────────────────────────────────────────────────┤
102
+ │ telemetry/metrics.py - Metrics Collection │
103
+ │ ├── Cloud Monitoring exporter │
104
+ │ └── Custom Genkit metrics │
105
+ ├─────────────────────────────────────────────────────────────────────────┤
106
+ │ telemetry/action.py - Action Instrumentation │
107
+ │ └── Automatic span creation for Genkit actions │
108
+ └─────────────────────────────────────────────────────────────────────────┘
109
+
110
+ ┌─────────────────────────────────────────────────────────────────────────┐
111
+ │ Telemetry Data Flow │
112
+ │ │
113
+ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────────────┐ │
114
+ │ │ Genkit App │───►│ OpenTelemetry│───►│ Google Cloud │ │
115
+ │ │ (actions, │ │ SDK │ │ (Trace, Monitoring) │ │
116
+ │ │ flows) │ └──────────────┘ └──────────────────────┘ │
117
+ │ └──────────────┘ │
118
+ └─────────────────────────────────────────────────────────────────────────┘
119
+
120
+ Example:
121
+ ```python
122
+ from genkit.plugins.google_cloud import add_gcp_telemetry
123
+
124
+ # Enable telemetry export to Google Cloud
125
+ add_gcp_telemetry()
126
+
127
+ # Traces and metrics are now exported to:
128
+ # - Cloud Trace (distributed tracing)
129
+ # - Cloud Monitoring (metrics)
130
+ ```
131
+
132
+ Caveats:
133
+ - Requires Google Cloud credentials (ADC or explicit)
134
+ - Telemetry is disabled by default in development mode (GENKIT_ENV=dev)
135
+ - Requires opentelemetry and google-cloud-* packages
136
+
137
+ See Also:
138
+ - Cloud Trace: https://cloud.google.com/trace
139
+ - Cloud Monitoring: https://cloud.google.com/monitoring
140
+ - Genkit documentation: https://genkit.dev/
141
+ """
142
+
143
+ from .telemetry import add_gcp_telemetry
19
144
 
20
145
 
21
146
  def package_name() -> str:
@@ -27,4 +152,4 @@ def package_name() -> str:
27
152
  return 'genkit.plugins.google_cloud'
28
153
 
29
154
 
30
- __all__ = ['package_name']
155
+ __all__ = ['add_gcp_telemetry', 'package_name']
@@ -0,0 +1,74 @@
1
+ # Copyright 2025 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # SPDX-License-Identifier: Apache-2.0
16
+
17
+ """Google Cloud telemetry integration for Genkit.
18
+
19
+ This package provides telemetry export to Google Cloud's observability suite,
20
+ enabling monitoring and debugging of Genkit applications through Cloud Trace,
21
+ Cloud Monitoring, and Cloud Logging.
22
+
23
+ Module Structure:
24
+ ┌─────────────────────────────────────────────────────────────────────────┐
25
+ │ Module │ Purpose │
26
+ ├─────────────────┼───────────────────────────────────────────────────────┤
27
+ │ tracing.py │ Main entry point, exporters, configuration │
28
+ │ feature.py │ Root span metrics (requests, latency) │
29
+ │ path.py │ Error path tracking and failure metrics │
30
+ │ generate.py │ Model/generate metrics (tokens, latency, media) │
31
+ │ action.py │ Action I/O logging (tools, flows) │
32
+ │ engagement.py │ User feedback and acceptance metrics │
33
+ │ metrics.py │ Metric definitions and lazy initialization │
34
+ │ utils.py │ Shared utilities (truncation, path parsing, logging) │
35
+ └─────────────────┴───────────────────────────────────────────────────────┘
36
+
37
+ Quick Start:
38
+ ```python
39
+ from genkit.plugins.google_cloud import add_gcp_telemetry
40
+
41
+ # Enable telemetry with defaults (PII redaction enabled)
42
+ add_gcp_telemetry()
43
+
44
+ # Or with custom options
45
+ add_gcp_telemetry(
46
+ project_id='my-project',
47
+ log_input_and_output=True, # Disable PII redaction (caution!)
48
+ )
49
+ ```
50
+
51
+ Cross-Language Parity:
52
+ This implementation maintains feature parity with:
53
+ - JavaScript: js/plugins/google-cloud/src/gcpOpenTelemetry.ts
54
+ - Go: go/plugins/googlecloud/ and go/plugins/firebase/telemetry.go
55
+
56
+ See Also:
57
+ - tracing.py module docstring for detailed architecture documentation
58
+
59
+ GCP Documentation:
60
+ Cloud Trace:
61
+ - Overview: https://cloud.google.com/trace/docs
62
+ - IAM Roles: https://cloud.google.com/trace/docs/iam
63
+
64
+ Cloud Monitoring:
65
+ - Overview: https://cloud.google.com/monitoring/docs
66
+ - Quotas & Limits: https://cloud.google.com/monitoring/quotas
67
+
68
+ OpenTelemetry GCP:
69
+ - Python Exporters: https://google-cloud-opentelemetry.readthedocs.io/
70
+ """
71
+
72
+ from .tracing import add_gcp_telemetry
73
+
74
+ __all__ = ['add_gcp_telemetry']
@@ -0,0 +1,124 @@
1
+ # Copyright 2025 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # SPDX-License-Identifier: Apache-2.0
16
+
17
+ """Action telemetry for GCP.
18
+
19
+ This module logs input/output for tool and generate actions,
20
+ matching the JavaScript implementation.
21
+
22
+ Logging:
23
+ When log_input_and_output=True, logs action inputs and outputs to
24
+ Cloud Logging with structured attributes for correlation.
25
+
26
+ Cross-Language Parity:
27
+ - JavaScript: js/plugins/google-cloud/src/telemetry/action.ts
28
+ - Go: go/plugins/googlecloud/action.go
29
+
30
+ See Also:
31
+ - Cloud Logging: https://cloud.google.com/logging/docs
32
+ - Structured Logging: https://cloud.google.com/logging/docs/structured-logging
33
+ """
34
+
35
+ from __future__ import annotations
36
+
37
+ import structlog
38
+ from opentelemetry.sdk.trace import ReadableSpan
39
+
40
+ from .utils import (
41
+ create_common_log_attributes,
42
+ extract_outer_feature_name_from_path,
43
+ to_display_path,
44
+ truncate,
45
+ truncate_path,
46
+ )
47
+
48
+ logger = structlog.get_logger(__name__)
49
+
50
+
51
+ class ActionTelemetry:
52
+ """Telemetry handler for Genkit actions (tools, generate)."""
53
+
54
+ def tick(
55
+ self,
56
+ span: ReadableSpan,
57
+ log_input_and_output: bool,
58
+ project_id: str | None = None,
59
+ ) -> None:
60
+ """Record telemetry for an action span.
61
+
62
+ Only logs input/output if log_input_and_output is True.
63
+
64
+ Args:
65
+ span: The span to record telemetry for.
66
+ log_input_and_output: Whether to log input/output.
67
+ project_id: Optional GCP project ID.
68
+ """
69
+ if not log_input_and_output:
70
+ return
71
+
72
+ attrs = span.attributes or {}
73
+ action_name = str(attrs.get('genkit:name', '')) or '<unknown>'
74
+ subtype = str(attrs.get('genkit:metadata:subtype', ''))
75
+
76
+ # Only log for tools and generate actions
77
+ if subtype != 'tool' and action_name != 'generate':
78
+ return
79
+
80
+ path = str(attrs.get('genkit:path', '')) or '<unknown>'
81
+ input_val = truncate(str(attrs.get('genkit:input', '')))
82
+ output_val = truncate(str(attrs.get('genkit:output', '')))
83
+ session_id = str(attrs.get('genkit:sessionId', '')) or None
84
+ thread_name = str(attrs.get('genkit:threadName', '')) or None
85
+
86
+ feature_name = extract_outer_feature_name_from_path(path)
87
+ if not feature_name or feature_name == '<unknown>':
88
+ feature_name = action_name
89
+
90
+ if input_val:
91
+ self._write_log(span, 'Input', feature_name, path, input_val, project_id, session_id, thread_name)
92
+ if output_val:
93
+ self._write_log(span, 'Output', feature_name, path, output_val, project_id, session_id, thread_name)
94
+
95
+ def _write_log(
96
+ self,
97
+ span: ReadableSpan,
98
+ tag: str,
99
+ feature_name: str,
100
+ qualified_path: str,
101
+ content: str,
102
+ project_id: str | None,
103
+ session_id: str | None,
104
+ thread_name: str | None,
105
+ ) -> None:
106
+ """Write a structured log entry."""
107
+ path = truncate_path(to_display_path(qualified_path))
108
+ metadata = {
109
+ **create_common_log_attributes(span, project_id),
110
+ 'path': path,
111
+ 'qualifiedPath': qualified_path,
112
+ 'featureName': feature_name,
113
+ 'content': content,
114
+ }
115
+ if session_id:
116
+ metadata['sessionId'] = session_id
117
+ if thread_name:
118
+ metadata['threadName'] = thread_name
119
+
120
+ logger.info(f'{tag}[{path}, {feature_name}]', **metadata)
121
+
122
+
123
+ # Singleton instance
124
+ action_telemetry = ActionTelemetry()
@@ -0,0 +1,170 @@
1
+ # Copyright 2025 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # SPDX-License-Identifier: Apache-2.0
16
+
17
+ """Engagement telemetry for GCP.
18
+
19
+ This module tracks user feedback and acceptance metrics,
20
+ matching the JavaScript implementation.
21
+
22
+ Metrics Recorded:
23
+ - genkit/engagement/feedback: Counter for user feedback events
24
+ - genkit/engagement/acceptance: Counter for user acceptance events
25
+
26
+ Cross-Language Parity:
27
+ - JavaScript: js/plugins/google-cloud/src/telemetry/engagement.ts
28
+ - Go: go/plugins/googlecloud/engagement.go
29
+
30
+ See Also:
31
+ - Cloud Monitoring Custom Metrics: https://cloud.google.com/monitoring/custom-metrics
32
+ """
33
+
34
+ from __future__ import annotations
35
+
36
+ import re
37
+ from typing import Any
38
+
39
+ import structlog
40
+ from opentelemetry import metrics
41
+ from opentelemetry.sdk.trace import ReadableSpan
42
+
43
+ from genkit.core import GENKIT_VERSION
44
+
45
+ from .utils import create_common_log_attributes, truncate
46
+
47
+ logger = structlog.get_logger(__name__)
48
+
49
+ # Lazy-initialized metrics
50
+ _feedback_counter: metrics.Counter | None = None
51
+ _acceptance_counter: metrics.Counter | None = None
52
+
53
+
54
+ def _get_feedback_counter() -> metrics.Counter:
55
+ """Get or create the user feedback counter."""
56
+ global _feedback_counter
57
+ if _feedback_counter is None:
58
+ meter = metrics.get_meter('genkit')
59
+ _feedback_counter = meter.create_counter(
60
+ 'genkit/engagement/feedback',
61
+ description='Counts user feedback events.',
62
+ unit='1',
63
+ )
64
+ return _feedback_counter
65
+
66
+
67
+ def _get_acceptance_counter() -> metrics.Counter:
68
+ """Get or create the user acceptance counter."""
69
+ global _acceptance_counter
70
+ if _acceptance_counter is None:
71
+ meter = metrics.get_meter('genkit')
72
+ _acceptance_counter = meter.create_counter(
73
+ 'genkit/engagement/acceptance',
74
+ description='Tracks user acceptance events.',
75
+ unit='1',
76
+ )
77
+ return _acceptance_counter
78
+
79
+
80
+ class EngagementTelemetry:
81
+ """Telemetry handler for user engagement (feedback, acceptance)."""
82
+
83
+ def tick(
84
+ self,
85
+ span: ReadableSpan,
86
+ log_input_and_output: bool,
87
+ project_id: str | None = None,
88
+ ) -> None:
89
+ """Record telemetry for a user engagement span.
90
+
91
+ Args:
92
+ span: The span to record telemetry for.
93
+ log_input_and_output: Whether to log input/output (unused here).
94
+ project_id: Optional GCP project ID.
95
+ """
96
+ attrs: dict[str, Any] = dict(span.attributes) if span.attributes else {}
97
+ subtype = str(attrs.get('genkit:metadata:subtype', ''))
98
+
99
+ if subtype == 'userFeedback':
100
+ self._write_user_feedback(span, attrs, project_id)
101
+ elif subtype == 'userAcceptance':
102
+ self._write_user_acceptance(span, attrs, project_id)
103
+ else:
104
+ logger.warning('Unknown user engagement subtype', subtype=subtype)
105
+
106
+ def _write_user_feedback(
107
+ self,
108
+ span: ReadableSpan,
109
+ attrs: dict[str, Any],
110
+ project_id: str | None,
111
+ ) -> None:
112
+ """Record user feedback metrics and logs."""
113
+ name = self._extract_trace_name(attrs)
114
+ feedback_value = attrs.get('genkit:metadata:feedbackValue')
115
+ text_feedback = attrs.get('genkit:metadata:textFeedback')
116
+
117
+ dimensions = {
118
+ 'name': str(name)[:256],
119
+ 'value': str(feedback_value)[:256] if feedback_value else '',
120
+ 'hasText': str(bool(text_feedback)),
121
+ 'source': 'py',
122
+ 'sourceVersion': GENKIT_VERSION,
123
+ }
124
+ _get_feedback_counter().add(1, dimensions)
125
+
126
+ metadata: dict[str, Any] = {
127
+ **create_common_log_attributes(span, project_id),
128
+ 'feedbackValue': feedback_value,
129
+ }
130
+ if text_feedback:
131
+ metadata['textFeedback'] = truncate(str(text_feedback))
132
+
133
+ logger.info(f'UserFeedback[{name}]', **metadata)
134
+
135
+ def _write_user_acceptance(
136
+ self,
137
+ span: ReadableSpan,
138
+ attrs: dict[str, Any],
139
+ project_id: str | None,
140
+ ) -> None:
141
+ """Record user acceptance metrics and logs."""
142
+ name = self._extract_trace_name(attrs)
143
+ acceptance_value = attrs.get('genkit:metadata:acceptanceValue')
144
+
145
+ dimensions = {
146
+ 'name': str(name)[:256],
147
+ 'value': str(acceptance_value)[:256] if acceptance_value else '',
148
+ 'source': 'py',
149
+ 'sourceVersion': GENKIT_VERSION,
150
+ }
151
+ _get_acceptance_counter().add(1, dimensions)
152
+
153
+ metadata = {
154
+ **create_common_log_attributes(span, project_id),
155
+ 'acceptanceValue': acceptance_value,
156
+ }
157
+ logger.info(f'UserAcceptance[{name}]', **metadata)
158
+
159
+ def _extract_trace_name(self, attrs: dict[str, Any]) -> str:
160
+ """Extract the trace name from span attributes."""
161
+ path = str(attrs.get('genkit:path', ''))
162
+ if not path or path == '<unknown>':
163
+ return '<unknown>'
164
+
165
+ match = re.search(r'/{(.+)}', path)
166
+ return match.group(1) if match else '<unknown>'
167
+
168
+
169
+ # Singleton instance
170
+ engagement_telemetry = EngagementTelemetry()