|
| 1 | +// Copyright 2020, OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <cstdint> |
| 18 | +#include <cstring> |
| 19 | + |
| 20 | +#include "opentelemetry/nostd/span.h" |
| 21 | + |
| 22 | +namespace opentelemetry |
| 23 | +{ |
| 24 | +namespace trace |
| 25 | +{ |
| 26 | + |
| 27 | +// TraceId represents an opaque 128-bit trace identifier. The trace identifier |
| 28 | +// remains constant across the trace. A valid trace identifier is a 16-byte array with at |
| 29 | +// least one non-zero byte. |
| 30 | +class TraceId final |
| 31 | +{ |
| 32 | +public: |
| 33 | + // The size in bytes of the TraceId. |
| 34 | + static constexpr int kSize = 16; |
| 35 | + |
| 36 | + // An invalid TraceId (all zeros). |
| 37 | + TraceId() noexcept : rep_{0} {} |
| 38 | + |
| 39 | + // Creates a TraceId with the given ID. |
| 40 | + explicit TraceId(nostd::span<const uint8_t, kSize> id) noexcept |
| 41 | + { |
| 42 | + memcpy(rep_, id.data(), kSize); |
| 43 | + } |
| 44 | + |
| 45 | + // Populates the buffer with the lowercase base16 representation of the ID. |
| 46 | + void ToLowerBase16(nostd::span<char, 2 * kSize> buffer) const noexcept |
| 47 | + { |
| 48 | + constexpr char kHex[] = "0123456789ABCDEF"; |
| 49 | + for (int i = 0; i < kSize; ++i) |
| 50 | + { |
| 51 | + buffer[i * 2 + 0] = kHex[(rep_[i] >> 4) & 0xF]; |
| 52 | + buffer[i * 2 + 1] = kHex[(rep_[i] >> 0) & 0xF]; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Returns a nostd::span of the ID. |
| 57 | + nostd::span<const uint8_t, kSize> Id() const noexcept |
| 58 | + { |
| 59 | + return nostd::span<const uint8_t, kSize>(rep_); |
| 60 | + } |
| 61 | + |
| 62 | + bool operator==(const TraceId &that) const noexcept |
| 63 | + { |
| 64 | + return memcmp(rep_, that.rep_, kSize) == 0; |
| 65 | + } |
| 66 | + |
| 67 | + bool operator!=(const TraceId &that) const noexcept { return !(*this == that); } |
| 68 | + |
| 69 | + // Returns false if the TraceId is all zeros. |
| 70 | + bool IsValid() const noexcept { return *this != TraceId(); } |
| 71 | + |
| 72 | + // Copies the opaque TraceId data to dest. |
| 73 | + void CopyBytesTo(nostd::span<uint8_t, kSize> dest) const noexcept |
| 74 | + { |
| 75 | + memcpy(dest.data(), rep_, kSize); |
| 76 | + } |
| 77 | + |
| 78 | +private: |
| 79 | + uint8_t rep_[kSize]; |
| 80 | +}; |
| 81 | + |
| 82 | +} // namespace trace |
| 83 | +} // namespace opentelemetry |
0 commit comments