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

How could i return dynamic attributes in a sampler #2273

Open
xiehuc opened this issue Aug 22, 2023 · 5 comments
Open

How could i return dynamic attributes in a sampler #2273

xiehuc opened this issue Aug 22, 2023 · 5 comments
Assignees
Labels
Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.

Comments

@xiehuc
Copy link

xiehuc commented Aug 22, 2023

Before opening a feature request against this repo, consider whether the feature should/could be implemented in the other OpenTelemetry client libraries. If so, please open an issue on opentelemetry-specification first.

Is your feature request related to a problem?

::opentelemetry::sdk::trace::SamplingResult MySampler::ShouldSample(
    const ::opentelemetry::trace::SpanContext& parent_context,
    ::opentelemetry::trace::TraceId trace_id, ::opentelemetry::nostd::string_view name,
    ::opentelemetry::trace::SpanKind span_kind,
    const ::opentelemetry::common::KeyValueIterable& attributes,
    const ::opentelemetry::trace::SpanContextKeyValueIterable& links) noexcept 

    result.attributes =
        std::make_unique<const std::map<std::string, opentelemetry::common::AttributeValue>>(
            std::move(kv));

SampleingResult has attributes, but it's value is AttributeValue, just reference, not own memory.

so, my question is, where could i attach original value string to this session, so it could free'd after span send.

parent_context?
trace_state?

no way !!! this API just could not make it work but still keeps it exist.

i think about thread_local, object pool, delay timer, std::string allocator, but all of this just make a simple things becomes very complex。

Describe the solution you'd like
no idea, dead lock.

Describe alternatives you've considered

Additional context

@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Aug 22, 2023
@xiehuc xiehuc changed the title How could i return attributes in a sampler How could i return dynamic attributes in a sampler Aug 22, 2023
@lalitb
Copy link
Member

lalitb commented Aug 23, 2023

Valid concern. The SamplingResult::attributes needs to stay valid for the duration of the Tracer::StartSpan() method's execution (not till the span's end). And it is difficult for Sampler implementation to guarantee this as attributes value is AttributeValue which doesn't own memory for non-primitive types. One approach could be to change SamplingResult::attributes to use OwnedAttributeValue instead of AttributeValue, which will give the ownership of attributes to SamplingResult.

struct SamplingResult
{
   ...
   std::unique_ptr<const std::map<std::string, opentelemetry::sdk::common::OwnedAttributeValue>> attributes;
   ...
}

Span::SetAttribute() takes AttributeValue as an argument, so we need conversions here. Need more discussion/thoughts to see if there is a better solution.

@xiehuc
Copy link
Author

xiehuc commented Aug 31, 2023

i rethink the code, found that:

class Span {
  void SetAttribute(nostd::string_view key,
                    const opentelemetry::common::AttributeValue &value) noexcept override;
}

the AttributeValue is string_view, is totally OK.

because Span's underline is sdk/src/trace/span.cc

would call recordable_->SetAttribute(key, value) would call

  auto *attribute = span_.add_attributes();
  OtlpPopulateAttributeUtils::PopulateAttribute(attribute, key, value);

span_ is protobuf, would finally hold value. so string_string is just a bridge.

that is

std::string value("value");
span.SetAttribute("key", value); 

just ok, SetAttribute finally make protobuf hold value.

but in SamplingResult things are totally different.

when a Sampler set attribute

::opentelemetry::sdk::trace::SamplingResult GalileoSampler::ShouldSample(......) noexcept {
  std::string value("value");
  std::map<std::string, opentelemetry::common::AttributeValue> kv_map{{"key", value}};
  result.attributes = std::make_unique(kv_map);
  return result;
}

after ShouldSample return, value would immediately free,so attribute hold a bad value.

on parent function,
image

even if it call SetAttribute after, the value is just a bad value.

@xiehuc
Copy link
Author

xiehuc commented Aug 31, 2023

the solution is simple.

let ShouldSample can call SetAttribute .

  1. put span in ShouldSample Argument
  2. or put a callback in ShouldSample Argument
  3. delete SampleResult attributes. it really misleading people.
  4. SampleResult hold value, do not use opentelemetry::common::AttributeValue type

@lalitb
Copy link
Member

lalitb commented Sep 3, 2023

the solution is simple.

let ShouldSample can call SetAttribute .

Well not that easy :) The Span doesn't exist when ShouldSample is called. As Span is created based on sampling decision.

put span in ShouldSample Argument
or put a callback in ShouldSample Argument

As mentioned above, this needs Span to already exist when ShouldSample is called.

delete SampleResult attributes. it really misleading people.

We could potentially implement this, but it would result in our implementation becoming non-compliant with the specification.

SampleResult hold value, do not use opentelemetry::common::AttributeValue type

Yes, this is one of the solutions, to instead use sdk::common::OwnedAttributeValue. The SDK in principle doesn't do any intermediate storage on span objects and instead serializes eagerly as methods are called. This change will go against this guiding principle.

@marcalff marcalff added triage/accepted Indicates an issue or PR is ready to be actively worked on. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Sep 6, 2023
Copy link

github-actions bot commented Nov 6, 2023

This issue was marked as stale due to lack of activity.

@github-actions github-actions bot added the Stale label Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Stale triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

No branches or pull requests

3 participants