-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add FromRaw to Flags struct #5959
Conversation
See usages in open-telemetry/opentelemetry-collector-contrib#13617 where we had to check for the sample bit to set the value instead of just setting the entire value. Signed-off-by: Bogdan <[email protected]>
Codecov Report
@@ Coverage Diff @@
## main #5959 +/- ##
=======================================
Coverage 92.30% 92.30%
=======================================
Files 212 212
Lines 13249 13253 +4
=======================================
+ Hits 12229 12233 +4
Misses 803 803
Partials 217 217
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
// FromRaw converts from the OTLP uint32 representation into this LogRecordFlags. | ||
func (ms LogRecordFlags) FromRaw(val uint32) { | ||
*ms.getOrig() = val | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of having both IsSampled and FromRaw I think a better way would be to do this:
- Declare new type LogRecordFlags.
- Add IsSampled/SetIsSampled methods to LogRecordFlags.
- Have LogRecord.SetFlags/GetFlags which uses LogRecordFlags.
- Delete IsSampled/SetIsSampled/FromRaw methods from LogRecordFlags
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So to set the flag you need:
flags := logRecord.Flags()
flags.SetIsSampled(true)
logRecord.SetFlags(flags)
This is a bit inconsistent with current pdata similar structs. It is similar with TraceID/SpanID in that regard, but these are immutable types. The rule is that if an object is "mutable" (SetIsSampled) then we don't offer a setter to avoid mistakes when updated but not "re-set".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I believe what I suggest is the right approach is separation of concerns. OTLP is not concerned with "sampled" flag directly. OTLP is concerned that there is a byte-sized W3C flags. That's all OTLP needs to know. The inner structure of W3C byte flag is W3C concern. pdata is our abstracted representation of OTLP and must be concerned with the same concepts as OTLP, not more, not less. In the spirit of this I believe pdata API must expose W3C flags as a byte (aliased as a new type for stronger typing), but not individual bits in the W3C flags.
So, perhaps a slightly different variation of what I suggested which is more consistent with my reasoning above would be to have this:
- Declare new type W3CTraceFlags (an alias of
byte
). - Add IsSampled/SetIsSampled methods to W3CTraceFlags.
- Have LogRecord.SetTraceFlags/GetTraceFlags which uses W3CTraceFlags.
- Delete IsSampled/SetIsSampled/FromRaw methods from LogRecordFlags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pdata is our abstracted representation of OTLP and must be concerned with the same concepts as OTLP
This is where I disagree, pdata is our collector data model, which is based on "OTLP" but is not "OTLP". It is the collector's data model. The structs in ptraceotlp
are related to OTLP (protocol), the others are collector's data model, indeed based on OTLP
.
So, perhaps a slightly different variation of what I suggested which is more consistent with my reasoning above would be to have this:
Even this has the downside I mentioned, this does not address that, on how to change the "Sampled" flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An option is to make the Flags immutable and have a "WithIsSampled" which returns a new Flags struct, so then we are consistent that we have setters only for immutable types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where I disagree, pdata is our collector data model, which is based on "OTLP" but is not "OTLP". It is the collector's data model.
Indeed. If that's the way we see it then we are free to deviate from OTLP's understanding. However, do we? What do we gain by that deviation?
On the contrary, there is value in saying "pdata is an efficient in-memory representation and API to work with OTLP data". I envision it being a separate Otel library, not even necessarily part of Collector. Any Go codebase that needs to work with OTLP data can use such a library. Think for example Schema transformation. This is now developed as a new processor in Collector, however backends also need similar functionality. If pdata becomes an independent Otel library, then Schema transformer can also be an independent Otel library that depends on pdata, but is not in the Collector codebase. Then in the Collector codebase we can have just the schematransform processor which uses both libraries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is fine, still I believe that we should respect the rule that we "Set" only immutable types, so we cannot have SetIsSampled
, we need to return a new type to keep the logic that all types that we offer setters are immutable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it matters for non-reference types (like the proposed W3CTraceFlags). The rule that types with setters need to be immutable is only important for reference types so that you can't use the mutability to modify the value without explicitly calling the setter. For non-reference types this is impossible, so not an issue.
Anyway, I am fine either way, this is a minor issue, works either way for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tigrannajaryan please review #5972 (I closed #5970 since is way more complicated).
See usages in open-telemetry/opentelemetry-collector-contrib#13617 where we had to check for the sample bit to set the value instead of just setting the entire value.
Signed-off-by: Bogdan [email protected]