Skip to content

Commit 786319b

Browse files
authored
Merge pull request #4 from runloopai/install-otel
Add OTEL
2 parents 669224e + e6df604 commit 786319b

7 files changed

Lines changed: 368 additions & 13 deletions

File tree

src/bk_download.cpp

Lines changed: 165 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,17 @@ void BkDownload::switch_to_local_file() {
6262
}
6363

6464
bool BkDownload::download_done() {
65+
auto tracer = overlaybd_otel::get_tracer("overlaybd");
66+
auto parent_span = opentelemetry::trace::Tracer::GetCurrentSpan();
67+
opentelemetry::trace::StartSpanOptions options;
68+
options.parent = parent_span->GetContext();
69+
auto span = tracer->StartSpan("overlaybd.download.verify_and_commit", {}, {}, options);
70+
auto scope = tracer->WithActiveSpan(span);
71+
6572
auto lfs = new_localfs_adaptor();
6673
if (!lfs) {
74+
span->SetAttribute("error", "failed_to_create_fs_adaptor");
75+
span->End();
6776
LOG_ERROR("new_localfs_adaptor() return NULL");
6877
return false;
6978
}
@@ -72,10 +81,18 @@ bool BkDownload::download_done() {
7281
std::string old_name, new_name;
7382
old_name = dir + "/" + DOWNLOAD_TMP_NAME;
7483
new_name = dir + "/" + COMMIT_FILE_NAME;
84+
span->SetAttribute("temp_file", old_name);
85+
span->SetAttribute("commit_file", new_name);
7586

7687
// verify sha256
7788
photon::semaphore done;
7889
std::string shares;
90+
opentelemetry::trace::StartSpanOptions verify_options;
91+
verify_options.parent = opentelemetry::trace::Tracer::GetCurrentSpan()->GetContext();
92+
auto verify_span = tracer->StartSpan("sha256_verification", {}, {}, verify_options);
93+
auto verify_scope = tracer->WithActiveSpan(verify_span);
94+
verify_span->SetAttribute("expected_digest", digest);
95+
7996
std::thread sha256_thread([&]() {
8097
shares = sha256sum(old_name.c_str());
8198
done.signal(1);
@@ -84,33 +101,90 @@ bool BkDownload::download_done() {
84101
// wait verify finish
85102
done.wait(1);
86103

104+
verify_span->SetAttribute("actual_digest", shares);
105+
verify_span->SetAttribute("success", shares == digest);
106+
verify_span->End();
107+
87108
if (shares != digest) {
109+
span->SetAttribute("error", "checksum_mismatch");
110+
span->SetAttribute("expected_digest", digest);
111+
span->SetAttribute("actual_digest", shares);
112+
span->End();
88113
LOG_ERROR("verify checksum ` failed (expect: `, got: `)", old_name, digest, shares);
89114
force_download = true; // force redownload next time
90115
return false;
91116
}
92117

118+
opentelemetry::trace::StartSpanOptions rename_options;
119+
rename_options.parent = opentelemetry::trace::Tracer::GetCurrentSpan()->GetContext();
120+
auto rename_span = tracer->StartSpan("rename_to_commit", {}, {}, rename_options);
121+
auto rename_scope = tracer->WithActiveSpan(rename_span);
93122
int ret = lfs->rename(old_name.c_str(), new_name.c_str());
123+
rename_span->SetAttribute("success", ret == 0);
124+
94125
if (ret != 0) {
126+
span->SetAttribute("error", "rename_failed");
127+
span->End();
95128
LOG_ERRNO_RETURN(0, false, "rename(`,`) failed", old_name, new_name);
96129
}
130+
131+
span->SetAttribute("success", true);
97132
LOG_INFO("download verify done. rename(`,`) success", old_name, new_name);
133+
span->End();
98134
return true;
99135
}
100136

101137
bool BkDownload::download() {
138+
auto tracer = overlaybd_otel::get_tracer("overlaybd");
139+
// Get current span context from parent if it exists
140+
auto parent_span = opentelemetry::trace::Tracer::GetCurrentSpan();
141+
opentelemetry::trace::StartSpanOptions options;
142+
options.parent = parent_span->GetContext();
143+
auto span = tracer->StartSpan("overlaybd.download.lifecycle", {}, {}, options);
144+
auto scope = tracer->WithActiveSpan(span);
145+
146+
span->SetAttribute("url", url);
147+
span->SetAttribute("dir", dir);
148+
span->SetAttribute("file_size", file_size);
149+
102150
if (check_downloaded(dir)) {
151+
opentelemetry::trace::StartSpanOptions local_options;
152+
local_options.parent = opentelemetry::trace::Tracer::GetCurrentSpan()->GetContext();
153+
auto local_span = tracer->StartSpan("overlaybd.download.switch_to_local", {}, {}, local_options);
154+
auto local_scope = tracer->WithActiveSpan(local_span);
103155
switch_to_local_file();
156+
span->SetAttribute("from_cache", true);
157+
span->End();
104158
return true;
105159
}
106160

161+
span->SetAttribute("from_cache", false);
162+
bool success = false;
107163
if (download_blob()) {
108-
if (!download_done())
164+
opentelemetry::trace::StartSpanOptions verify_options;
165+
verify_options.parent = opentelemetry::trace::Tracer::GetCurrentSpan()->GetContext();
166+
auto verify_span = tracer->StartSpan("overlaybd.download.verify", {}, {}, verify_options);
167+
auto verify_scope = tracer->WithActiveSpan(verify_span);
168+
if (!download_done()) {
169+
verify_span->SetAttribute("success", false);
170+
span->SetAttribute("success", false);
171+
span->End();
109172
return false;
173+
}
174+
verify_span->SetAttribute("success", true);
175+
verify_span->End();
176+
177+
opentelemetry::trace::StartSpanOptions switch_options;
178+
switch_options.parent = opentelemetry::trace::Tracer::GetCurrentSpan()->GetContext();
179+
auto switch_span = tracer->StartSpan("overlaybd.download.switch_to_local", {}, {}, switch_options);
180+
auto switch_scope = tracer->WithActiveSpan(switch_span);
110181
switch_to_local_file();
111-
return true;
182+
success = true;
112183
}
113-
return false;
184+
185+
span->SetAttribute("success", success);
186+
span->End();
187+
return success;
114188
}
115189

116190
bool BkDownload::lock_file() {
@@ -127,15 +201,26 @@ void BkDownload::unlock_file() {
127201
}
128202

129203
bool BkDownload::download_blob() {
204+
auto tracer = overlaybd_otel::get_tracer("overlaybd");
205+
auto parent_span = opentelemetry::trace::Tracer::GetCurrentSpan();
206+
opentelemetry::trace::StartSpanOptions options;
207+
options.parent = parent_span->GetContext();
208+
auto span = tracer->StartSpan("overlaybd.download.blob", {}, {}, options);
209+
auto scope = tracer->WithActiveSpan(span);
210+
130211
std::string dl_file_path = dir + "/" + DOWNLOAD_TMP_NAME;
212+
span->SetAttribute("download_path", dl_file_path);
213+
span->SetAttribute("try_count", try_cnt);
131214
try_cnt--;
215+
132216
IFile *src = src_file;
133217
if (limit_MB_ps > 0) {
134218
ThrottleLimits limits;
135219
limits.R.throughput = limit_MB_ps * 1024UL * 1024; // MB
136220
limits.R.block_size = 1024UL * 1024;
137221
limits.time_window = 1UL;
138222
src = new_throttled_file(src, limits);
223+
span->SetAttribute("throttle_limit_mbps", limit_MB_ps);
139224
}
140225
DEFER({
141226
if (limit_MB_ps > 0)
@@ -144,6 +229,8 @@ bool BkDownload::download_blob() {
144229

145230
auto dst = open_localfile_adaptor(dl_file_path.c_str(), O_RDWR | O_CREAT, 0644);
146231
if (dst == nullptr) {
232+
span->SetAttribute("error", "failed_to_open_dst");
233+
span->End();
147234
LOG_ERRNO_RETURN(0, false, "failed to open dst file `", dl_file_path.c_str());
148235
}
149236
DEFER(delete dst;);
@@ -154,13 +241,22 @@ bool BkDownload::download_blob() {
154241
void *buff = nullptr;
155242
// buffer allocate, with 4K alignment
156243
::posix_memalign(&buff, ALIGNMENT, bs);
157-
if (buff == nullptr)
244+
if (buff == nullptr) {
245+
span->SetAttribute("error", "failed_to_allocate_buffer");
246+
span->End();
158247
LOG_ERRNO_RETURN(0, false, "failed to allocate buffer with ", VALUE(bs));
248+
}
159249
DEFER(free(buff));
160250

161251
LOG_INFO("download blob start. (`)", url);
252+
uint64_t total_bytes_read = 0;
253+
uint64_t total_bytes_written = 0;
254+
uint64_t retries = 0;
255+
162256
while (offset < (ssize_t)file_size) {
163257
if (running != 1) {
258+
span->SetAttribute("error", "download_interrupted");
259+
span->End();
164260
LOG_INFO("image file exit when background downloading");
165261
return false;
166262
}
@@ -170,6 +266,7 @@ bool BkDownload::download_blob() {
170266
if (hole_pos >= offset + (ssize_t)bs) {
171267
// alread downloaded
172268
offset += bs;
269+
total_bytes_written += bs;
173270
continue;
174271
}
175272
}
@@ -179,44 +276,88 @@ bool BkDownload::download_blob() {
179276
if (offset + count > file_size)
180277
count = file_size - offset;
181278
again_read:
182-
if (!(retry--))
279+
if (!(retry--)) {
280+
span->SetAttribute("error", "max_read_retries_exceeded");
281+
span->SetAttribute("failed_offset", offset);
282+
span->End();
183283
LOG_ERROR_RETURN(EIO, false, "failed to read at ", VALUE(offset), VALUE(count));
284+
}
184285
ssize_t rlen;
185286
{
287+
auto read_span = tracer->StartSpan("overlaybd.download.read_block");
288+
auto read_scope = tracer->WithActiveSpan(read_span);
289+
read_span->SetAttribute("offset", offset);
290+
read_span->SetAttribute("size", count);
186291
SCOPE_AUDIT("bk_download", AU_FILEOP(url, offset, rlen));
187292
rlen = src->pread(buff, bs, offset);
293+
if (rlen >= 0) {
294+
read_span->SetAttribute("bytes_read", rlen);
295+
total_bytes_read += rlen;
296+
}
297+
read_span->End();
188298
}
189299
if (rlen < 0) {
300+
retries++;
190301
LOG_WARN("failed to read at ", VALUE(offset), VALUE(count), VALUE(errno), " retry...");
191302
goto again_read;
192303
}
193304
retry = 2;
194305
again_write:
195-
if (!(retry--))
306+
if (!(retry--)) {
307+
span->SetAttribute("error", "max_write_retries_exceeded");
308+
span->SetAttribute("failed_offset", offset);
309+
span->End();
196310
LOG_ERROR_RETURN(EIO, false, "failed to write at ", VALUE(offset), VALUE(count));
311+
}
312+
auto write_span = tracer->StartSpan("overlaybd.download.write_block");
313+
auto write_scope = tracer->WithActiveSpan(write_span);
314+
write_span->SetAttribute("offset", offset);
315+
write_span->SetAttribute("size", count);
197316
auto wlen = dst->pwrite(buff, count, offset);
317+
if (wlen >= 0) {
318+
write_span->SetAttribute("bytes_written", wlen);
319+
total_bytes_written += wlen;
320+
}
321+
write_span->End();
198322
// but once write lenth larger than read length treats as OK
199323
if (wlen < rlen) {
324+
retries++;
200325
LOG_WARN("failed to write at ", VALUE(offset), VALUE(count), VALUE(errno), " retry...");
201326
goto again_write;
202327
}
203328
offset += count;
204329
}
330+
331+
span->SetAttribute("total_bytes_read", total_bytes_read);
332+
span->SetAttribute("total_bytes_written", total_bytes_written);
333+
span->SetAttribute("total_retries", retries);
334+
span->SetAttribute("success", true);
205335
LOG_INFO("download blob done. (`)", dl_file_path);
336+
span->End();
206337
return true;
207338
}
208339

209340
void bk_download_proc(std::list<BKDL::BkDownload *> &dl_list, uint64_t delay_sec, int &running) {
341+
auto tracer = overlaybd_otel::get_tracer("overlaybd");
342+
auto span = tracer->StartSpan("background_download_process");
343+
auto scope = tracer->WithActiveSpan(span);
344+
345+
span->SetAttribute("delay_seconds", delay_sec);
346+
span->SetAttribute("initial_queue_size", dl_list.size());
347+
210348
LOG_INFO("BACKGROUND DOWNLOAD THREAD STARTED.");
211349
uint64_t time_st = photon::now;
212350
while (photon::now - time_st < delay_sec * 1000000) {
213351
photon::thread_usleep(200 * 1000);
214-
if (running != 1)
352+
if (running != 1) {
353+
span->SetAttribute("early_exit", "delay_interrupted");
215354
break;
355+
}
216356
}
217357

218358
while (!dl_list.empty()) {
219359
if (running != 1) {
360+
span->SetAttribute("early_exit", "image_exited");
220361
LOG_WARN("image exited, background download exit...");
221362
break;
222363
}
@@ -225,9 +366,16 @@ void bk_download_proc(std::list<BKDL::BkDownload *> &dl_list, uint64_t delay_sec
225366
BKDL::BkDownload *dl_item = dl_list.front();
226367
dl_list.pop_front();
227368

369+
auto dl_span = tracer->StartSpan("download_item");
370+
auto dl_scope = tracer->WithActiveSpan(dl_span);
371+
dl_span->SetAttribute("directory", dl_item->dir);
372+
dl_span->SetAttribute("retry_count", dl_item->try_cnt);
373+
228374
LOG_INFO("start downloading for dir `", dl_item->dir);
229375

230376
if (!dl_item->lock_file()) {
377+
dl_span->SetAttribute("status", "lock_failed");
378+
dl_span->End();
231379
dl_list.push_back(dl_item);
232380
continue;
233381
}
@@ -236,22 +384,31 @@ void bk_download_proc(std::list<BKDL::BkDownload *> &dl_list, uint64_t delay_sec
236384
dl_item->unlock_file();
237385

238386
if (running != 1) {
387+
dl_span->SetAttribute("status", "interrupted");
388+
dl_span->End();
239389
LOG_WARN("image exited, background download exit...");
240390
delete dl_item;
241391
break;
242392
}
243393

244394
if (!succ && dl_item->try_cnt > 0) {
395+
dl_span->SetAttribute("status", "retry");
396+
dl_span->End();
245397
dl_list.push_back(dl_item);
246398
LOG_WARN("download failed, push back to download queue and retry `", dl_item->dir);
247399
continue;
248400
}
401+
402+
dl_span->SetAttribute("status", succ ? "success" : "failed");
403+
dl_span->End();
404+
249405
LOG_DEBUG("finish downloading or no retry any more: `, retry_cnt: `", dl_item->dir,
250406
dl_item->try_cnt);
251407
delete dl_item;
252408
}
253409

254410
if (!dl_list.empty()) {
411+
span->SetAttribute("unfinished_downloads", dl_list.size());
255412
LOG_INFO("DOWNLOAD THREAD EXITED in advance, delete dl_list.");
256413
while (!dl_list.empty()) {
257414
BKDL::BkDownload *dl_item = dl_list.front();
@@ -260,6 +417,7 @@ void bk_download_proc(std::list<BKDL::BkDownload *> &dl_list, uint64_t delay_sec
260417
}
261418
}
262419
LOG_INFO("BACKGROUND DOWNLOAD THREAD EXIT.");
420+
span->End();
263421
}
264422

265423
} // namespace BKDL

src/bk_download.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <string>
1919
#include <cstdint>
2020
#include <photon/fs/filesystem.h>
21+
#include "overlaybd/otel/tracer_common.h"
2122

2223
class ImageFile;
2324
class ISwitchFile;

0 commit comments

Comments
 (0)