sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Support passing views to the meter provider created in `StrandsTelemetry.setup_meter()`

Open bbckr opened this issue 1 year ago • 5 comments

Problem Statement

When configuring telemetry and setting up the meter similar to:

from strands.telemetry import StrandsTelemetry
strands_telemetry = StrandsTelemetry()
strands_telemetry.setup_otlp_exporter()
strands_telemetry.setup_meter(enable_otlp_exporter=True)

I want to be able to pass views to StrandsTelemetry.setup_meter(), as this cannot be configured unless I explicitly set strands_telemetry.meter_provider myself similar to how StrandsTelemetry.setup_meter already does it.

Proposed Solution

I propose StrandsTelemetry.setup_meter() includes an optional argument for views, so that the metric provider configures the views alongside the readers its already configuring.

from strands.telemetry import StrandsTelemetry

views = [
  View(instrument_name="*", aggregation=DropAggregation()),
  View(instrument_name="mycounter"),
]

strands_telemetry = StrandsTelemetry()
strands_telemetry.setup_otlp_exporter()
strands_telemetry.setup_meter(enable_otlp_exporter=True, views=views) # the proposed change

Use Case

I need to filter out certain tags we do not need (like tool_use_id and event_loop_cycle_id) that are high-cardinality and would like to increased custom metric costs with vendors like Datadog.

Alternatives Solutions

No response

Additional Context

Let me know what you all think and (if needed) I can open the PR myself for the change.

bbckr avatar Sep 11 '25 13:09 bbckr

Wondering also if exposing the resource used by the MetricsProvider would be helpful for changing the service name tag that gets submitted by the MetricsProvider, or would setting the open telemetry headers for service.name via the env var be able to handle this? Since we would also want to update the service.name as well.

This is what we are doing now given the limitation:

def configure_otel() -> None:
...
    strands_telemetry = StrandsTelemetry(tracer_provider=datadog_tracer_provider)
    strands_telemetry.setup_otlp_exporter()

    # taken from StrandsTelemetry.setup_meter
    def setup_meter() -> None:
        """
        Setup the meter provider for the strands telemetry. We avoid using strands_telemetry.setup_meter because we need to override the views.
        """
        import opentelemetry.metrics as metrics_api
        from opentelemetry.exporter.otlp.proto.http.metric_exporter import (
            OTLPMetricExporter,
        )
        from opentelemetry.sdk.metrics import MeterProvider
        from opentelemetry.sdk.metrics.export import (
            PeriodicExportingMetricReader,
        )
        from opentelemetry.sdk.metrics.view import View
        from opentelemetry.sdk.resources import Resource

        resource = Resource(attributes={"service.name": settings.DD_SERVICE})

        # Configure with the OTLP exporter we've already set up
        metrics_readers = [
            PeriodicExportingMetricReader(OTLPMetricExporter()),
        ]

        # Filter out the tags we don't want to send, like tool_use_id and event_loop_cycle_id
        views = [
            View(
                instrument_name="strands.event_loop.*",
                attribute_keys=set([]),
            ),
            View(
                instrument_name="strands.tool.*",
                attribute_keys=set(["tool_name"]),
            ),
        ]
        meter_provider = MeterProvider(resource=resource, metric_readers=metrics_readers, views=views)
        strands_telemetry.meter_provider = meter_provider

        # Set the meter provider globally
        metrics_api.set_meter_provider(meter_provider)

    setup_meter()

bbckr avatar Sep 15 '25 10:09 bbckr

any update on the implementation? I would also need an option in order to reduce some metrics I am not interested.

tb102122 avatar Nov 27 '25 06:11 tb102122

Hi, thanks for raising this! Glad you found a workaround. Given that you already have it, it seems getting this merged would not be too difficult. I am tagging @lizradway and @poshinchen for their input on the design side.

After they give a thumbs-up we'll mark this as ready-for-contribution. Thanks!

dbschmigelski avatar Jan 27 '26 17:01 dbschmigelski

I see that there are more kwargs that you could pass to the meter_provider. Probably you make them as provider_kwargs?

I would keep the resources and reader as is, but others can be pass as kwargs

poshinchen avatar Jan 27 '26 17:01 poshinchen

Hi, would like to take this on since it looks like @bbckr never got around to opening the PR. Will follow the design @poshinchen suggested above.

aribault avatar May 25 '26 20:05 aribault