OpenTelemetry SDK resources for representing immutable entities that produce telemetry data
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Automatic resource discovery system with built-in detectors for environment variables, host information, operating system details, and process metadata. The detection system provides a pluggable architecture for discovering environment-specific resource attributes.
Main function that runs resource detectors and merges their results into a single Resource.
/**
* Detects resources using the provided detectors and merges results
* @param config - Optional configuration specifying which detectors to run
* @returns Merged Resource containing all detected attributes
*/
function detectResources(config?: ResourceDetectionConfig): Resource;
interface ResourceDetectionConfig {
/** Array of resource detectors to run. If not provided, runs all available detectors */
detectors?: ResourceDetector[];
}Usage Examples:
import {
detectResources,
envDetector,
hostDetector,
osDetector,
processDetector
} from "@opentelemetry/resources";
// Detect using all available detectors
const allResources = detectResources();
// Detect using specific detectors
const selectedResources = detectResources({
detectors: [envDetector, hostDetector, osDetector]
});
// Detect using only environment variables
const envOnlyResources = detectResources({
detectors: [envDetector]
});
console.log(selectedResources.attributes);Interface for implementing custom resource detectors.
/**
* Interface for resource detector implementations
*/
interface ResourceDetector {
/**
* Detects resource attributes specific to this detector's domain
* @param config - Optional detection configuration
* @returns Detected resource attributes (synchronous)
*/
detect(config?: ResourceDetectionConfig): DetectedResource;
}
type DetectedResource = {
/** Detected resource attributes (optional) */
attributes?: DetectedResourceAttributes;
/** Optional schema URL for the detected attributes */
schemaUrl?: string;
};
type DetectedResourceAttributes = Record<string, AttributeValue | Promise<AttributeValue> | undefined>;Detects resources from OpenTelemetry environment variables.
/**
* Detects resources from OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME environment variables
*/
declare const envDetector: ResourceDetector;Environment Variables:
OTEL_RESOURCE_ATTRIBUTES: Comma-separated key=value pairsOTEL_SERVICE_NAME: Service name overrideUsage Examples:
import { envDetector, detectResources } from "@opentelemetry/resources";
// Set environment variables
process.env.OTEL_SERVICE_NAME = "my-service";
process.env.OTEL_RESOURCE_ATTRIBUTES = "service.version=1.2.0,deployment.environment=production";
// Detect from environment
const envResource = detectResources({ detectors: [envDetector] });
console.log(envResource.attributes);
// { "service.name": "my-service", "service.version": "1.2.0", "deployment.environment": "production" }Detects host-related resource attributes in Node.js environments.
/**
* Detects host information including hostname, architecture, and host ID
* Available only in Node.js environments
*/
declare const hostDetector: ResourceDetector;Detected Attributes:
host.name: System hostnamehost.arch: System architecture (x64, arm64, etc.)host.id: Unique host identifierUsage Examples:
import { hostDetector, detectResources } from "@opentelemetry/resources";
// Detect host information (Node.js only)
const hostResource = detectResources({ detectors: [hostDetector] });
console.log(hostResource.attributes);
// { "host.name": "server-01", "host.arch": "x64", "host.id": "unique-host-id" }Detects operating system information in Node.js environments.
/**
* Detects operating system type and version
* Available only in Node.js environments
*/
declare const osDetector: ResourceDetector;Detected Attributes:
os.type: Operating system type (Linux, Windows, Darwin, etc.)os.version: Operating system versionUsage Examples:
import { osDetector, detectResources } from "@opentelemetry/resources";
// Detect OS information (Node.js only)
const osResource = detectResources({ detectors: [osDetector] });
console.log(osResource.attributes);
// { "os.type": "Linux", "os.version": "5.4.0-74-generic" }Detects process-related resource attributes in Node.js environments.
/**
* Detects process information including PID, executable details, and runtime information
* Available only in Node.js environments
*/
declare const processDetector: ResourceDetector;Detected Attributes:
process.pid: Process IDprocess.executable.name: Executable nameprocess.executable.path: Executable pathprocess.command: Command used to run the processprocess.command_args: Command argumentsprocess.runtime.name: Runtime name (nodejs)process.runtime.version: Runtime versionprocess.runtime.description: Runtime descriptionUsage Examples:
import { processDetector, detectResources } from "@opentelemetry/resources";
// Detect process information (Node.js only)
const processResource = detectResources({ detectors: [processDetector] });
console.log(processResource.attributes);
// {
// "process.pid": 12345,
// "process.executable.name": "node",
// "process.runtime.name": "nodejs",
// "process.runtime.version": "18.19.0",
// ...
// }Detects or generates a unique service instance identifier.
/**
* Detects or generates a unique service instance ID
* Available only in Node.js environments
*/
declare const serviceInstanceIdDetector: ResourceDetector;Detected Attributes:
service.instance.id: Unique service instance identifierUsage Examples:
import { serviceInstanceIdDetector, detectResources } from "@opentelemetry/resources";
// Detect service instance ID (Node.js only)
const instanceResource = detectResources({ detectors: [serviceInstanceIdDetector] });
console.log(instanceResource.attributes);
// { "service.instance.id": "unique-instance-id" }All detectors are available and provide comprehensive system information:
Limited detection capabilities due to browser security restrictions:
Usage Examples:
import { detectResources, envDetector } from "@opentelemetry/resources";
// Cross-platform detection (adapts to environment)
const resource = detectResources();
// Browser-safe detection
const browserSafeResource = detectResources({
detectors: [envDetector] // Other detectors will return empty results in browser
});import { ResourceDetector, DetectedResource } from "@opentelemetry/resources";
class CustomDetector implements ResourceDetector {
async detect(): Promise<DetectedResource> {
return {
attributes: {
"custom.attribute": "custom-value",
"async.attribute": Promise.resolve("async-value")
}
};
}
}
const customDetector = new CustomDetector();
const resource = detectResources({ detectors: [customDetector] });A no-operation detector that returns empty attributes, useful for testing or when no detection is needed.
/**
* A no-operation detector that returns empty resource attributes
*/
declare const noopDetector: ResourceDetector;Usage Examples:
import { noopDetector, detectResources } from "@opentelemetry/resources";
// Use noop detector (returns empty attributes)
const emptyResource = detectResources({ detectors: [noopDetector] });
console.log(emptyResource.attributes); // {}Install with Tessl CLI
npx tessl i tessl/npm-opentelemetry--resources