Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support explicit monitored resource for Metrics #299

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class CloudMonitoringMetricsExporter(MetricExporter):
each other.
prefix: the prefix of the metric. It is "workload.googleapis.com" by
default if not specified.
monitored_resource: the GCP monitored resource to use for all metrics.
If not specified, the exporter will attempt to detect the
monitored_resource automatically from the OpenTelemetry resource.
"""

def __init__(
Expand All @@ -103,6 +106,7 @@ def __init__(
client: Optional[MetricServiceClient] = None,
add_unique_identifier: bool = False,
prefix: Optional[str] = "workload.googleapis.com",
monitored_resource: Optional[MonitoredResource] = None,
):
# Default preferred_temporality is all CUMULATIVE so need to customize
super().__init__()
Expand Down Expand Up @@ -133,6 +137,7 @@ def __init__(
self._exporter_start_time_nanos,
) = divmod(time_ns(), NANOS_PER_SECOND)
self._prefix = prefix
self.monitored_resource = monitored_resource

def _batch_write(self, series: List[TimeSeries]) -> None:
"""Cloud Monitoring allows writing up to 200 time series at once
Expand Down Expand Up @@ -301,18 +306,21 @@ def export(
all_series = []

for resource_metric in metrics_data.resource_metrics:
monitored_resource_data = get_monitored_resource(
resource_metric.resource
)
# convert it to proto
monitored_resource = (
MonitoredResource(
type=monitored_resource_data.type,
labels=monitored_resource_data.labels,
monitored_resource = self.monitored_resource
if monitored_resource is None:
# auto-detect the monitored resource
monitored_resource_data = get_monitored_resource(
resource_metric.resource
)
# convert it to proto
monitored_resource = (
MonitoredResource(
type=monitored_resource_data.type,
labels=monitored_resource_data.labels,
)
if monitored_resource_data
else None
)
if monitored_resource_data
else None
)

for scope_metric in resource_metric.scope_metrics:
for metric in scope_metric.metrics:
Expand Down