Isomorphic client library for making HTTP requests in node.js and browser with flexible middleware-style pipeline architecture.
—
Logging, tracing, and user agent policies for monitoring, debugging, and identifying HTTP requests in Azure SDK libraries.
Configurable request and response logging for debugging and monitoring.
/**
* A policy that logs information about requests and responses
* @param options - Options for configuring logging behavior
* @returns PipelinePolicy that handles request/response logging
*/
function logPolicy(options?: LogPolicyOptions): PipelinePolicy;
/**
* The programmatic identifier of the logPolicy
*/
const logPolicyName: string;
interface LogPolicyOptions {
/**
* Header names whose values will be logged when logging is enabled
*/
allowedHeaders?: string[];
/**
* Query string names whose values will be logged when logging is enabled
*/
allowedQueryParameters?: string[];
/**
* The log function to use for writing log messages
*/
logger?: AzureLogger;
}Usage Examples:
import { logPolicy, type LogPolicyOptions } from "@azure/core-rest-pipeline";
// Basic logging with console
const basicLogPolicy = logPolicy({
logger: console
});
// Detailed logging configuration
const detailedLogOptions: LogPolicyOptions = {
logger: console,
allowedHeaders: [
"Authorization",
"Content-Type",
"User-Agent",
"x-ms-request-id"
],
allowedQueryParameters: [
"api-version",
"timeout"
]
};
const detailedLogPolicy = logPolicy(detailedLogOptions);
pipeline.addPolicy(detailedLogPolicy);Distributed tracing integration for tracking requests across services.
/**
* A policy that creates and manages distributed tracing spans for requests
* @param options - Options for configuring tracing behavior
* @returns PipelinePolicy that handles distributed tracing
*/
function tracingPolicy(options?: TracingPolicyOptions): PipelinePolicy;
/**
* The programmatic identifier of the tracingPolicy
*/
const tracingPolicyName: string;
interface TracingPolicyOptions {
/**
* String to use as the user agent prefix when creating the tracing span
*/
userAgentPrefix?: string;
/**
* Additional attributes to add to the tracing span
*/
additionalPolicyConfig?: Record<string, unknown>;
}Usage Examples:
import { tracingPolicy, type TracingPolicyOptions } from "@azure/core-rest-pipeline";
// Basic tracing
const basicTracingPolicy = tracingPolicy();
// Tracing with custom configuration
const tracingOptions: TracingPolicyOptions = {
userAgentPrefix: "MyService/1.0",
additionalPolicyConfig: {
"service.name": "my-azure-service",
"service.version": "1.0.0"
}
};
const customTracingPolicy = tracingPolicy(tracingOptions);
pipeline.addPolicy(customTracingPolicy);Sets and manages User-Agent headers for identifying the client and SDK version.
/**
* A policy that sets the User-Agent header on requests
* @param options - Options for configuring the user agent
* @returns PipelinePolicy that sets User-Agent headers
*/
function userAgentPolicy(options?: UserAgentPolicyOptions): PipelinePolicy;
/**
* The programmatic identifier of the userAgentPolicy
*/
const userAgentPolicyName: string;
interface UserAgentPolicyOptions {
/**
* String prefix to add to the user agent header
*/
userAgentPrefix?: string;
}Usage Examples:
import { userAgentPolicy, type UserAgentPolicyOptions } from "@azure/core-rest-pipeline";
// Basic user agent
const basicUserAgentPolicy = userAgentPolicy();
// Custom user agent prefix
const userAgentOptions: UserAgentPolicyOptions = {
userAgentPrefix: "MyAzureApp/2.1.0"
};
const customUserAgentPolicy = userAgentPolicy(userAgentOptions);
pipeline.addPolicy(customUserAgentPolicy);
// The resulting User-Agent header will be something like:
// "MyAzureApp/2.1.0 azsdk-js-core-rest-pipeline/1.22.0 Node/18.17.0 (x64-Linux-5.15.0)"Generates and sets unique request IDs for tracking individual requests.
/**
* A policy that generates a unique request ID for each request and sets it in the headers
* @param clientRequestIdHeaderName - The name of the header to set (defaults to "x-ms-client-request-id")
* @returns PipelinePolicy that sets client request IDs
*/
function setClientRequestIdPolicy(clientRequestIdHeaderName?: string): PipelinePolicy;
/**
* The programmatic identifier of the setClientRequestIdPolicy
*/
const setClientRequestIdPolicyName: string;Usage Examples:
import { setClientRequestIdPolicy } from "@azure/core-rest-pipeline";
// Add request ID generation
pipeline.addPolicy(setClientRequestIdPolicy());
// The policy will automatically generate and set headers like:
// "x-ms-client-request-id": "12345678-1234-1234-1234-123456789012"Configure comprehensive observability with all policies working together:
import {
createPipelineFromOptions,
logPolicy,
tracingPolicy,
userAgentPolicy,
setClientRequestIdPolicy,
type LogPolicyOptions,
type TracingPolicyOptions,
type UserAgentPolicyOptions
} from "@azure/core-rest-pipeline";
// Create pipeline with observability
const pipeline = createPipelineFromOptions({
userAgentOptions: {
userAgentPrefix: "MyApp/1.0.0"
}
});
// Add comprehensive observability
const logOptions: LogPolicyOptions = {
logger: console,
allowedHeaders: ["Content-Type", "Authorization", "x-ms-request-id"],
allowedQueryParameters: ["api-version"]
};
const tracingOptions: TracingPolicyOptions = {
userAgentPrefix: "MyApp/1.0.0",
additionalPolicyConfig: {
"service.name": "my-service"
}
};
// Add policies in recommended order
pipeline.addPolicy(setClientRequestIdPolicy());
pipeline.addPolicy(tracingPolicy(tracingOptions));
pipeline.addPolicy(logPolicy(logOptions));You can integrate with custom logging frameworks:
import { logPolicy, type LogPolicyOptions } from "@azure/core-rest-pipeline";
// Custom logger implementation
class CustomLogger {
info(message: string) {
// Send to custom logging service
console.log(`[INFO] ${new Date().toISOString()} ${message}`);
}
warning(message: string) {
console.warn(`[WARN] ${new Date().toISOString()} ${message}`);
}
error(message: string) {
console.error(`[ERROR] ${new Date().toISOString()} ${message}`);
}
}
const customLogger = new CustomLogger();
const logOptions: LogPolicyOptions = {
logger: customLogger,
allowedHeaders: ["Content-Type", "User-Agent"],
allowedQueryParameters: ["api-version", "timeout"]
};
pipeline.addPolicy(logPolicy(logOptions));/**
* Policy name constants for use in pipeline configuration
*/
const logPolicyName: string;
const tracingPolicyName: string;
const userAgentPolicyName: string;
const setClientRequestIdPolicyName: string;These constants are used for policy ordering and identification in pipeline configuration.
Install with Tessl CLI
npx tessl i tessl/npm-azure--core-rest-pipeline