Skip to content

Commit

Permalink
refactor: split global instrumentation into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
esroyo committed Oct 17, 2024
1 parent a1daea4 commit df3ea30
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 72 deletions.
78 changes: 78 additions & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type { Config } from './types.ts';
import { BatchTracedSpanProcessor } from '@esroyo/otel-batch-traced-span-processor';
import { ServerTimingSpanExporter } from '@esroyo/otel-server-timing-span-exporter';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import { Resource } from '@opentelemetry/resources';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { loadSync as dotenvLoad } from '@std/dotenv';
import { CustomTracerProvider } from './custom-tracer-provider.ts';
import { CustomOTLPTraceExporter } from './custom-otlp-trace-exporter.ts';
import { sanitizeBasePath, sanitizeUpstreamOrigin } from './utils.ts';

// Step: resolve config
dotenvLoad({ export: true });

export const config: Config = {
BASE_PATH: sanitizeBasePath(Deno.env.get('BASE_PATH') ?? '/'),
CACHE: Deno.env.get('CACHE') === 'true',
CACHE_CONN_MAX: Number(Deno.env.get('CACHE_CONN_MAX')) || 20,
CACHE_CONN_MIN: Number(Deno.env.get('CACHE_CONN_MIN')) || 2,
CACHE_REDIRECT: Number(Deno.env.get('CACHE_REDIRECT') as string) || 600,
CACHE_CLIENT_REDIRECT:
Number(Deno.env.get('CACHE_CLIENT_REDIRECT') as string) || 600,
CACHE_REDIS_HOSTNAME: Deno.env.get('CACHE_REDIS_HOSTNAME') ?? '',
DD_TRACE_ENABLED: Deno.env.get('DD_TRACE_ENABLED') === 'true', // default false
HOMEPAGE: Deno.env.get('HOMEPAGE') ?? '',
OUTPUT_BANNER: Deno.env.get('OUTPUT_BANNER') ?? '',
REDIRECT_FASTPATH: Deno.env.get('REDIRECT_FASTPATH') !== 'false', // default true
UPSTREAM_ORIGIN: sanitizeUpstreamOrigin(
Deno.env.get('UPSTREAM_ORIGIN') ?? 'https://esm.sh',
),
WORKER_ENABLE: Deno.env.get('WORKER_ENABLE') === 'true', // default false
};

const isMainProcess = 'Window' in globalThis;

if (isMainProcess) {
console.log(`Config:`, config);
}

// Step: minimal tracing setup
const provider = new CustomTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]:
Deno.env.get('ENV') ??
'dev',
[SemanticResourceAttributes.SERVICE_NAME]: 'systemjs',
[SemanticResourceAttributes.SERVICE_VERSION]:
Deno.env.get('DEPLOYMENT_TAG') ?? undefined,
[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]: 'javascript',
}),
});
provider.addSpanProcessor(
new SimpleSpanProcessor(new ServerTimingSpanExporter()),
);
provider.register({
contextManager: new AsyncLocalStorageContextManager(),
});

// Step: OTLP tracing setup
if (config.DD_TRACE_ENABLED) {
const collectorOptions = {
// url is optional and can be omitted - default is http://localhost:4318/v1/traces
url: `http://${
Deno.env.get('DD_AGENT_HOST') ?? 'localhost'
}:4318/v1/traces`,
// an optional object containing custom headers to be sent with each request will only work with http
headers: {},
// an optional limit on pending requests
concurrencyLimit: 10,
};
const otlpExporter = new CustomOTLPTraceExporter(collectorOptions);
const otelProcessor = new BatchTracedSpanProcessor(otlpExporter);
provider.addSpanProcessor(otelProcessor);
if (isMainProcess) {
console.log('OTLP options:', collectorOptions);
}
}
73 changes: 1 addition & 72 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,8 @@
import type { Config } from './types.ts';
import { BatchTracedSpanProcessor } from '@esroyo/otel-batch-traced-span-processor';
import { ServerTimingSpanExporter } from '@esroyo/otel-server-timing-span-exporter';
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
import { Resource } from '@opentelemetry/resources';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { loadSync as dotenvLoad } from '@std/dotenv';
import { config } from './global.ts'; // First always, and only once per process
import { createCachePool } from './create-cache-pool.ts';
import { createRequestHandler } from './create-request-handler.ts';
import { CustomTracerProvider } from './custom-tracer-provider.ts';
import { CustomOTLPTraceExporter } from './custom-otlp-trace-exporter.ts';
import { instrumentRequestHandler } from './instrument-request-handler.ts';
import { sanitizeBasePath, sanitizeUpstreamOrigin } from './utils.ts';

// Step: resolve config
dotenvLoad({ export: true });

const config: Config = {
BASE_PATH: sanitizeBasePath(Deno.env.get('BASE_PATH') ?? '/'),
CACHE: Deno.env.get('CACHE') === 'true',
CACHE_CONN_MAX: Number(Deno.env.get('CACHE_CONN_MAX')) || 20,
CACHE_CONN_MIN: Number(Deno.env.get('CACHE_CONN_MIN')) || 2,
CACHE_REDIRECT: Number(Deno.env.get('CACHE_REDIRECT') as string) || 600,
CACHE_CLIENT_REDIRECT:
Number(Deno.env.get('CACHE_CLIENT_REDIRECT') as string) || 600,
CACHE_REDIS_HOSTNAME: Deno.env.get('CACHE_REDIS_HOSTNAME') ?? '',
DD_TRACE_ENABLED: Deno.env.get('DD_TRACE_ENABLED') === 'true', // default false
HOMEPAGE: Deno.env.get('HOMEPAGE') ?? '',
OUTPUT_BANNER: Deno.env.get('OUTPUT_BANNER') ?? '',
REDIRECT_FASTPATH: Deno.env.get('REDIRECT_FASTPATH') !== 'false', // default true
UPSTREAM_ORIGIN: sanitizeUpstreamOrigin(
Deno.env.get('UPSTREAM_ORIGIN') ?? 'https://esm.sh',
),
WORKER_ENABLE: Deno.env.get('WORKER_ENABLE') === 'true', // default false
};
console.log('Config:', config);

// Step: minimal tracing setup
const provider = new CustomTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]:
Deno.env.get('ENV') ??
'dev',
[SemanticResourceAttributes.SERVICE_NAME]: 'systemjs',
[SemanticResourceAttributes.SERVICE_VERSION]:
Deno.env.get('DEPLOYMENT_TAG') ?? undefined,
[SemanticResourceAttributes.TELEMETRY_SDK_LANGUAGE]: 'javascript',
}),
});
provider.addSpanProcessor(
new SimpleSpanProcessor(new ServerTimingSpanExporter()),
);
provider.register({
contextManager: new AsyncLocalStorageContextManager(),
});

// Step: OTLP tracing setup
if (config.DD_TRACE_ENABLED) {
const collectorOptions = {
// url is optional and can be omitted - default is http://localhost:4318/v1/traces
url: `http://${
Deno.env.get('DD_AGENT_HOST') ?? 'localhost'
}:4318/v1/traces`,
// an optional object containing custom headers to be sent with each request will only work with http
headers: {},
// an optional limit on pending requests
concurrencyLimit: 10,
};
const otlpExporter = new CustomOTLPTraceExporter(collectorOptions);
const otelProcessor = new BatchTracedSpanProcessor(otlpExporter);
provider.addSpanProcessor(otelProcessor);
console.log('OTLP options:', collectorOptions);
}

// Step: cache service
const cache = createCachePool(config);

Deno.serve(
Expand Down

0 comments on commit df3ea30

Please sign in to comment.