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

Equivalent of opentracing's 'sampling.priority 1' tag? #1920

Closed
VivekSubr opened this issue Jan 12, 2023 · 13 comments
Closed

Equivalent of opentracing's 'sampling.priority 1' tag? #1920

VivekSubr opened this issue Jan 12, 2023 · 13 comments

Comments

@VivekSubr
Copy link

Hello!

Opentracing had a way to force sample a span, by setting 'sampling.priority' tag to 1 we could force sampling of a particular span by a trace ratio sampler.

Looking at this library, there seems to be no equivalent way? A similar discussion in the golang lib suggests using a custom sampler: open-telemetry/opentelemetry-go#2651

Can something like that be done here? Or some other, better way?

@marcalff
Copy link
Member

A SpanContext consist of a trace id, a span id, and trace flags.

See TraceFlags::isSampled().

@VivekSubr
Copy link
Author

@marcalff - but there seems to be no way to set that?

@marcalff
Copy link
Member

@VivekSubr

The trace flags should typically:

  • be set when creating a root span,
  • be inherited from the parent span when creating a child span.

See StartSpanOptions::parent

The SpanContext constructor can be invoked with a given trace flag.

@VivekSubr
Copy link
Author

@marcalff - thanks for the hints, you meant like this?

    auto ctx = trace::SpanContext(true, false);
    ASSERT_TRUE(ctx.trace_flags().IsSampled());
    
    trace::StartSpanOptions options; options.parent = ctx;
    for(int i=0; i<10; i++)
    {
        std::ostringstream s;
        s << "span" << i;
        auto span = m_tracer->StartSpan(s.str(), {}, {}, options);
        ASSERT_TRUE(span->GetContext().trace_flags().IsSampled());
        span->End();
    }

The ASSERT inside the for failed, looks like it won't take the flag from the parent context.

@lalitb
Copy link
Member

lalitb commented Jan 12, 2023

@VivekSubr - Do you want to force sample a Span by override the default behavior of the configured sampler? I think you would need a custom sampler to achieve it.

@VivekSubr
Copy link
Author

@lalitb - are there any examples of how to write custom samplers?

@esigo
Copy link
Member

esigo commented Jan 15, 2023

@VivekSubr
Copy link
Author

Thanks for all the help! I think I'm almost there, just one weird issue with custom sampler - it doesn't seem to receive set attributes? Here's a test case -

TEST_F(TestTracer, Span)
{
    std::unique_ptr<memory::InMemorySpanExporter> exporter(new memory::InMemorySpanExporter());

    auto resource_attributes = opentelemetry::sdk::resource::ResourceAttributes
    {
        {"service.name", "Test"},
        {"service.instance.id", "Test-12"}
    };
    auto resource = opentelemetry::sdk::resource::Resource::Create(resource_attributes);
    
    std::shared_ptr<memory::InMemorySpanData> span_data = exporter->GetData();
    auto sampler = std::unique_ptr<CustomSampler>(new CustomSampler(0.1));
    auto processor = std::unique_ptr<trace_sdk::SpanProcessor>(new trace_sdk::SimpleSpanProcessor(std::move(exporter)));
    std::vector<std::unique_ptr<trace_sdk::SpanProcessor>> v; v.push_back(std::move(processor));
    auto m_tracer_ctx = std::make_shared<trace_sdk::TracerContext>(
                              std::move(v), 
                              resource, 
                              std::move(sampler));
        
    auto m_trace_provider = std::shared_ptr<trace::TracerProvider>(new trace_sdk::TracerProvider(m_tracer_ctx));
    auto m_tracer = m_trace_provider->GetTracer("TestTracer", "1.0.0");

    trace::StartSpanOptions options; 
    for(int i=0; i<10; i++)
    {
        std::ostringstream s;
        s << "span" << i;
        auto span = m_tracer->StartSpan(s.str(), {}, {}, options);
        span->SetAttribute("ForceSampling", true);
        span->End();
    }

    ASSERT_EQ(span_data->GetSpans().size(), 10);
}

And here's my sampler: https://github.com/VivekSubr/Client-Server/blob/main/opentelemetry/jaeger/sampler.h

I can see that attributes recieved by 'ShouldSample' is always size 0, it should have picked by the SetAttribute one right?

@VivekSubr
Copy link
Author

Yeah, since it has be done with startSpan...

https://github.com/open-telemetry/opentelemetry-cpp/blob/59e7496f94a0e1c3f8daa087dd3571c81ad5f92c/sdk/src/trace/tracer.cc

Not really a replacement for the sampling.priorty tag, since that could be done any any time.

@lalitb
Copy link
Member

lalitb commented Jan 31, 2023

Yeah, since it has be done with startSpan...

Yes that's correct, the sampling decision is taken during span creation and the span attributes are not populated at that time.

@lalitb
Copy link
Member

lalitb commented Jan 31, 2023

Making sampling decision through span attribute - this is not possible as of now, and not supported as per the specs.

@lalitb
Copy link
Member

lalitb commented Feb 2, 2023

@VivekSubr - Do you have further comments here, or if we can close this issue as something not supported in specs.

@VivekSubr
Copy link
Author

No, thanks, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants