How to register and activate a rust gstreamer tracer plugin in tests?

I am unsure of how to activate a tracer in test code.

I am used to the pattern with plugins:

    fn setup_test() {
        // Initialize GStreamer
        gst::init().expect("Failed to initialize GStreamer");

        mycustomplugin::plugin_register_static().unwrap();

        // ... Can then use this plugin in tests ...

When I try the equivalent with my tracer, it registers the factory, but no tracers are considered as active. I am unsure of how to activate the tracer from here.

    fn setup_test() {
        let tracer_full_name = "mycustomtracer0";
        let tracer_name = "my-tracer";
        env::set_var("GST_TRACERS", tracer_name);
        env::set_var("GST_DEBUG", format!("GST_TRACER:5,{}:6", tracer_name));

        // Initialize GStreamer
        gst::init().expect("Failed to initialize GStreamer");

        gstprometheustracer::plugin_register_static().unwrap();

        // Verify that our element is registered:
        assert!(
            gst::TracerFactory::factories()
                .iter()
                .any(|f| f.name() == tracer_name),
            "Expected to find the {} element after registration",
            tracer_name
        );

        let binding = gst::active_tracers();
        println!("Active tracers: {}", binding.len());
        let _tracer = binding
            .iter()
            .inspect(|t| {
                println!("Active tracer: {}", t.name());
            })
            .find(|t| t.name() == tracer_full_name)
            .expect(format!("Expected to find the `{}` tracer", tracer_name).as_str());
    }

However, if I build the tracer and add it to my path and remove the static registration, the above works.

Is a way to activate a tracer after I have registered it?

From what I can tell, tracers are instantiated using gst_tracer_utils_create_tracer(), which is called by _priv_gst_tracing_init() which is called by init_post().

So when you register your tracer, the above has already been executed. I don’t think there is a workaround for this with current API because gst_tracer_utils_create_tracer() is not publicly exposed.

1 Like