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

Add SpanId class. #10

Merged
merged 12 commits into from
Jan 16, 2020
Merged

Add SpanId class. #10

merged 12 commits into from
Jan 16, 2020

Conversation

g-easy
Copy link
Contributor

@g-easy g-easy commented Oct 25, 2019

No description provided.

WORKSPACE Outdated Show resolved Hide resolved
examples/helloworld/BUILD Outdated Show resolved Hide resolved
class SpanId {
public:
// The size in bytes of the SpanId.
static constexpr size_t kSize = 8;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious:

  1. Do we need this to be public?
  2. Which naming style are we following?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it doesn't have to be public, but my reasoning is that span_id is a rigidly defined part of the otel data model and having a size constant is useful.

There would be e.g. SpanId::kSize and also TraceId::kSize

@@ -0,0 +1,29 @@
// Copyright 2019, OpenTelemetry Authors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re. file naming convention - if the only class in that module is SpanId , should we name the file SpanId[.cpp|.hpp] or SpanId[.cc|.hh] ? .cpp looks a bit better to my eye, others may have a different opinion :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep it as-is (api/include/opentelemetry/trace/span_id.h) to match e.g. api/include/opentelemetry/nostd/string_view.h

std::string ToHex() const;

private:
uint8_t rep_[kSize];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we're using an array of 8-bit numbers to represent this type instead of a uint64_t?

A char array like this will have a byte-level alignment (e.g. alignof(SpanId) == 1)) and will be less performant for doing basic operations like equality check, assignment, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid implying that this is an integer, and thereby avoid endianness issues. SpanId is an opaque 8-byte binary blob.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been discussed in the protocol repository, fwiw: open-telemetry/opentelemetry-proto#52

(It looks like we'll end up on using a byte array in the protocol, which doesn't preclude using 64-bit integers here.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we defer this as an implementation detail, or do we treat alignment as part of the ABI, and therefore have to set it in stone?

std::string ToHex() const;

private:
uint8_t rep_[kSize];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been discussed in the protocol repository, fwiw: open-telemetry/opentelemetry-proto#52

(It looks like we'll end up on using a byte array in the protocol, which doesn't preclude using 64-bit integers here.)

@g-easy
Copy link
Contributor Author

g-easy commented Dec 19, 2019

Folks, I've respun this PR on top of #13 which added the start of the bazel build system.

This has the start of a span_id class, and an example program that shows how to consume the //api target. No work on //sdk yet.

Please take a fresh look at the changes, especially the BUILD parts and the directory structures.

@@ -1,3 +1,17 @@
# Copyright 2019, OpenTelemetry Authors
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone please remind me: do we need the license comment on basically every file?

{
constexpr char kHex[] = "0123456789ABCDEF";
std::string s(kSize * 2, ' ');
for (int i = 0; i < kSize; ++i)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not comfortable with non-trivial implementations in headers. Is this okay?

I don't know what the alternative is, if we stick to the requirement that the api is strictly header-only.

opentelemetry::trace::SpanId* id = GetGlobalTracer::MakeSpanId(args...); seems a bit extreme.

Can we make basic parts of the data model (eg SpanId, TraceId, Attribute) have concrete implementations in the API that can't be overridden by e.g. a dynamically loaded tracer at runtime.

@g-easy
Copy link
Contributor Author

g-easy commented Jan 7, 2020

Friendly ping.

@g-easy g-easy changed the title [WIP] Initial sketch of bazel build system. Add SpanId class. Jan 15, 2020
@g-easy g-easy merged commit 5241561 into open-telemetry:master Jan 16, 2020
@g-easy g-easy deleted the bazel branch January 16, 2020 03:11
@g-easy
Copy link
Contributor Author

g-easy commented Jan 16, 2020

FYI:

void f(const opentelemetry::trace::SpanId& span, opentelemetry::nostd::span<uint8_t, 8> out) {
  span.CopyBytesTo(out);
}

bazel build -c opt --save_temps

_Z1fRKN13opentelemetry5trace6SpanIdENS_5nostd4spanIhLm8EEE:
        movq    (%rdi), %rax
        movq    %rax, (%rsi)
        ret

@rnburn
Copy link
Contributor

rnburn commented Jan 16, 2020

If you pass SpanId by value does it still use a register?

@g-easy
Copy link
Contributor Author

g-easy commented Jan 16, 2020

No:

_Z1gN13opentelemetry5trace6SpanIdENS_5nostd4spanIhLm8EEE:
        movq    %rdi, (%rsi)
        ret

Maybe we should add a comment encouraging users to pass span ids as values.

@rnburn
Copy link
Contributor

rnburn commented Jan 16, 2020

If you keep the same approach but use an int64 (you can still cast it back to a char[]), it should pass in a register like a regular integer argument.

@g-easy
Copy link
Contributor Author

g-easy commented Jan 16, 2020

Sorry, I miscommunicated. When passing spanid by value, uint8_t rep_[kSize] does become a register. I think the optimization you're going for does already happen.

@rnburn
Copy link
Contributor

rnburn commented Jan 16, 2020

Ok, so %rdi is storing the TraceId value and %rsi is storing the pointer from the span -- yeah that looks good.

@g-easy
Copy link
Contributor Author

g-easy commented Jan 16, 2020

This is my understanding, yes. :)

@g-easy
Copy link
Contributor Author

g-easy commented Jan 16, 2020

Also in #10 (comment) I was trying to show that the memcpy compiles to two MOV instructions on Intel, as discussed during the weekly meeting.

0x4b pushed a commit to 0x4b/opentelemetry-cpp that referenced this pull request Jul 31, 2020
GerHobbelt pushed a commit to GerHobbelt/opentelemetry-cpp that referenced this pull request Jun 21, 2024
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

Successfully merging this pull request may close these issues.

5 participants