Skip to content

Commit

Permalink
Merge pull request #541 from SteveL-MSFT/trace-envvar
Browse files Browse the repository at this point in the history
Add `pass-through` trace format to `dsc` so that child-processes get traced appropriately by top process
  • Loading branch information
SteveL-MSFT authored Sep 13, 2024
2 parents f047b42 + f573900 commit 2e742c9
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 37 deletions.
1 change: 1 addition & 0 deletions dsc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 8 additions & 10 deletions dsc/assertion.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"get": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-group",
"test",
Expand All @@ -17,6 +19,8 @@
"set": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-group",
"test"
Expand All @@ -28,6 +32,8 @@
"test": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-group",
"test",
Expand All @@ -45,19 +51,11 @@
"5": "Resource instance failed schema validation",
"6": "Command cancelled"
},
"schema": {
"command": {
"executable": "dsc",
"args": [
"schema",
"--type",
"configuration"
]
}
},
"validate": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"validate"
],
Expand Down
8 changes: 8 additions & 0 deletions dsc/group.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"get": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-group",
"get"
Expand All @@ -16,6 +18,8 @@
"set": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-group",
"set"
Expand All @@ -27,6 +31,8 @@
"test": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-group",
"test"
Expand All @@ -46,6 +52,8 @@
"validate": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"validate"
],
Expand Down
8 changes: 8 additions & 0 deletions dsc/include.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"get": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-include",
"--as-group",
Expand All @@ -17,6 +19,8 @@
"set": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-include",
"--as-group",
Expand All @@ -29,6 +33,8 @@
"test": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-include",
"--as-group",
Expand All @@ -48,6 +54,8 @@
"validate": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--as-include",
"validate"
Expand Down
8 changes: 8 additions & 0 deletions dsc/parallel.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"get": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--parallel",
"--as-group",
Expand All @@ -17,6 +19,8 @@
"set": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--parallel",
"--as-group",
Expand All @@ -29,6 +33,8 @@
"test": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"--parallel",
"--as-group",
Expand All @@ -49,6 +55,8 @@
"validate": {
"executable": "dsc",
"args": [
"--trace-format",
"pass-through",
"config",
"validate"
],
Expand Down
12 changes: 3 additions & 9 deletions dsc/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use clap::{Parser, Subcommand, ValueEnum};
use clap_complete::Shell;
use dsc_lib::dscresources::command_resource::TraceLevel;

#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
Expand All @@ -16,15 +17,8 @@ pub enum TraceFormat {
Default,
Plaintext,
Json,
}

#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
pub enum TraceLevel {
Error,
Warn,
Info,
Debug,
Trace
#[clap(hide = true)]
PassThrough,
}

#[derive(Debug, Parser)]
Expand Down
14 changes: 7 additions & 7 deletions dsc/src/tablewriter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crossterm::terminal::{size};
use crossterm::terminal::size;

pub struct Table {
header: Vec<String>,
Expand All @@ -8,13 +8,13 @@ pub struct Table {

impl Table {
/// Create a new table.
///
///
/// # Arguments
///
///
/// * `header` - The header row
///
///
/// # Returns
///
///
/// * `Table` - The new table
#[must_use]
pub fn new(header: &[&str]) -> Table {
Expand All @@ -31,9 +31,9 @@ impl Table {
}

/// Add a row to the table.
///
///
/// # Arguments
///
///
/// * `row` - The row to add
pub fn add_row(&mut self, row: Vec<String>) {
for (i, column) in row.iter().enumerate() {
Expand Down
8 changes: 4 additions & 4 deletions dsc/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::args::{DscType, OutputFormat, TraceFormat, TraceLevel};

use crate::args::{DscType, OutputFormat, TraceFormat};
use atty::Stream;
use crate::resolve::Include;
use dsc_lib::{
Expand All @@ -16,6 +15,7 @@ use dsc_lib::{
},
dscerror::DscError,
dscresources::{
command_resource::TraceLevel,
dscresource::DscResource, invoke_result::{
GetResult,
SetResult,
Expand Down Expand Up @@ -323,15 +323,15 @@ pub fn enable_tracing(trace_level: &Option<TraceLevel>, trace_format: &TraceForm
.with_line_number(with_source)
.boxed()
},
TraceFormat::Json => {
TraceFormat::Json | TraceFormat::PassThrough => {
layer
.with_ansi(false)
.with_level(true)
.with_target(with_source)
.with_line_number(with_source)
.json()
.boxed()
}
},
};

let subscriber = tracing_subscriber::Registry::default().with(fmt).with(filter).with(indicatif_layer);
Expand Down
12 changes: 12 additions & 0 deletions dsc/tests/dsc_tracing.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ Describe 'tracing tests' {
$out = (dsc -l $level config get -d $configYaml 2> $null) | ConvertFrom-Json
$out.results[0].result.actualState.level | Should -BeExactly $level
}

It 'Pass-through tracing should only emit JSON for child processes' {
$logPath = "$TestDrive/dsc_trace.log"
$out = dsc -l info -f pass-through config get -p ../examples/groups.dsc.yaml 2> $logPath
foreach ($line in (Get-Content $logPath)) {
$line | Should -Not -BeNullOrEmpty
$json = $line | ConvertFrom-Json
$json.timestamp | Should -Not -BeNullOrEmpty
$json.level | Should -BeIn 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE'
}
$out | Should -BeNullOrEmpty
}
}
1 change: 1 addition & 0 deletions dsc_lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dsc_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
base64 = "0.22.1"
chrono = "0.4.26"
clap = { version = "4.4", features = ["derive"] }
derive_builder ="0.20.0"
indicatif = "0.17.0"
jsonschema = "0.18.0"
Expand Down
Loading

0 comments on commit 2e742c9

Please sign in to comment.