Skip to main content

azure_core_macros/
lib.rs

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4#![doc = include_str!("../README.md")]
5#![cfg_attr(docsrs, feature(doc_cfg))]
6#![warn(missing_docs)]
7
8mod tracing;
9mod tracing_client;
10mod tracing_function;
11mod tracing_new;
12mod tracing_subclient;
13
14use proc_macro::TokenStream;
15
16/// Attribute client struct declarations to enable distributed tracing.
17///
18/// To declare a client that will be traced, you should use the `#[tracing::client]` attribute
19/// exported from azure_core.
20///
21#[proc_macro_attribute]
22pub fn client(attr: TokenStream, item: TokenStream) -> TokenStream {
23    tracing_client::parse_client(attr.into(), item.into())
24        .map_or_else(|e| e.into_compile_error().into(), |v| v.into())
25}
26
27/// Attribute client struct instantiation to enable distributed tracing.
28///
29/// To enable tracing for a client instantiation, you should use the `#[tracing::new]` attribute
30/// exported from azure_core.
31///
32/// This macro will automatically instrument the client instantiation with tracing information.
33/// It will also ensure that the client is created with the necessary tracing context.
34///
35/// The `#[tracing::new]` attribute takes a single argument, which is a string
36/// representing the Azure Namespace name for the service being traced.
37///
38/// The list of Azure Namespaces can be found [on this page](https://learn.microsoft.com/azure/azure-resource-manager/management/azure-services-resource-providers)
39///
40#[proc_macro_attribute]
41pub fn new(attr: TokenStream, item: TokenStream) -> TokenStream {
42    tracing_new::parse_new(attr.into(), item.into())
43        .map_or_else(|e| e.into_compile_error().into(), |v| v.into())
44}
45
46/// Attribute client subclient struct declarations to enable distributed tracing.
47///
48/// To declare a subclient that will be traced, you should use the `#[tracing::subclient]` attribute
49/// exported from azure_core.
50///
51/// This macro will automatically instrument the subclient declaration with tracing information. It will also ensure that the subclient is created with the necessary tracing context.
52/// The `#[tracing::subclient]` attribute takes a single argument, which is a string representing the Azure Namespace name for the service being traced.
53#[proc_macro_attribute]
54pub fn subclient(attr: TokenStream, item: TokenStream) -> TokenStream {
55    tracing_subclient::parse_subclient(attr.into(), item.into())
56        .map_or_else(|e| e.into_compile_error().into(), |v| v.into())
57}
58
59/// Attribute client public APIs to enable distributed tracing.
60///
61/// To declare a public API function that will be traced, you should use the `#[tracing::function]` attribute
62/// exported from azure_core.
63///
64/// This macro will automatically instrument the public API function with tracing information. It will also ensure that the function is executed with the necessary tracing context.
65///
66/// The `function` attribute takes one required argument, which is a string representing the name of the operation being traced.
67/// This name will be used in the tracing spans to identify the operation being performed. The name should be unique and match the
68/// typespec name for the operation being traced if possible.
69///
70#[proc_macro_attribute]
71pub fn function(attr: TokenStream, item: TokenStream) -> TokenStream {
72    tracing_function::parse_function(attr.into(), item.into())
73        .map_or_else(|e| e.into_compile_error().into(), |v| v.into())
74}