Provides a configuration option for the metrics exporter allowing users to specify mappings for custom MonitoredResource type.
The mapping logic for the custom MR type is activated based on the presence of gcp.resource_type resource attribute in the underlying OpenTelemetry resource.
Some methods were marked with @Deprecated but no breaking changes were made in this PR.
Sample usage with the proposed changes
// MetricConfiguration object for the metrics exporter -
MetricConfiguration configuration =
MetricConfiguration.builder()
.setProjectId(PROJECT_ID)
.setCredentials(CREDENTIALS)
.setResourceAttributesFilter(allowAllPredicate)
.setMonitoredResourceDescription(
new MonitoredResourceDescription(
"custom_mr", Set.of("instance_id", "api", "host_id")))
.setUseServiceTimeSeries(true)
.build();
// If the OTel resource now has the Attribute label `gcp.resource_type` with value
// `custom_mr` the exporter will attempt to map the OTel resource to the set MR type.
Testing
Tested this newly added configuration with the existing metrics example.
Used the configuration to override MR type to dataflow-job.
The program returned valid error when all required labels were not present in the resource attributes.
When all required labels were present, metric was successfully written against the correct MR type.
Configuration code for reference
// MeterProvider and OTel Resource setup
MonitoredResourceDescription monitoredResourceDescription =
new MonitoredResourceDescription(
"dataflow_job", Set.of("job_name", "project_id", "region"));
MetricExporter metricExporter =
GoogleCloudMetricExporter.createWithConfiguration(
MetricConfiguration.builder()
.setMonitoredResourceDescription(monitoredResourceDescription)
.build());
// project_id inferred from env variables
Attributes attrs =
Attributes.builder()
.put("gcp.resource_type", "dataflow_job") // required to trigger platform mapping
.put("network_id", "random-id") // was ignored
.put("job_name", "some-job")
.put("region", "us1")
.build();
METER_PROVIDER =
SdkMeterProvider.builder()
.setResource(Resource.create(attrs))
.registerMetricReader(
PeriodicMetricReader.builder(metricExporter)
.setInterval(java.time.Duration.ofSeconds(30))
.build())
.build();
...
// Standard OpenTelemetry code to record metric
Description
Provides a configuration option for the metrics exporter allowing users to specify mappings for custom MonitoredResource type.
The mapping logic for the custom MR type is activated based on the presence of
gcp.resource_type
resource attribute in the underlying OpenTelemetry resource.Some methods were marked with
@Deprecated
but no breaking changes were made in this PR.Sample usage with the proposed changes
Testing
Configuration code for reference