]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #include "opentelemetry/nostd/string_view.h" | |
7 | #include "opentelemetry/nostd/unique_ptr.h" | |
8 | #include "opentelemetry/version.h" | |
9 | ||
10 | #include <functional> | |
11 | #include <string> | |
12 | ||
13 | OPENTELEMETRY_BEGIN_NAMESPACE | |
14 | ||
15 | namespace sdk | |
16 | { | |
17 | namespace instrumentationlibrary | |
18 | { | |
19 | ||
20 | class InstrumentationLibrary | |
21 | { | |
22 | public: | |
23 | InstrumentationLibrary(const InstrumentationLibrary &) = default; | |
24 | ||
25 | /** | |
26 | * Returns a newly created InstrumentationLibrary with the specified library name and version. | |
27 | * @param name name of the instrumentation library. | |
28 | * @param version version of the instrumentation library. | |
29 | * @param schema_url schema url of the telemetry emitted by the library. | |
30 | * @returns the newly created InstrumentationLibrary. | |
31 | */ | |
32 | static nostd::unique_ptr<InstrumentationLibrary> Create(nostd::string_view name, | |
33 | nostd::string_view version = "", | |
34 | nostd::string_view schema_url = "") | |
35 | { | |
36 | return nostd::unique_ptr<InstrumentationLibrary>( | |
37 | new InstrumentationLibrary{name, version, schema_url}); | |
38 | } | |
39 | ||
40 | std::size_t HashCode() const noexcept { return hash_code_; } | |
41 | ||
42 | /** | |
43 | * Compare 2 instrumentation libraries. | |
44 | * @param other the instrumentation library to compare to. | |
45 | * @returns true if the 2 instrumentation libraries are equal, false otherwise. | |
46 | */ | |
47 | bool operator==(const InstrumentationLibrary &other) const | |
48 | { | |
49 | return equal(other.name_, other.version_, other.schema_url_); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Check whether the instrumentation library has given name and version. | |
54 | * This could be used to check version equality and avoid heap allocation. | |
55 | * @param name name of the instrumentation library to compare. | |
56 | * @param version version of the instrumentatoin library to compare. | |
57 | * @param schema_url schema url of the telemetry emitted by the library. | |
58 | * @returns true if name and version in this instrumentation library are equal with the given name | |
59 | * and version. | |
60 | */ | |
61 | bool equal(const nostd::string_view name, | |
62 | const nostd::string_view version, | |
63 | const nostd::string_view schema_url = "") const | |
64 | { | |
65 | return this->name_ == name && this->version_ == version && this->schema_url_ == schema_url; | |
66 | } | |
67 | ||
68 | const std::string &GetName() const { return name_; } | |
69 | const std::string &GetVersion() const { return version_; } | |
70 | const std::string &GetSchemaURL() const { return schema_url_; } | |
71 | ||
72 | private: | |
73 | InstrumentationLibrary(nostd::string_view name, | |
74 | nostd::string_view version, | |
75 | nostd::string_view schema_url = "") | |
76 | : name_(name), version_(version), schema_url_(schema_url) | |
77 | { | |
78 | std::string hash_data; | |
79 | hash_data.reserve(name_.size() + version_.size() + schema_url_.size()); | |
80 | hash_data += name_; | |
81 | hash_data += version_; | |
82 | hash_data += schema_url_; | |
83 | hash_code_ = std::hash<std::string>{}(hash_data); | |
84 | } | |
85 | ||
86 | private: | |
87 | std::string name_; | |
88 | std::string version_; | |
89 | std::string schema_url_; | |
90 | std::size_t hash_code_; | |
91 | }; | |
92 | ||
93 | } // namespace instrumentationlibrary | |
94 | } // namespace sdk | |
95 | ||
96 | OPENTELEMETRY_END_NAMESPACE |