Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions packages/node-opentelemetry/src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@ import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import {
DEFAULT_OTEL_METRIC_EXPORT_INTERVAL,
DEFAULT_OTEL_METRIC_EXPORT_TIMEOUT,
DEFAULT_OTEL_METRICS_EXPORTER_URL,
} from './constants';

const env = process.env;

export const getHyperDXMetricReader = () =>
export const getHyperDXMetricReader = (url: string) =>
new PeriodicExportingMetricReader({
exporter:
env.OTEL_EXPORTER_OTLP_PROTOCOL === 'grpc'
? new OTLPMetricExporterGRPC({
url: DEFAULT_OTEL_METRICS_EXPORTER_URL,
})
: new OTLPMetricExporterHTTP({
url: DEFAULT_OTEL_METRICS_EXPORTER_URL,
}),
? new OTLPMetricExporterGRPC({ url })
: new OTLPMetricExporterHTTP({ url }),
exportIntervalMillis: DEFAULT_OTEL_METRIC_EXPORT_INTERVAL,
exportTimeoutMillis: DEFAULT_OTEL_METRIC_EXPORT_TIMEOUT,
});
25 changes: 17 additions & 8 deletions packages/node-opentelemetry/src/otel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
DEFAULT_OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
DEFAULT_OTEL_LOG_LEVEL,
DEFAULT_OTEL_LOGS_EXPORTER,
DEFAULT_OTEL_LOGS_EXPORTER_URL,
DEFAULT_OTEL_METRICS_EXPORTER,
DEFAULT_OTEL_METRICS_EXPORTER_URL,
DEFAULT_OTEL_TRACES_EXPORTER,
Expand Down Expand Up @@ -77,6 +78,7 @@ export type SDKConfig = {
sentryIntegrationEnabled?: boolean;
service?: string;
stopOnTerminationSignals?: boolean;
url?: string;
};

const setOtelEnvs = ({
Expand Down Expand Up @@ -193,6 +195,12 @@ export const initSDK = (config: SDKConfig) => {
config.enableInternalProfiling ?? false;
const defaultServiceName = config.service ?? DEFAULT_SERVICE_NAME();

// Override OTLP endpoint URLs if config.url is provided
const otlpEndpoint = config.url ?? env.OTEL_EXPORTER_OTLP_ENDPOINT;
const tracesUrl = otlpEndpoint ? `${otlpEndpoint}/v1/traces` : DEFAULT_OTEL_TRACES_EXPORTER_URL;
const logsUrl = otlpEndpoint ? `${otlpEndpoint}/v1/logs` : DEFAULT_OTEL_LOGS_EXPORTER_URL;
const metricsUrl = otlpEndpoint ? `${otlpEndpoint}/v1/metrics` : DEFAULT_OTEL_METRICS_EXPORTER_URL;

ui.succeed(`Service name is configured to be "${defaultServiceName}"`);

if (!env.OTEL_EXPORTER_OTLP_HEADERS && !defaultApiKey) {
Expand Down Expand Up @@ -242,6 +250,7 @@ export const initSDK = (config: SDKConfig) => {
//--------------------------------------------------
ui.text = 'Initializing OpenTelemetry Logger...';
const _logger = new OtelLogger({
baseUrl: logsUrl,
detectResources: defaultDetectResources,
service: defaultServiceName,
});
Expand All @@ -250,7 +259,7 @@ export const initSDK = (config: SDKConfig) => {

// Health check
Promise.all([
healthCheckUrl(ui, DEFAULT_OTEL_TRACES_EXPORTER_URL, {
healthCheckUrl(ui, tracesUrl, {
method: 'POST',
headers: healthCheckHeaders,
body: JSON.stringify({}),
Expand All @@ -260,7 +269,7 @@ export const initSDK = (config: SDKConfig) => {
headers: healthCheckHeaders,
body: JSON.stringify({}),
}),
healthCheckUrl(ui, DEFAULT_OTEL_METRICS_EXPORTER_URL, {
healthCheckUrl(ui, metricsUrl, {
method: 'POST',
headers: healthCheckHeaders,
body: JSON.stringify({}),
Expand Down Expand Up @@ -342,7 +351,7 @@ export const initSDK = (config: SDKConfig) => {
logRecordProcessor: defaultDisableLogs ? undefined : _logger.getProcessor(),
metricReader:
config.metricReader ??
(defaultDisableMetrics ? undefined : getHyperDXMetricReader()),
(defaultDisableMetrics ? undefined : getHyperDXMetricReader(metricsUrl)),
spanProcessors: [
...(defaultDisableTracing
? []
Expand All @@ -352,11 +361,11 @@ export const initSDK = (config: SDKConfig) => {
env.OTEL_EXPORTER_OTLP_PROTOCOL === 'grpc'
? new OTLPTraceExporterGRPC({
timeoutMillis: DEFAULT_OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
url: DEFAULT_OTEL_TRACES_EXPORTER_URL,
url: tracesUrl,
})
: new OTLPTraceExporterHTTP({
timeoutMillis: DEFAULT_OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
url: DEFAULT_OTEL_TRACES_EXPORTER_URL,
url: tracesUrl,
}),
enableHDXGlobalContext: defaultBetaMode,
contextManager,
Expand Down Expand Up @@ -606,17 +615,17 @@ export const initSDK = (config: SDKConfig) => {
if (defaultDisableLogs) {
ui.warn('Logs are disabled');
} else {
ui.succeed(`Sending logs to "${_logger.getExporterUrl()}"`);
ui.succeed(`Sending logs to "${logsUrl}"`);
}
if (defaultDisableMetrics) {
ui.warn('Metrics are disabled');
} else {
ui.succeed(`Sending metrics to "${DEFAULT_OTEL_METRICS_EXPORTER_URL}"`);
ui.succeed(`Sending metrics to "${metricsUrl}"`);
}
if (defaultDisableTracing) {
ui.warn('Tracing is disabled');
} else {
ui.succeed(`Sending traces to "${DEFAULT_OTEL_TRACES_EXPORTER_URL}"`);
ui.succeed(`Sending traces to "${tracesUrl}"`);
}

ui.stopAndPersist({
Expand Down