Skip to content
Closed
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
4 changes: 2 additions & 2 deletions src/bk_download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ bool BkDownload::download_blob() {
return true;
}

void bk_download_proc(std::list<BKDL::BkDownload *> &dl_list, uint64_t delay_sec, int &running) {
void bk_download_proc(std::list<BKDL::BkDownload *> &dl_list, uint64_t delay_sec, int &running, opentelemetry::trace::SpanContext linkContext) {
auto tracer = overlaybd_otel::GetTracer();
auto span = tracer->StartSpan("background_download_process");
span->AddLink(linkContext, {});
auto scope = tracer->WithActiveSpan(span);

span->SetAttribute("delay_seconds", delay_sec);
span->SetAttribute("initial_queue_size", dl_list.size());

Expand Down
2 changes: 1 addition & 1 deletion src/bk_download.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ class BkDownload {
bool force_download = false;
};

void bk_download_proc(std::list<BKDL::BkDownload *> &, uint64_t, int &);
void bk_download_proc(std::list<BKDL::BkDownload *> &, uint64_t, int &, opentelemetry::trace::SpanContext);

} // namespace BKDL
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct ImageConfig : public ConfigUtils::Config {
APPCFG_PARA(download, DownloadConfig);
APPCFG_PARA(accelerationLayer, bool, false);
APPCFG_PARA(recordTracePath, std::string, "");
APPCFG_PARA(traceContext, ConfigUtils::Document);
};

struct P2PConfig : public ConfigUtils::Config {
Expand Down
14 changes: 13 additions & 1 deletion src/image_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "switch_file.h"
#include "overlaybd/gzip/gz.h"
#include "overlaybd/gzindex/gzfile.h"
#include "overlaybd/otel/tracer_common.h"
#include "overlaybd/tar/tar_file.h"

#define PARALLEL_LOAD_INDEX 32
Expand Down Expand Up @@ -217,8 +218,10 @@ void ImageFile::start_bk_dl_thread() {
uint64_t delay_sec = (rand() % extra_range) + conf.download().delay();
LOG_INFO("background download is enabled, delay `, maxMBps `, tryCnt `, blockSize `", delay_sec,
conf.download().maxMBps(), conf.download().tryCnt(), conf.download().blockSize());
opentelemetry::trace::SpanContext linkContext = overlaybd_otel::GetTracer()->GetCurrentSpan()->GetContext();
dl_thread_jh = photon::thread_enable_join(
photon::thread_create11(&BKDL::bk_download_proc, dl_list, delay_sec, m_status));
photon::thread_create11(
BKDL::bk_download_proc, dl_list, delay_sec, m_status, std::move(linkContext)));
}

struct ParallelOpenTask {
Expand Down Expand Up @@ -437,6 +440,15 @@ LSMT::IFileRW *ImageFile::open_upper(ImageConfigNS::UpperConfig &upper) {
}

int ImageFile::init_image_file() {
// Extracts the trace context from the image config as a parent context.
// This allows us to link activity associated with overlaybd-snapshotter.
auto parent_context = overlaybd_otel::ExtractTraceContext(conf.traceContext());
auto tracer = overlaybd_otel::GetTracer();
opentelemetry::trace::StartSpanOptions opts;
opts.parent = parent_context;
auto span = tracer->StartSpan("ImageFile::init_image_file", opts);
auto scope = tracer->WithActiveSpan(span);

LSMT::IFileRO *lower_file = nullptr;
LSMT::IFileRW *upper_file = nullptr;
LSMT::IFileRW *stack_ret = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/overlaybd/otel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set_target_properties(otel_lib PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(otel_lib PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${PHOTON_INCLUDE_DIR}
${rapidjson_SOURCE_DIR}/include
${opentelemetry-cpp_SOURCE_DIR}/api/include
${opentelemetry-cpp_SOURCE_DIR}/sdk/include
${opentelemetry-cpp_SOURCE_DIR}/exporters/otlp/include
Expand Down
46 changes: 42 additions & 4 deletions src/overlaybd/otel/tracer_common.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include "opentelemetry/context/runtime_context.h"
#include "tracer_common.h"

#include "context_storage.h"
#include "opentelemetry/context/runtime_context.h"

using opentelemetry::nostd::unique_ptr;
using opentelemetry::nostd::shared_ptr;
using opentelemetry::nostd::string_view;
using opentelemetry::nostd::function_ref;

namespace overlaybd_otel {

void InitTracer() {
// Install a libphoton coroutine-aware context storage implementation, which uses libphoton's thread-local implementation.
opentelemetry::context::RuntimeContext::SetRuntimeContextStorage(
opentelemetry::nostd::unique_ptr<opentelemetry::context::RuntimeContextStorage>(
unique_ptr<opentelemetry::context::RuntimeContextStorage>(
new LibPhotonContextStorage()));

// Create OTLP/HTTP exporter using the factory
Expand All @@ -26,12 +32,44 @@ void InitTracer() {
}

void CleanupTracer() {
std::shared_ptr<opentelemetry::trace::TracerProvider> none;
opentelemetry::sdk::trace::Provider::SetTracerProvider(none);
opentelemetry::sdk::trace::Provider::SetTracerProvider({});
}

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(opentelemetry::nostd::string_view tracer_name) {
return opentelemetry::trace::Provider::GetTracerProvider()->GetTracer(tracer_name);
}

class JsonTextMapCarrier : public opentelemetry::context::propagation::TextMapCarrier {
public:
JsonTextMapCarrier(const rapidjson::Document& traceContext): m_traceContext(traceContext) {}

string_view Get(string_view key) const noexcept override {
for (auto iter = m_traceContext.MemberBegin(); iter != m_traceContext.MemberEnd(); ++iter) {
if (key == iter->name.GetString() && iter->value.IsString()) {
return iter->value.GetString();
}
}
return {};
}

void Set(string_view key, string_view value) noexcept override {
// not implemented
}

private:
const rapidjson::Document& m_traceContext;
};

opentelemetry::context::Context ExtractTraceContext(const rapidjson::Document& document) {
if (!document.IsObject()) {
return {};
}

// Extract the traceContext if it exists.
opentelemetry::context::Context context;
auto carrier = JsonTextMapCarrier(document);
auto propagator = opentelemetry::trace::propagation::HttpTraceContext();
return propagator.Extract(carrier, context);
}

} // namespace overlaybd_otel
13 changes: 9 additions & 4 deletions src/overlaybd/otel/tracer_common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#define OPENTELEMETRY_ABI_VERSION_NO 2

#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
#include "opentelemetry/exporters/otlp/otlp_http_exporter_options.h"
#include "opentelemetry/nostd/shared_ptr.h"
Expand All @@ -20,11 +20,16 @@
#include <iostream>
#include <vector>

namespace overlaybd_otel {
#include <rapidjson/document.h>

namespace overlaybd_otel {

void InitTracer();

void CleanupTracer();

opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(opentelemetry::nostd::string_view tracer_name = "overlaybd");

opentelemetry::context::Context ExtractTraceContext(const rapidjson::Document& config);

} // namespace overlaybd_otel
Loading