-
Notifications
You must be signed in to change notification settings - Fork 32
feat:add test mode #782
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
Draft
zhansan114514
wants to merge
4
commits into
PKUHPC:master
Choose a base branch
from
zhansan114514:feat/test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat:add test mode #782
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
||
| 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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
STL可以用CXX23,可以看下他们的这些选项的支持
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK