Skip to content

Commit

Permalink
Remove protobuf depenency
Browse files Browse the repository at this point in the history
Original motivation for this change was to
remove unnecessary binary dependency on
libatomic. As it appeared source of this dependency
was protobuf. As this quite complex piece of
software was used only to generate simple json
I swiched it to boost::json.
I decided to use boost::json because boost already was
dependency for eBPF discovery.
  • Loading branch information
karczex committed Nov 19, 2024
1 parent 63e58bd commit 7d09c78
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 179 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/third_party/libbpf-boo

add_subdirectory(third_party)
add_subdirectory(libebpfdiscovery)
add_subdirectory(libebpfdiscoveryproto)
add_subdirectory(libebpfdiscoveryshared)
add_subdirectory(libebpfdiscoveryskel)
add_subdirectory(libhttpparser)
Expand Down
1 change: 0 additions & 1 deletion conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[requires]
protobuf/3.21.9
ms-gsl/4.0.0
fmt/10.1.1
spdlog/1.12.0
Expand Down
4 changes: 4 additions & 0 deletions ebpfdiscoverysrv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ add_executable(${TARGET} ${SOURCES})
target_link_libraries(${TARGET} PRIVATE ebpfdiscovery)
target_link_options(${TARGET} PRIVATE "-static-libgcc" "-static-libstdc++")
target_compile_definitions(${TARGET} PUBLIC PROJECT_VERSION="${PROJECT_VERSION}")

if (BUILD_TESTS)
add_test(NAME binary_dependencies COMMAND bash ${TESTS_ROOT_DIR}/binary_dependencies.sh $<TARGET_FILE:${TARGET}>)
endif ()
11 changes: 11 additions & 0 deletions ebpfdiscoverysrv/test/dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

binary_path="${1}"
approved_binary_dependencies=("libelf|libz|libm|libdl|librt|libpthread|libc|ld-linux")

depenencies=$(objdump -p "${binary_path}" 2>/dev/null | grep NEEDED | grep -Ev "${approved_binary_dependencies}")

if [ ! -z "${depenencies}" ]; then
echo "Additional dependencies: ${depenencies}"
exit 1
fi
1 change: 0 additions & 1 deletion libebpfdiscovery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ target_include_directories(${TARGET} PRIVATE src PUBLIC headers)
target_link_libraries(${TARGET}
PUBLIC
libpf-tools
ebpfdiscoveryproto
ebpfdiscoveryshared
ebpfdiscoveryskel
httpparser
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
/*
* Copyright 2023 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "ebpfdiscoveryproto/service.pb.h"
#include "service/Service.h"

#include <vector>

namespace proto {

ServicesList internalToProto(const std::vector<std::reference_wrapper<service::Service>>& services);

std::string protoToJson(const ServicesList& protoServices);

} // namespace proto
/*
* Copyright 2023 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <boost/json.hpp>
#include <boost/describe.hpp>
#include <string_view>

namespace boost::json {
template<typename T>
void tag_invoke(json::value_from_tag, json::value& v, std::reference_wrapper<T> const& wrapped) {
v = json::value_from(wrapped.get());
}
} // namespace boost::json

template<typename T>
boost::json::object toJson(std::string_view key, T convertibleValue) {
boost::json::object outJson{};
outJson[key] = boost::json::value_from(convertibleValue);
return outJson;
}
8 changes: 4 additions & 4 deletions libebpfdiscovery/src/Discovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "ebpfdiscovery/Discovery.h"

#include "ebpfdiscovery/Session.h"
#include "ebpfdiscoveryproto/Translator.h"
#include "ebpfdiscovery/Json.h"
#include "logging/Logger.h"
#include "service/IpAddress.h"

Expand All @@ -32,6 +32,7 @@
#include <string>
#include <string_view>
#include <thread>
#include <iostream>

namespace ebpfdiscovery {

Expand Down Expand Up @@ -62,9 +63,8 @@ void Discovery::outputServicesToStdout() {
return;
}

const auto servicesProto{proto::internalToProto(services)};
const auto servicesJson{proto::protoToJson(servicesProto)};
std::cout << servicesJson << std::endl;

std::cout << toJson("services",services) << std::endl;
serviceAggregator.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
* limitations under the License.
*/

#include "ebpfdiscoveryproto/Translator.h"

#include "ebpfdiscovery/Json.h"
#include <gtest/gtest.h>

using namespace proto;
#include <iostream>
#include <string>
#include <vector>

class ProtobufTranslatorTest : public testing::Test {};
class Json : public testing::Test {};

TEST_F(ProtobufTranslatorTest, successfulTranslationToJson) {
TEST_F(Json, servicesToJson) {

std::vector<std::reference_wrapper<service::Service>> internalServices;
service::Service service1{.pid = 1, .endpoint = "/endpoint/1", .internalClientsNumber = 1, .externalClientsNumber = 2};
Expand All @@ -38,13 +39,17 @@ TEST_F(ProtobufTranslatorTest, successfulTranslationToJson) {
internalServices.push_back(service4);
internalServices.push_back(service5);

const auto proto{internalToProto(internalServices)};
const auto result{protoToJson(proto)};
boost::json::object outJson{};
outJson["service"] = boost::json::value_from(internalServices);

std::stringstream buffer;
buffer << outJson;

const std::string expected{"{\"service\":[{\"pid\":1,\"endpoint\":\"/endpoint/"
"1\",\"internalClientsNumber\":1,\"externalClientsNumber\":2},{\"pid\":2,\"endpoint\":\"/endpoint/"
"1\",\"internalClientsNumber\":1,\"externalClientsNumber\":2},{\"pid\":3,\"endpoint\":\"/endpoint/"
"2\",\"internalClientsNumber\":1,\"externalClientsNumber\":2},{\"pid\":4,\"endpoint\":\"google.com/"
"endpoint/3\",\"domain\":\"google.com\",\"scheme\":\"http\",\"internalClientsNumber\":1,\"externalClientsNumber\":2},{\"pid\":5,\"endpoint\":\"dynatrace.com/"
"endpoint/4\",\"domain\":\"dynatrace.com\",\"scheme\":\"https\",\"internalClientsNumber\":1,\"externalClientsNumber\":2}]}"};
EXPECT_EQ(result, expected);
EXPECT_EQ(buffer.str(), expected);
}
53 changes: 0 additions & 53 deletions libebpfdiscoveryproto/CMakeLists.txt

This file was deleted.

30 changes: 0 additions & 30 deletions libebpfdiscoveryproto/ebpfdiscoveryproto/service.proto

This file was deleted.

50 changes: 0 additions & 50 deletions libebpfdiscoveryproto/src/Translator.cpp

This file was deleted.

5 changes: 4 additions & 1 deletion libservice/headers/service/Service.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <cstdint>
#include <string>
#include <boost/describe.hpp>

namespace service {

Expand All @@ -35,4 +36,6 @@ struct Service {
}
};

} // namespace service
BOOST_DESCRIBE_STRUCT(Service, (), (pid, endpoint, domain, scheme, internalClientsNumber, externalClientsNumber))

} // namespace service

0 comments on commit 7d09c78

Please sign in to comment.