GoogleCloudPlatform / opentelemetry-operations-java

Apache License 2.0
73 stars 39 forks source link

Add support for custom MR mappings #346

Closed psx95 closed 6 months ago

psx95 commented 6 months ago

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

  // 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

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