Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dependencies/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ add_subdirectory(backward-cpp)
add_subdirectory(fpm)
add_subdirectory(parallel-hashmap)
add_subdirectory(libvault)
add_subdirectory(opentelemetry-cpp)


include(${CMAKE_SOURCE_DIR}/CMakeModule/SuppressHeaderWarning.cmake)
Expand Down
29 changes: 29 additions & 0 deletions dependencies/cmake/opentelemetry-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
include(FetchContent)

FetchContent_Declare(
opentelemetry-cpp
GIT_REPOSITORY https://github.com/open-telemetry/opentelemetry-cpp.git
GIT_TAG v1.16.1
)

set(WITH_OTLP_GRPC OFF CACHE BOOL "" FORCE)
set(WITH_OTLP_HTTP OFF CACHE BOOL "" FORCE)
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
set(WITH_EXAMPLES OFF CACHE BOOL "" FORCE)
set(WITH_BENCHMARK OFF CACHE BOOL "" FORCE)
set(OPENTELEMETRY_INSTALL OFF CACHE BOOL "" FORCE)
set(WITH_STL ON CACHE BOOL "" FORCE)
Copy link
Collaborator

Choose a reason for hiding this comment

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

STL可以用CXX23,可以看下他们的这些选项的支持

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK


FetchContent_MakeAvailable(opentelemetry-cpp)

if(TARGET opentelemetry_trace)
add_library(opentelemetry-cpp::trace ALIAS opentelemetry_trace)
endif()

if(TARGET opentelemetry_exporter_ostream_span)
add_library(opentelemetry-cpp::ostream_span_exporter ALIAS opentelemetry_exporter_ostream_span)
endif()

