Skip to content

Commit

Permalink
[diem-trace] restore previous behavior of set_diem_trace
Browse files Browse the repository at this point in the history
The previous behavior of this function was to error out on being called
more than once. Restore that behavior while not introducing unsafe code.

There's a separate function `diem_trace_set` whose behavior didn't
change -- it would return true if in INITIALIZED status, which is what
the current function does as well.

Closes: aptos-labs#7667
  • Loading branch information
sunshowers authored and bors-libra committed Feb 18, 2021
1 parent 2c291a7 commit 059ff2c
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions common/trace/src/trace.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::{ensure, Result};
use anyhow::{bail, ensure, Context, Result};
use diem_logger::json_log::JsonLogEntry;
use std::time::Instant;
use once_cell::sync::OnceCell;
use std::{collections::HashMap, time::Instant};

pub const TRACE_EVENT: &str = "trace_event";
pub const TRACE_EDGE: &str = "trace_edge";
pub const DIEM_TRACE: &str = "diem_trace";

use once_cell::sync::OnceCell;
use std::collections::HashMap;

// Sampling rate is the form of (nominator, denominator)
static SAMPLING_CONFIG: OnceCell<Sampling> = OnceCell::new();

Expand Down Expand Up @@ -390,9 +388,23 @@ fn abbreviate_crate(name: &str) -> &str {
}
}

/// Sets diem trace config
/// Sets diem trace config.
///
/// This should only be called once.
pub fn set_diem_trace(config: &HashMap<String, String>) -> Result<()> {
SAMPLING_CONFIG.get_or_try_init(|| parse_sampling_config(config))?;
// Ensure that this function is called just once. OnceCell guarantees that its initializer is
// called exactly once.
let mut initializer_called = false;
SAMPLING_CONFIG
.get_or_try_init(|| {
initializer_called = true;
parse_sampling_config(config)
})
.with_context(|| "failed to parse sampling config")?;

if !initializer_called {
bail!("failed to initialize: set_diem_trace called multiple times")
}
Ok(())
}

Expand Down

0 comments on commit 059ff2c

Please sign in to comment.