CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-opentelemetry--resources

OpenTelemetry SDK resources for representing immutable entities that produce telemetry data

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

resource-management.mddocs/

Resource Management

Core functionality for creating, manipulating, and accessing resource information. Resources represent immutable entities that produce telemetry data, containing attributes that describe the environment, service, host, and other relevant metadata.

Capabilities

Resource Creation from Attributes

Creates a Resource instance from a set of attributes, with optional configuration.

/**
 * Creates a Resource instance from detected resource attributes
 * @param attributes - Resource attributes that may contain promises
 * @param options - Optional configuration including schema URL
 * @returns Resource instance with the provided attributes
 */
function resourceFromAttributes(
  attributes: DetectedResourceAttributes, 
  options?: ResourceOptions
): Resource;

interface ResourceOptions {
  /** Schema URL for the resource attributes */
  schemaUrl?: string;
}

type DetectedResourceAttributes = Record<string, AttributeValue | Promise<AttributeValue> | undefined>;

Usage Examples:

import { resourceFromAttributes } from "@opentelemetry/resources";

// Simple resource creation
const resource = resourceFromAttributes({
  "service.name": "my-api",
  "service.version": "2.1.0",
  "deployment.environment": "production"
});

// Resource with schema URL
const resourceWithSchema = resourceFromAttributes({
  "service.name": "my-api",
  "host.name": "server-01"
}, {
  schemaUrl: "https://opentelemetry.io/schemas/1.9.0"
});

// Resource with async attributes
const resourceWithAsync = resourceFromAttributes({
  "service.name": "my-api",
  "host.id": Promise.resolve("unique-host-id"),
  "process.pid": Promise.resolve(process.pid)
});

Resource Creation from DetectedResource

Creates a Resource instance from a DetectedResource object, with optional configuration.

/**
 * Creates a Resource instance from a DetectedResource object
 * @param detectedResource - DetectedResource object containing attributes and optional schema URL
 * @param options - Optional configuration including schema URL override
 * @returns Resource instance with the provided attributes
 */
function resourceFromDetectedResource(
  detectedResource: DetectedResource,
  options?: ResourceOptions
): Resource;

Usage Examples:

import { resourceFromDetectedResource } from "@opentelemetry/resources";

// Create resource from DetectedResource object
const detectedResource = {
  attributes: {
    "service.name": "detected-service",
    "host.name": "auto-detected-host"
  },
  schemaUrl: "https://opentelemetry.io/schemas/1.9.0"
};

const resource = resourceFromDetectedResource(detectedResource);

// Override schema URL
const resourceWithOverride = resourceFromDetectedResource(detectedResource, {
  schemaUrl: "https://opentelemetry.io/schemas/1.10.0"
});

Default Resource Creation

Creates a resource with SDK information and default attributes.

/**
 * Creates a default resource containing SDK information
 * @returns Resource with default SDK attributes
 */
function defaultResource(): Resource;

Usage Examples:

import { defaultResource } from "@opentelemetry/resources";

// Get default resource with SDK info
const sdkResource = defaultResource();
console.log(sdkResource.attributes);
// Contains telemetry.sdk.name, telemetry.sdk.language, telemetry.sdk.version

Empty Resource Creation

Creates an empty resource with no attributes.

/**
 * Creates an empty resource with no attributes
 * @returns Empty Resource instance
 */
function emptyResource(): Resource;

Usage Examples:

import { emptyResource } from "@opentelemetry/resources";

// Create empty resource as a base for building
const baseResource = emptyResource();
const customResource = baseResource.merge(resourceFromAttributes({
  "service.name": "custom-service"
}));

Default Service Name

Returns platform-specific default service name.

/**
 * Returns the default service name for the current platform
 * @returns Default service name string
 */
function defaultServiceName(): string;

Usage Examples:

import { defaultServiceName } from "@opentelemetry/resources";

// Get platform-specific default service name
const serviceName = defaultServiceName();
console.log(serviceName); // "unknown_service:node" on Node.js, "unknown_service" on browser

Resource Interface

The core Resource interface providing access to attributes and merge capabilities.

/**
 * Represents an immutable resource with attributes describing a telemetry source
 */
interface Resource {
  /** Indicates if there are pending asynchronous attributes */
  readonly asyncAttributesPending?: boolean;
  /** Resource attributes as key-value pairs */
  readonly attributes: Attributes;
  /** Optional schema URL for the resource */
  readonly schemaUrl?: string;
  
  /** 
   * Waits for all asynchronous attributes to resolve
   * @returns Promise that resolves when all async attributes are ready
   */
  waitForAsyncAttributes?(): Promise<void>;
  
  /**
   * Merges this resource with another resource, with the other resource taking precedence
   * @param other - Resource to merge with (or null)
   * @returns New merged Resource instance
   */
  merge(other: Resource | null): Resource;
  
  /**
   * Gets raw resource attributes including unresolved promises
   * @returns Array of attribute key-value tuples
   */
  getRawAttributes(): RawResourceAttribute[];
}

type RawResourceAttribute = [string, MaybePromise<AttributeValue | undefined>];
type MaybePromise<T> = T | Promise<T>;

Usage Examples:

import { resourceFromAttributes, defaultResource } from "@opentelemetry/resources";

// Create resources
const serviceResource = resourceFromAttributes({
  "service.name": "my-service",
  "service.version": "1.0.0"
});

const hostResource = resourceFromAttributes({
  "host.name": "production-server",
  "host.arch": "x64"
});

// Merge resources (hostResource attributes take precedence)
const combinedResource = serviceResource.merge(hostResource);

// Access merged attributes
console.log(combinedResource.attributes);

// Handle async attributes
const asyncResource = resourceFromAttributes({
  "service.name": "my-service",
  "dynamic.value": Promise.resolve("computed-value")
});

if (asyncResource.asyncAttributesPending) {
  await asyncResource.waitForAsyncAttributes();
  console.log(asyncResource.attributes); // Now includes resolved "dynamic.value"
}

// Get raw attributes (including unresolved promises)
const rawAttributes = asyncResource.getRawAttributes();
console.log(rawAttributes); // [["service.name", "my-service"], ["dynamic.value", Promise<string>]]

Type Definitions

// From @opentelemetry/api
type AttributeValue = 
  | string 
  | number 
  | boolean 
  | Array<null | undefined | string> 
  | Array<null | undefined | number> 
  | Array<null | undefined | boolean>;

type Attributes = Record<string, AttributeValue>;

Install with Tessl CLI

npx tessl i tessl/npm-opentelemetry--resources

docs

index.md

resource-detection.md

resource-management.md

tile.json