if(TARGET opentelemetry_resources)
add_library(opentelemetry-cpp::resources ALIAS opentelemetry_resources)
endif()
1 change: 1 addition & 0 deletions src/CraneCtld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ target_link_libraries(cranectld PRIVATE

Utility_PublicHeader
Utility_PluginClient
crane_tracer
uvw

cxxopts
Expand Down
16 changes: 16 additions & 0 deletions src/CraneCtld/CraneCtld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "TaskScheduler.h"
#include "crane/Network.h"
#include "crane/PluginClient.h"
#include "crane/TracingMacros.h"

void ParseCtldConfig(const YAML::Node& config) {
using util::YamlValueOr;
Expand Down Expand Up @@ -1043,6 +1044,21 @@ int main(int argc, char** argv) {
CheckSingleton();
InstallStackTraceHooks();

// Test OpenTelemetry tracing
auto& tracer = crane::TracerManager::GetInstance();
std::string trace_file =
"/nfs/home/interntwo/crane/CraneSched/output/cranectld_traces.json";
if (tracer.Initialize(trace_file, "cranectld")) {
CRANE_TRACE_BEGIN("cranectld.startup_test");
CRANE_TRACE_SET_ATTRIBUTE("test.type", "initialization");
CRANE_TRACE_SET_ATTRIBUTE("component", "cranectld");
CRANE_TRACE_ADD_EVENT("test.start");
CRANE_TRACE_ADD_EVENT("test.complete");
CRANE_TRACE_END("OK");
tracer.Shutdown();
CRANE_INFO("OpenTelemetry test trace written to: {}", trace_file);
}

if (g_config.CraneCtldForeground)
StartServer();
else
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory(AnonymousPipe)
add_subdirectory(CriClient)
add_subdirectory(FileLogger)
add_subdirectory(OpenTelemetry)
add_subdirectory(PluginClient)
add_subdirectory(PublicHeader)
22 changes: 22 additions & 0 deletions src/Utilities/OpenTelemetry/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# OpenTelemetry tracing library for CraneSched

add_library(crane_tracer STATIC TracerManager.cpp)

target_include_directories(crane_tracer PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(crane_tracer PUBLIC CRANE_ENABLE_TRACING)

target_link_libraries(
crane_tracer
PUBLIC opentelemetry-cpp::trace
opentelemetry-cpp::ostream_span_exporter
opentelemetry-cpp::resources)

message(STATUS "OpenTelemetry tracing enabled (Debug build) - output to file")
else()
message(STATUS "OpenTelemetry tracing disabled (Release build)")
endif()

install(DIRECTORY include/ DESTINATION include)
129 changes: 129 additions & 0 deletions src/Utilities/OpenTelemetry/TracerManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Copyright (c) 2024 Peking University and Peking University
* Changsha Institute for Computing and Digital Economy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "crane/TracerManager.h"

#ifdef CRANE_ENABLE_TRACING
# include <fstream>

# include "opentelemetry/exporters/ostream/span_exporter_factory.h"
# include "opentelemetry/sdk/resource/resource.h"
# include "opentelemetry/sdk/resource/semantic_conventions.h"
# include "opentelemetry/sdk/trace/batch_span_processor_factory.h"
# include "opentelemetry/sdk/trace/batch_span_processor_options.h"
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
# include "opentelemetry/sdk/trace/tracer_provider.h"
#endif

namespace crane {

namespace _internal {
#ifdef CRANE_ENABLE_TRACING
thread_local opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>
g_current_span;
#endif
} // namespace _internal

TracerManager& TracerManager::GetInstance() {
static TracerManager instance;
return instance;
}

bool TracerManager::Initialize(const std::string& output_file_path,
const std::string& service_name) {
#ifdef CRANE_ENABLE_TRACING
namespace trace_api = opentelemetry::trace;
namespace trace_sdk = opentelemetry::sdk::trace;
namespace resource = opentelemetry::sdk::resource;

service_name_ = service_name;

output_stream_ = std::make_shared<std::ofstream>(
output_file_path, std::ios::out | std::ios::app);
if (!static_cast<std::ofstream*>(output_stream_.get())->is_open()) {
return false;
}

auto exporter =
opentelemetry::exporter::trace::OStreamSpanExporterFactory::Create(
*output_stream_);

auto processor =
trace_sdk::SimpleSpanProcessorFactory::Create(std::move(exporter));

auto resource_attributes = resource::ResourceAttributes{
{resource::SemanticConventions::kServiceName, service_name_}};
auto resource_ptr = resource::Resource::Create(resource_attributes);

auto provider = std::make_shared<trace_sdk::TracerProvider>(
std::move(processor), resource_ptr);
tracer_provider_ = provider;

trace_api::Provider::SetTracerProvider(tracer_provider_);

tracer_ = tracer_provider_->GetTracer(service_name_);

initialized_ = true;
return true;
#else
return false;
#endif
}

void TracerManager::Shutdown() {
#ifdef CRANE_ENABLE_TRACING
if (tracer_provider_) {
static_cast<opentelemetry::sdk::trace::TracerProvider*>(
tracer_provider_.get())
->Shutdown();
}
initialized_ = false;
#endif
}

#ifdef CRANE_ENABLE_TRACING
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>
TracerManager::CreateSpan(const std::string& span_name) {
if (!tracer_) {
return opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>(
nullptr);
}
return tracer_->StartSpan(span_name);
}

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>
TracerManager::CreateChildSpan(
const std::string& span_name,
const opentelemetry::trace::SpanContext& parent_context) {
if (!tracer_) {
return opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span>(
nullptr);
}

opentelemetry::trace::StartSpanOptions options;
options.parent = parent_context;
return tracer_->StartSpan(span_name, options);
}

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer>
TracerManager::GetTracer() {
return tracer_;
}
#endif

} // namespace crane
76 changes: 76 additions & 0 deletions src/Utilities/OpenTelemetry/include/crane/TracerManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2024 Peking University and Peking University
* Changsha Institute for Computing and Digital Economy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>

#ifdef CRANE_ENABLE_TRACING
# include "opentelemetry/sdk/trace/processor.h"
# include "opentelemetry/sdk/trace/simple_processor_factory.h"
# include "opentelemetry/sdk/trace/tracer_provider.h"
# include "opentelemetry/sdk/trace/tracer_provider_factory.h"
# include "opentelemetry/trace/provider.h"
# include "opentelemetry/trace/span.h"
# include "opentelemetry/trace/tracer.h"
#endif

namespace crane {

class TracerManager {
public:
static TracerManager& GetInstance();

bool Initialize(const std::string& output_file_path,
const std::string& service_name);

void Shutdown();

#ifdef CRANE_ENABLE_TRACING
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> CreateSpan(
const std::string& span_name);

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> CreateChildSpan(
const std::string& span_name,
const opentelemetry::trace::SpanContext& parent_context);

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer();
#endif

TracerManager(const TracerManager&) = delete;
TracerManager& operator=(const TracerManager&) = delete;

private:
TracerManager() = default;
~TracerManager() = default;

#ifdef CRANE_ENABLE_TRACING
opentelemetry::nostd::shared_ptr<opentelemetry::trace::TracerProvider>
tracer_provider_;
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> tracer_;
std::shared_ptr<std::ostream> output_stream_;
#endif
std::string service_name_;
bool initialized_ = false;
};

} // namespace crane
Loading