OpenTelemetry SDK resources for representing immutable entities that produce telemetry data
npx @tessl/cli install tessl/npm-opentelemetry--resources@2.1.0OpenTelemetry Resources provides utilities for representing immutable entities that produce telemetry data. It includes resource detection capabilities for automatically discovering environment-specific information like host details, process information, and service metadata across Node.js and browser environments.
npm install @opentelemetry/resourcesimport {
Resource,
detectResources,
resourceFromAttributes,
resourceFromDetectedResource,
defaultResource,
emptyResource,
envDetector,
hostDetector,
osDetector,
processDetector,
serviceInstanceIdDetector,
noopDetector,
defaultServiceName
} from "@opentelemetry/resources";For CommonJS:
const {
detectResources,
resourceFromAttributes,
resourceFromDetectedResource,
defaultResource,
emptyResource,
envDetector,
hostDetector,
osDetector,
processDetector,
serviceInstanceIdDetector,
noopDetector,
defaultServiceName
} = require("@opentelemetry/resources");import {
detectResources,
resourceFromAttributes,
resourceFromDetectedResource,
envDetector,
hostDetector
} from "@opentelemetry/resources";
// Automatic resource detection
const autoDetectedResource = detectResources({
detectors: [envDetector, hostDetector]
});
// Manual resource creation
const manualResource = resourceFromAttributes({
"service.name": "my-service",
"service.version": "1.0.0",
"host.name": "production-server"
});
// Create resource from DetectedResource object
const detectedResourceObj = {
attributes: {
"deployment.environment": "production",
"service.instance.id": "instance-123"
}
};
const resourceFromDetected = resourceFromDetectedResource(detectedResourceObj);
// Merge resources
const combinedResource = autoDetectedResource.merge(manualResource).merge(resourceFromDetected);
// Access resource attributes
console.log(combinedResource.attributes);OpenTelemetry Resources is built around several key components:
Core functionality for creating, manipulating, and accessing resource information.
interface Resource {
readonly asyncAttributesPending?: boolean;
readonly attributes: Attributes;
readonly schemaUrl?: string;
waitForAsyncAttributes?(): Promise<void>;
merge(other: Resource | null): Resource;
getRawAttributes(): RawResourceAttribute[];
}
function resourceFromAttributes(
attributes: DetectedResourceAttributes,
options?: ResourceOptions
): Resource;
function resourceFromDetectedResource(
detectedResource: DetectedResource,
options?: ResourceOptions
): Resource;
function defaultResource(): Resource;
function emptyResource(): Resource;
function defaultServiceName(): string;Automatic resource discovery system with built-in detectors for environment variables, host information, operating system details, and process metadata.
function detectResources(config?: ResourceDetectionConfig): Resource;
interface ResourceDetectionConfig {
detectors?: ResourceDetector[];
}
interface ResourceDetector {
detect(config?: ResourceDetectionConfig): DetectedResource | Promise<DetectedResource>;
}interface ResourceOptions {
schemaUrl?: string;
}
type DetectedResource = {
attributes?: DetectedResourceAttributes;
schemaUrl?: string;
};
type DetectedResourceAttributes = Record<string, AttributeValue | Promise<AttributeValue> | undefined>;
type RawResourceAttribute = [string, MaybePromise<AttributeValue | undefined>];
type MaybePromise<T> = T | Promise<T>;
type AttributeValue =
| string
| number
| boolean
| Array<null | undefined | string>
| Array<null | undefined | number>
| Array<null | undefined | boolean>;
type Attributes = Record<string, AttributeValue>;