API Reference

Complete documentation for all StackSleuth APIs and methods

ProfilerCore

The core profiling class that provides the foundation for all StackSleuth agents.

Constructor

new ProfilerCore(config: ProfilerConfig)

Parameters

Name Type Required Description
config ProfilerConfig Yes Configuration object for the profiler
config.apiKey string Yes Your StackSleuth API key
config.environment string No Environment name (default: 'production')
config.enabled boolean No Enable/disable profiling (default: true)

Example

const profiler = new ProfilerCore({
  apiKey: 'your-api-key',
  environment: 'production',
  enabled: process.env.NODE_ENV === 'production'
});

startProfiling()

startProfiling(sessionId?: string): void

Starts a new profiling session.

Parameters

Name Type Required Description
sessionId string No Optional custom session ID

stopProfiling()

stopProfiling(): Promise<ProfileReport>

Stops the current profiling session and returns the collected data.

Returns

A promise that resolves to a ProfileReport object containing all collected metrics.

Example

const report = await profiler.stopProfiling();
console.log('Total duration:', report.duration);
console.log('Metrics collected:', report.metrics.length);

captureMetric()

captureMetric(name: string, value: number, metadata?: object): void

Captures a custom metric.

Parameters

Name Type Required Description
name string Yes Metric name (e.g., 'api.response.time')
value number Yes Metric value
metadata object No Additional metadata for the metric

Metrics API

Methods for working with performance metrics.

getMetrics()

getMetrics(filter?: MetricFilter): Metric[]

Retrieves collected metrics with optional filtering.

Example

// Get all metrics
const allMetrics = profiler.getMetrics();

// Get metrics by type
const apiMetrics = profiler.getMetrics({ type: 'api' });

// Get metrics within time range
const recentMetrics = profiler.getMetrics({
  startTime: Date.now() - 60000,
  endTime: Date.now()
});

Events API

Event handling and custom event tracking.

on()

on(event: string, handler: Function): void

Subscribes to profiler events.

Available Events

  • metric:captured - Fired when a metric is captured
  • session:start - Fired when profiling starts
  • session:end - Fired when profiling ends
  • error - Fired on profiler errors

Example

profiler.on('metric:captured', (metric) => {
  console.log('New metric:', metric.name, metric.value);
});

profiler.on('error', (error) => {
  console.error('Profiler error:', error);
});

React Agent API

React-specific profiling methods and components.

ReactAgent.init()

ReactAgent.init(config: ReactAgentConfig): void

Initializes the React profiling agent.

Configuration Options

Option Type Default Description
trackRenders boolean true Track component render cycles
trackStateChanges boolean true Track state updates
trackEffects boolean true Track useEffect executions
componentWhitelist string[] [] Components to specifically track

useProfiler Hook

useProfiler(componentName: string): ProfilerHook

React hook for component-level profiling.

Example

function MyComponent() {
  const profiler = useProfiler('MyComponent');
  
  const handleClick = () => {
    profiler.startMeasure('click-handler');
    // ... expensive operation
    profiler.endMeasure('click-handler');
  };
  
  return <button onClick={handleClick}>Click me</button>;
}

Database Agent APIs

APIs for database performance monitoring.

RedisAgent

new RedisAgent(config: AgentConfig)

Methods

instrument()
instrument(client: RedisClient): void

Instruments a Redis client for automatic tracking.

getSlowQueries()
getSlowQueries(threshold?: number): Query[]

Returns queries that exceeded the specified threshold (default: 100ms).

TypeScript Types

Type definitions for TypeScript users.

Core Types

interface ProfilerConfig {
  apiKey: string;
  environment?: string;
  enabled?: boolean;
  endpoint?: string;
  batchSize?: number;
  flushInterval?: number;
}

interface Metric {
  name: string;
  value: number;
  timestamp: number;
  metadata?: Record<string, any>;
}

interface ProfileReport {
  sessionId: string;
  startTime: number;
  endTime: number;
  duration: number;
  metrics: Metric[];
  metadata: Record<string, any>;
}