Otel custom instrumentation

From Intense-Visions/harness-engineering · View interactive version

Otel custom instrumentation is an AI agent skill from the GitHub repository Intense-Visions/harness-engineering. > Add custom spans, attributes, and events to business-critical code paths beyond auto-instrumentation

How to use this skill

Skill file

OpenTelemetry Custom Instrumentation

Add custom spans, attributes, and events to business-critical code paths beyond auto-instrumentation

When to Use

Instructions

  1. Import the API only (@opentelemetry/api), not the SDK, in application code. The SDK is initialized in instrumentation.ts.
  2. Get a tracer once per module: const tracer = trace.getTracer('module-name').
  3. Use tracer.startActiveSpan for most cases — it sets the span as active context automatically.
  4. Add attributes that help debugging: IDs, counts, flags, operation types.
  5. Record exceptions with span.recordException(error) before setting error status.
  6. For utility functions called from many places, create a reusable tracing wrapper.
// decorators/traced.ts — reusable tracing wrapper
import { trace, SpanKind, SpanStatusCode, Span } from '@opentelemetry/api';

const tracer = trace.getTracer('app');

export function traced<T extends (...args: any[]) => Promise<any>>(
  name: string,
  fn: T,
  options?: {
    kind?: SpanKind;
    attributes?: Record<string, string | number | boolean>;
  }
): T {
  return (async (...args: any[]) => {
    return tracer.startActiveSpan(
      name,
      {
        kind: options?.kind ?? SpanKind.INTERNAL,
        attributes: options?.attributes,
      },
      async (span: Span) => {
        try {
          const result = await fn(...args);
          span.setStatus({ code: SpanStatusCode.OK });
          return result;
        } catch (error) {
          span.recordException(error as Error);
          span.setStatus({ code: SpanStatusCode.ERROR, message: (error as Error).message });
          throw error;
        } finally {
          span.end();
        }
      }
    );
  }) as T;
}

// Usage
const processPayment = traced('payment.process', async (orderId: string, amount: number) => {
  const span = trace.getActiveSpan();
  span?.setAttribute('payment.order_id', orderId);
  span?.setAttribute('payment.amount', amount);

  const result = await paymentGateway.charge(orderId, amount);

  span?.setAttribute('payment.transaction_id', result.transactionId);
  span?.addEvent('payment.charged', { 'payment.method': result.method });

  return result;
});
// services/recommendation-service.ts — inline custom spans
import { trace, SpanStatusCode } from '@opentelemetry/api';

const tracer = trace.getTracer('recommendation-service');

export async function getRecommendations(userId: string, limit: number): Promise<Product[]> {
  return tracer.startActiveSpan('recommendations.generate', async (span) => {
    span.setAttribute('user.id', userId);
    span.setAttribute('recommendations.limit', limit);

    // Step 1: Get user profile
    const profile = await tracer.startActiveSpan(
      'recommendations.loadProfile',
      async (profileSpan) => {
        const result = await userService.getProfile(userId);
        profileSpan.setAttribute('user.segment', result.segment);
        profileSpan.end();
        return result;
      }
    );

    // Step 2: Score candidates
    const scored = await tracer.startActiveSpan('recommendations.score', async (scoreSpan) => {
      const candidates = await productService.getCandidates(profile.segment);
      scoreSpan.setAttribute('candidates.count', candidates.length);
      const results = await mlService.score(candidates, profile);
      scoreSpan.addEvent('scoring.complete', { top_score: results[0]?.score ?? 0 });
      scoreSpan.end();
      return results;
    });

    span.setAttribute('recommendations.returned', Math.min(scored.length, limit));
    span.end();
    return scored.slice(0, limit);
  });
}

Details

API vs SDK separation: Application code imports @opentelemetry/api (zero-cost no-op if no SDK is registered). The SDK is configured once in instrumentation.ts. This means your business code has no dependency on the SDK — if OpenTelemetry is not initialized, spans are silently discarded.

Getting the active span (without creating a new one):

import { trace } from '@opentelemetry/api';

function enrichSpan(orderId: string) {
  const span = trace.getActiveSpan();
  if (span) {
    span.setAttribute('order.id', orderId);
  }
}

Span links: Connect related traces that are not parent-child (e.g., a batch job processing items from different requests):

const span = tracer.startSpan('batch.process', {
  links: items.map((item) => ({
    context: item.spanContext,
    attributes: { 'batch.item_id': item.id },
  })),
});

What to instrument (rule of thumb):

Source

https://opentelemetry.io/docs/languages/js/instrumentation/

Process

  1. Read the instructions and examples in this document.
  2. Apply the patterns to your implementation, adapting to your specific context.
  3. Verify your implementation against the details and edge cases listed above.

Harness Integration

Success Criteria

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.