CodeSampleX

示例

tracing-attributes 0.1.31: tracing_attributes.instrument

已验证示例 — cargo tracing-attributes 0.1.31: tracing_attributes.instrument. contract 在 rust 1 · linux alpine/x64 · docker 上运行并通过: #[instrument(name =…

sha256:0d267f78451552ef9110e6e60ba3197e77b12f5b907e2951241cf37733fbf1db

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux x64 rust cargo

验证运行环境

环境 契约 阶段 运行日期
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-11-01

案例

HOW
目标
verify pkg:cargo/tracing-attributes@0.1.31
包
符号
  • tracing_attributes.instrument
创建时间
2026-11-01T17:37:12Z

契约

  1. #[instrument(name = "custom_add", skip(ignored), fields(extra = "tag"))] creates a span named "custom_add" when the annotated function is invoked under a tracing Subscriber
  2. Arguments not listed in skip(...) (a = 10, b = 32) and static fields(extra = "tag") are recorded on the span, while skipped arguments (ignored) are omitted
  3. The instrumented function executes normally and returns the expected sum (42)

文件

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/main.rs

下载源代码构件 (tar.gz)

源代码

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"

[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "sample"
version = "0.1.0"
dependencies = [
 "tracing",
 "tracing-attributes",
]

[[package]]
name = "syn"
version = "2.0.119"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "tracing"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
 "pin-project-lite",
 "tracing-attributes",
 "tracing-core",
]

[[package]]
name = "tracing-attributes"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "tracing-core"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [
 "once_cell",
]

[[package]]
name = "unicode-ident"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
Cargo.toml
[package]
name = "sample"
version = "0.1.0"
edition = "2021"

[dependencies]
tracing-attributes = "=0.1.31"
tracing = "=0.1.44"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify pkg:cargo/tracing-attributes@0.1.31
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/tracing-attributes@0.1.31

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:879647fd99dcacd9bd515da040cfcc71de3236def3198b7769756d70fcb045a5","contract":["#[instrument(name = \"custom_add\", skip(ignored), fields(extra = \"tag\"))] creates a span named \"custom_add\" when the annotated function is invoked under a tracing Subscriber","Arguments not listed in skip(...) (a = 10, b = 32) and static fields(extra = \"tag\") are recorded on the span, while skipped arguments (ignored) are omitted","The instrumented function executes normally and returns the expected sum (42)"],"goal":"verify pkg:cargo/tracing-attributes@0.1.31","kind":"HOW","packages":["pkg:cargo/tracing-attributes@0.1.31","pkg:cargo/tracing@0.1.44"],"schemaVersion":1,"symbols":["tracing_attributes.instrument"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","language":"rust","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/tracing-attributes@0.1.31","pkg:cargo/tracing@0.1.44"],"schemaVersion":1,"subject":"pkg:cargo/tracing-attributes@0.1.31","symbols":["tracing_attributes.instrument"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:cargo/tracing-attributes@0.1.31",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/tracing-attributes@0.1.31"
  ]
}
src/main.rs
use std::sync::{Arc, Mutex};
use tracing::field::{Field, Visit};
use tracing::span::{Attributes, Id, Record};
use tracing::{Event, Metadata, Subscriber};
use tracing_attributes::instrument;

struct FieldVisitor(Vec<(String, String)>);

impl Visit for FieldVisitor {
    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        self.0
            .push((field.name().to_string(), format!("{:?}", value)));
    }
}

struct RecordingSubscriber {
    spans: Arc<Mutex<Vec<(String, Vec<(String, String)>)>>>,
}

impl Subscriber for RecordingSubscriber {
    fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
        true
    }

    fn new_span(&self, span: &Attributes<'_>) -> Id {
        let mut visitor = FieldVisitor(Vec::new());
        span.record(&mut visitor);
        let mut guard = self.spans.lock().unwrap();
        guard.push((span.metadata().name().to_string(), visitor.0));
        Id::from_u64(guard.len() as u64)
    }

    fn record(&self, _span: &Id, _values: &Record<'_>) {}
    fn record_follows_from(&self, _span: &Id, _follows: &Id) {}
    fn event(&self, _event: &Event<'_>) {}
    fn enter(&self, _span: &Id) {}
    fn exit(&self, _span: &Id) {}
}

#[instrument(name = "custom_add", skip(ignored), fields(extra = "tag"))]
fn add_values(a: i32, b: i32, ignored: &str) -> i32 {
    let _ = ignored;
    a + b
}

fn main() {
    let spans = Arc::new(Mutex::new(Vec::new()));
    let subscriber = RecordingSubscriber {
        spans: Arc::clone(&spans),
    };

    tracing::subscriber::with_default(subscriber, || {
        let sum = add_values(10, 32, "secret");
        assert_eq!(sum, 42);
    });

    let recorded = spans.lock().unwrap();
    assert_eq!(recorded.len(), 1);
    let (ref name, ref fields) = recorded[0];
    assert_eq!(name, "custom_add");
    assert!(fields.iter().any(|(k, v)| k == "a" && v == "10"));
    assert!(fields.iter().any(|(k, v)| k == "b" && v == "32"));
    assert!(fields.iter().any(|(k, v)| k == "extra" && v == "\"tag\""));
    assert!(!fields.iter().any(|(k, _)| k == "ignored"));
}

原始种子者

匿名