openlit
Version:
OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications, facilitating the integration of observability into your GenAI-driven projects
143 lines • 6.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.injectAdditionalAttributes = exports.usingAttributes = exports.Openlit = void 0;
const resources_1 = require("@opentelemetry/resources");
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
const api_1 = require("@opentelemetry/api");
const tracing_1 = __importDefault(require("./otel/tracing"));
const events_1 = __importDefault(require("./otel/events"));
const constant_1 = require("./constant");
const base_1 = __importDefault(require("./features/base"));
const config_1 = __importDefault(require("./config"));
const helpers_1 = __importDefault(require("./helpers"));
const helpers_2 = require("./helpers");
Object.defineProperty(exports, "usingAttributes", { enumerable: true, get: function () { return helpers_2.usingAttributes; } });
Object.defineProperty(exports, "injectAdditionalAttributes", { enumerable: true, get: function () { return helpers_2.injectAdditionalAttributes; } });
const evals_1 = require("./evals");
const metrics_1 = __importDefault(require("./otel/metrics"));
const semantic_convention_1 = __importDefault(require("./semantic-convention"));
const prompt_injection_1 = require("./guard/prompt-injection");
const sensitive_topic_1 = require("./guard/sensitive-topic");
const topic_restriction_1 = require("./guard/topic-restriction");
const all_1 = require("./guard/all");
const utils_1 = require("./otel/utils");
const evals = {
Hallucination: (options) => new evals_1.Hallucination(options),
Bias: (options) => new evals_1.Bias(options),
Toxicity: (options) => new evals_1.Toxicity(options),
All: (options) => new evals_1.All(options),
};
const guard = {
PromptInjection: (options) => new prompt_injection_1.PromptInjection(options),
SensitiveTopic: (options) => new sensitive_topic_1.SensitiveTopic(options),
TopicRestriction: (options) => new topic_restriction_1.TopicRestriction(options),
All: (options) => new all_1.All(options),
};
/**
* Resolve OpenlitOptions into a single ResolvedOptions object.
* Precedence: arg > env var > default.
*/
function resolveOptions(options) {
const o = options || {};
const environment = o.environment ?? constant_1.DEFAULT_ENVIRONMENT;
const applicationName = o.applicationName ?? constant_1.DEFAULT_APPLICATION_NAME;
const rawEndpoint = o.otlpEndpoint ??
process.env.OTEL_EXPORTER_OTLP_ENDPOINT ??
undefined;
const otlpEndpoint = rawEndpoint
? rawEndpoint.replace(/\/v1\/traces$/, '')
: undefined;
let otlpHeaders = o.otlpHeaders ?? undefined;
if (!otlpHeaders && process.env.OTEL_EXPORTER_OTLP_HEADERS) {
otlpHeaders = process.env.OTEL_EXPORTER_OTLP_HEADERS.split(',').reduce((acc, item) => {
const keyVal = item.split('=');
acc[keyVal[0]] = keyVal[1];
return acc;
}, {});
}
if (!otlpHeaders)
otlpHeaders = {};
let disableBatch = o.disableBatch ?? undefined;
const envDisableBatch = (0, utils_1.parseBoolEnv)('OPENLIT_DISABLE_BATCH');
if (disableBatch === undefined) {
disableBatch = envDisableBatch ?? false;
}
let captureMessageContent = o.captureMessageContent ?? undefined;
const envCapture = (0, utils_1.parseBoolEnv)('OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT');
if (captureMessageContent === undefined) {
captureMessageContent = envCapture ?? true;
}
let disableMetrics = o.disableMetrics ?? undefined;
const envDisableMetrics = (0, utils_1.parseBoolEnv)('OPENLIT_DISABLE_METRICS');
if (disableMetrics === undefined) {
disableMetrics = envDisableMetrics ?? false;
}
let disableEvents = o.disableEvents ?? undefined;
const envDisableEvents = (0, utils_1.parseBoolEnv)('OPENLIT_DISABLE_EVENTS');
if (disableEvents === undefined) {
disableEvents = envDisableEvents ?? false;
}
return {
environment,
applicationName,
tracer: o.tracer,
otlpEndpoint,
otlpHeaders,
disableBatch,
captureMessageContent,
disabledInstrumentors: o.disabledInstrumentors,
instrumentations: o.instrumentations,
disableMetrics,
disableEvents,
pricingJson: o.pricingJson,
maxContentLength: o.maxContentLength ?? null,
customSpanAttributes: o.customSpanAttributes ?? null,
};
}
class Openlit extends base_1.default {
static init(options) {
try {
// Enable OTel diagnostic logging so exporter errors (connection refused,
// 404, timeouts) are surfaced to the user — matches Python SDK behavior.
api_1.diag.setLogger(new api_1.DiagConsoleLogger(), api_1.DiagLogLevel.WARN);
const resolved = resolveOptions(options);
this.options = resolved;
this.resource = (0, resources_1.resourceFromAttributes)({
[semantic_conventions_1.ATTR_SERVICE_NAME]: resolved.applicationName,
[semantic_convention_1.default.ATTR_DEPLOYMENT_ENVIRONMENT]: resolved.environment,
[semantic_conventions_1.ATTR_TELEMETRY_SDK_NAME]: constant_1.SDK_NAME,
});
const setupBase = {
...resolved,
resource: this.resource,
};
tracing_1.default.setup(setupBase);
if (!resolved.disableEvents) {
events_1.default.setup(setupBase);
}
if (!resolved.disableMetrics) {
const exportIntervalMillis = Number(process.env.OTEL_EXPORTER_OTLP_METRICS_EXPORT_INTERVAL ?? 60000) || 60000;
metrics_1.default.setup({
...setupBase,
exportIntervalMillis,
});
}
// Fetch pricing info once and cache — matches Python SDK behavior.
helpers_1.default.fetchPricingInfo(resolved.pricingJson).then((info) => { config_1.default.pricingInfo = info; }, () => { config_1.default.pricingInfo = {}; });
}
catch (e) {
console.log('Connection time out', e);
}
}
}
exports.Openlit = Openlit;
Openlit.evals = evals;
Openlit.guard = guard;
const openlit = Openlit;
openlit.usingAttributes = helpers_2.usingAttributes;
openlit.injectAdditionalAttributes = helpers_2.injectAdditionalAttributes;
exports.default = openlit;
//# sourceMappingURL=index.js.map