Skip to content

Commit 25ffa24

Browse files
committed
[CONFIGURATION] File configuration - yaml parser
1 parent 0bbbdb1 commit 25ffa24

File tree

12 files changed

+1378
-0
lines changed

12 files changed

+1378
-0
lines changed

sdk/include/opentelemetry/sdk/configuration/configuration.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
* Yaml node OpenTelemetryConfiguration,
3535
* in file
3636
* https://github.com/open-telemetry/opentelemetry-configuration/blob/main/schema/opentelemetry_configuration.json
37+
*
38+
* Every property in the yaml schema is already documented in the
39+
* opentelemetry-configuration repository,
40+
* in file schema/type_descriptions.yaml, see
41+
* https://github.com/open-telemetry/opentelemetry-configuration/blob/main/schema/type_descriptions.yaml
42+
*
43+
* As a result, C++ class members representing yaml properties are not
44+
* commented with details, refer to the source of truth in
45+
* type_descriptions.yaml directly.
3746
*/
3847

3948
OPENTELEMETRY_BEGIN_NAMESPACE
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
8+
#include "opentelemetry/sdk/configuration/configuration.h"
9+
#include "opentelemetry/sdk/configuration/document_node.h"
10+
#include "opentelemetry/version.h"
11+
12+
OPENTELEMETRY_BEGIN_NAMESPACE
13+
namespace sdk
14+
{
15+
namespace configuration
16+
{
17+
18+
class ConfigurationParser
19+
{
20+
public:
21+
static std::unique_ptr<Configuration> Parse(std::unique_ptr<Document> doc);
22+
};
23+
24+
} // namespace configuration
25+
} // namespace sdk
26+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
#include <string>
8+
9+
#include "opentelemetry/sdk/configuration/document_node.h"
10+
#include "opentelemetry/version.h"
11+
12+
OPENTELEMETRY_BEGIN_NAMESPACE
13+
namespace sdk
14+
{
15+
namespace configuration
16+
{
17+
18+
class Document
19+
{
20+
public:
21+
Document() = default;
22+
Document(Document &&) = default;
23+
Document(const Document &) = default;
24+
Document &operator=(Document &&) = default;
25+
Document &operator=(const Document &other) = default;
26+
virtual ~Document() = default;
27+
28+
virtual std::unique_ptr<DocumentNode> GetRootNode() = 0;
29+
};
30+
31+
} // namespace configuration
32+
} // namespace sdk
33+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
#include <string>
8+
9+
#include "opentelemetry/version.h"
10+
11+
OPENTELEMETRY_BEGIN_NAMESPACE
12+
namespace sdk
13+
{
14+
namespace configuration
15+
{
16+
17+
class DocumentNodeConstIterator;
18+
class PropertiesNodeConstIterator;
19+
20+
class DocumentNode
21+
{
22+
public:
23+
// FIXME: proper sizing
24+
static constexpr std::size_t MAX_NODE_DEPTH = 100;
25+
26+
DocumentNode() = default;
27+
DocumentNode(DocumentNode &&) = default;
28+
DocumentNode(const DocumentNode &) = default;
29+
DocumentNode &operator=(DocumentNode &&) = default;
30+
DocumentNode &operator=(const DocumentNode &other) = default;
31+
virtual ~DocumentNode() = default;
32+
33+
virtual std::string Key() const = 0;
34+
35+
virtual bool AsBoolean() = 0;
36+
virtual std::size_t AsInteger() = 0;
37+
virtual double AsDouble() = 0;
38+
virtual std::string AsString() = 0;
39+
40+
virtual std::unique_ptr<DocumentNode> GetRequiredChildNode(const std::string &name) = 0;
41+
virtual std::unique_ptr<DocumentNode> GetChildNode(const std::string &name) = 0;
42+
43+
virtual bool GetRequiredBoolean(const std::string &name) = 0;
44+
virtual bool GetBoolean(const std::string &name, bool default_value) = 0;
45+
46+
virtual std::size_t GetRequiredInteger(const std::string &name) = 0;
47+
virtual std::size_t GetInteger(const std::string &name, std::size_t default_value) = 0;
48+
49+
virtual double GetRequiredDouble(const std::string &name) = 0;
50+
virtual double GetDouble(const std::string &name, double default_value) = 0;
51+
52+
virtual std::string GetRequiredString(const std::string &name) = 0;
53+
virtual std::string GetString(const std::string &name, const std::string &default_value) = 0;
54+
55+
virtual DocumentNodeConstIterator begin() const = 0;
56+
virtual DocumentNodeConstIterator end() const = 0;
57+
58+
virtual std::size_t num_children() const = 0;
59+
virtual std::unique_ptr<DocumentNode> GetChild(std::size_t index) const = 0;
60+
61+
virtual PropertiesNodeConstIterator begin_properties() const = 0;
62+
virtual PropertiesNodeConstIterator end_properties() const = 0;
63+
64+
protected:
65+
std::string DoSubstitution(const std::string &text);
66+
std::string DoOneSubstitution(const std::string &text);
67+
68+
bool BooleanFromString(const std::string &value);
69+
std::size_t IntegerFromString(const std::string &value);
70+
double DoubleFromString(const std::string &value);
71+
};
72+
73+
class DocumentNodeConstIteratorImpl
74+
{
75+
public:
76+
DocumentNodeConstIteratorImpl() = default;
77+
DocumentNodeConstIteratorImpl(DocumentNodeConstIteratorImpl &&) = default;
78+
DocumentNodeConstIteratorImpl(const DocumentNodeConstIteratorImpl &) = default;
79+
DocumentNodeConstIteratorImpl &operator=(DocumentNodeConstIteratorImpl &&) = default;
80+
DocumentNodeConstIteratorImpl &operator=(const DocumentNodeConstIteratorImpl &other) = default;
81+
virtual ~DocumentNodeConstIteratorImpl() = default;
82+
83+
virtual void Next() = 0;
84+
virtual std::unique_ptr<DocumentNode> Item() const = 0;
85+
virtual bool Equal(const DocumentNodeConstIteratorImpl *rhs) const = 0;
86+
};
87+
88+
class PropertiesNodeConstIteratorImpl
89+
{
90+
public:
91+
PropertiesNodeConstIteratorImpl() = default;
92+
PropertiesNodeConstIteratorImpl(PropertiesNodeConstIteratorImpl &&) = default;
93+
PropertiesNodeConstIteratorImpl(const PropertiesNodeConstIteratorImpl &) = default;
94+
PropertiesNodeConstIteratorImpl &operator=(PropertiesNodeConstIteratorImpl &&) = default;
95+
PropertiesNodeConstIteratorImpl &operator=(const PropertiesNodeConstIteratorImpl &other) =
96+
default;
97+
virtual ~PropertiesNodeConstIteratorImpl() = default;
98+
99+
virtual void Next() = 0;
100+
virtual std::string Name() const = 0;
101+
virtual std::unique_ptr<DocumentNode> Value() const = 0;
102+
virtual bool Equal(const PropertiesNodeConstIteratorImpl *rhs) const = 0;
103+
};
104+
105+
class DocumentNodeConstIterator
106+
{
107+
public:
108+
DocumentNodeConstIterator(DocumentNodeConstIteratorImpl *impl) : impl_(impl) {}
109+
DocumentNodeConstIterator(DocumentNodeConstIterator &&) = default;
110+
DocumentNodeConstIterator(const DocumentNodeConstIterator &) = default;
111+
DocumentNodeConstIterator &operator=(DocumentNodeConstIterator &&) = default;
112+
DocumentNodeConstIterator &operator=(const DocumentNodeConstIterator &other) = default;
113+
114+
~DocumentNodeConstIterator() { delete impl_; }
115+
116+
bool operator==(const DocumentNodeConstIterator &rhs) const { return (impl_->Equal(rhs.impl_)); }
117+
118+
bool operator!=(const DocumentNodeConstIterator &rhs) const { return (!impl_->Equal(rhs.impl_)); }
119+
120+
std::unique_ptr<DocumentNode> operator*() const { return impl_->Item(); }
121+
122+
DocumentNodeConstIterator &operator++()
123+
{
124+
impl_->Next();
125+
return *this;
126+
}
127+
128+
private:
129+
DocumentNodeConstIteratorImpl *impl_;
130+
};
131+
132+
class PropertiesNodeConstIterator
133+
{
134+
public:
135+
PropertiesNodeConstIterator(PropertiesNodeConstIteratorImpl *impl) : impl_(impl) {}
136+
PropertiesNodeConstIterator(PropertiesNodeConstIterator &&) = default;
137+
PropertiesNodeConstIterator(const PropertiesNodeConstIterator &) = default;
138+
PropertiesNodeConstIterator &operator=(PropertiesNodeConstIterator &&) = default;
139+
PropertiesNodeConstIterator &operator=(const PropertiesNodeConstIterator &other) = default;
140+
~PropertiesNodeConstIterator() { delete impl_; }
141+
142+
bool operator==(const PropertiesNodeConstIterator &rhs) const
143+
{
144+
return (impl_->Equal(rhs.impl_));
145+
}
146+
147+
bool operator!=(const PropertiesNodeConstIterator &rhs) const
148+
{
149+
return (!impl_->Equal(rhs.impl_));
150+
}
151+
152+
std::string Name() const { return impl_->Name(); }
153+
std::unique_ptr<DocumentNode> Value() const { return impl_->Value(); }
154+
155+
PropertiesNodeConstIterator &operator++()
156+
{
157+
impl_->Next();
158+
return *this;
159+
}
160+
161+
private:
162+
PropertiesNodeConstIteratorImpl *impl_;
163+
};
164+
165+
} // namespace configuration
166+
} // namespace sdk
167+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <stdexcept>
7+
#include <string>
8+
9+
#include "opentelemetry/sdk/configuration/document.h"
10+
#include "opentelemetry/sdk/configuration/document_node.h"
11+
#include "opentelemetry/version.h"
12+
13+
OPENTELEMETRY_BEGIN_NAMESPACE
14+
namespace sdk
15+
{
16+
namespace configuration
17+
{
18+
19+
class InvalidSchemaException : public std::runtime_error
20+
{
21+
public:
22+
InvalidSchemaException(const std::string &msg) : std::runtime_error(msg) {}
23+
InvalidSchemaException(InvalidSchemaException &&) = default;
24+
InvalidSchemaException(const InvalidSchemaException &) = default;
25+
InvalidSchemaException &operator=(InvalidSchemaException &&) = default;
26+
InvalidSchemaException &operator=(const InvalidSchemaException &other) = default;
27+
~InvalidSchemaException() override = default;
28+
};
29+
30+
} // namespace configuration
31+
} // namespace sdk
32+
OPENTELEMETRY_END_NAMESPACE
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#pragma once
5+
6+
#include <memory>
7+
#include <ryml.hpp>
8+
#include <string>
9+
10+
#include "opentelemetry/sdk/configuration/document.h"
11+
#include "opentelemetry/sdk/configuration/document_node.h"
12+
#include "opentelemetry/version.h"
13+
14+
OPENTELEMETRY_BEGIN_NAMESPACE
15+
namespace sdk
16+
{
17+
namespace configuration
18+
{
19+
20+
class RymlDocument : public Document
21+
{
22+
public:
23+
static std::unique_ptr<Document> Parse(const std::string &source, const std::string &content);
24+
25+
RymlDocument(ryml::Tree tree) : tree_(std::move(tree)) {}
26+
RymlDocument(RymlDocument &&) = delete;
27+
RymlDocument(const RymlDocument &) = delete;
28+
RymlDocument &operator=(RymlDocument &&) = delete;
29+
RymlDocument &operator=(const RymlDocument &other) = delete;
30+
~RymlDocument() override = default;
31+
32+
std::unique_ptr<DocumentNode> GetRootNode() override;
33+
34+
private:
35+
ryml::Tree tree_;
36+
};
37+
38+
} // namespace configuration
39+
} // namespace sdk
40+
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)