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

Use protobuf reflection for loading runtime configuration #2025

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

Molter73
Copy link
Collaborator

@Molter73 Molter73 commented Feb 3, 2025

Description

This change is based upon a toy project I have at https://github.com/Molter73/config-much.

The basic idea is to use the introspection and reflection APIs from protobuf to populate the runtime configuration structures, which should allow us to add new fields to our protobuf definitions and have them magically show up in our code to use.

This is an alternative implementation to #1993.

Checklist

  • Investigated and inspected CI test results
  • Updated documentation accordingly

Automated testing

  • Added unit tests
  • Added integration tests
  • Added regression tests

If any of these don't apply, please comment below.

Testing Performed

CI should be enough to ensure no behavioral changes have been introduced.

This change is based upon a toy project I have at
https://github.com/Molter73/config-much.

The basic idea is to use the reflection API from protobuf to populate
the runtime configuration structures, which should allow us to add new
fields to our protobuf definitions and have them magically show up in
our code to use.
@Molter73 Molter73 added the run-multiarch-builds Run steps for non-x86 archs. label Feb 5, 2025
@Molter73 Molter73 marked this pull request as ready for review February 5, 2025 09:55
@Molter73 Molter73 requested a review from a team as a code owner February 5, 2025 09:55
collector/lib/ConfigLoader.cpp Outdated Show resolved Hide resolved
Comment on lines 380 to 400
case FieldDescriptor::CPPTYPE_INT32:
return ParseArrayInner<int32>(msg, node, field);
case FieldDescriptor::CPPTYPE_UINT32:
return ParseArrayInner<uint32_t>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
return ParseArrayInner<int64_t>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
return ParseArrayInner<uint64_t>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
return ParseArrayInner<double>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
return ParseArrayInner<float>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
return ParseArrayInner<bool>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
return ParseArrayEnum(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
return ParseArrayInner<std::string>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
return {{"Unsupport repeated type MESSAGE"}};
} break;
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit]

Suggested change
case FieldDescriptor::CPPTYPE_INT32:
return ParseArrayInner<int32>(msg, node, field);
case FieldDescriptor::CPPTYPE_UINT32:
return ParseArrayInner<uint32_t>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
return ParseArrayInner<int64_t>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
return ParseArrayInner<uint64_t>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
return ParseArrayInner<double>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
return ParseArrayInner<float>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
return ParseArrayInner<bool>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
return ParseArrayEnum(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
return ParseArrayInner<std::string>(msg, node, field);
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
return {{"Unsupport repeated type MESSAGE"}};
} break;
case FieldDescriptor::CPPTYPE_INT32:
return ParseArrayInner<int32>(msg, node, field);
case FieldDescriptor::CPPTYPE_UINT32:
return ParseArrayInner<uint32_t>(msg, node, field);
case FieldDescriptor::CPPTYPE_INT64:
return ParseArrayInner<int64_t>(msg, node, field);
case FieldDescriptor::CPPTYPE_UINT64:
return ParseArrayInner<uint64_t>(msg, node, field);
case FieldDescriptor::CPPTYPE_DOUBLE:
return ParseArrayInner<double>(msg, node, field);
case FieldDescriptor::CPPTYPE_FLOAT:
return ParseArrayInner<float>(msg, node, field);
case FieldDescriptor::CPPTYPE_BOOL:
return ParseArrayInner<bool>(msg, node, field);
case FieldDescriptor::CPPTYPE_ENUM:
return ParseArrayEnum(msg, node, field);
case FieldDescriptor::CPPTYPE_STRING:
return ParseArrayInner<std::string>(msg, node, field);
case FieldDescriptor::CPPTYPE_MESSAGE: {
return {{"Unsupport repeated type MESSAGE"}};
} break;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm also going to space them out a bit, it's a little hard to read otherwise.

}

config_.SetRuntimeConfig(std::move(runtime_config));
CLOG(DEBUG) << "Runtime configuration:\n"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we set this to INFO? I don't think it should trigger often enough to be spamming the log, and would be invaluable to see how Collector is operating at any given moment

@Molter73 Molter73 requested a review from Stringy February 11, 2025 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-multiarch-builds Run steps for non-x86 archs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants