@splunk/otel
Version:
The Splunk distribution of OpenTelemetry Node Instrumentation provides a Node agent that automatically instruments your Node application to capture and report distributed traces to Splunk APM.
101 lines • 3.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultDbStatementSerializer = void 0;
exports.getIndexName = getIndexName;
exports.startSpan = startSpan;
exports.normalizeArguments = normalizeArguments;
exports.getPort = getPort;
exports.getNetAttributes = getNetAttributes;
exports.onResponse = onResponse;
exports.onError = onError;
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const api_1 = require("@opentelemetry/api");
const instrumentation_1 = require("@opentelemetry/instrumentation");
const semantic_conventions_1 = require("@opentelemetry/semantic-conventions");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getIndexName(params) {
if (!(params === null || params === void 0 ? void 0 : params.index)) {
return undefined;
}
if (typeof params.index === 'string') {
return params.index;
}
if (Array.isArray(params.index)) {
return params.index.join(',');
}
}
function startSpan({ tracer, attributes }) {
return tracer.startSpan('elasticsearch.request', {
kind: api_1.SpanKind.CLIENT,
attributes: Object.assign({ [semantic_conventions_1.SemanticAttributes.DB_SYSTEM]: 'elasticsearch' }, attributes),
});
}
function normalizeArguments(params, options, callback) {
// Copied normalizeArguments function from @elastic/elasticsearch
if (typeof options === 'function') {
callback = options;
options = {};
}
if (typeof params === 'function' || params === undefined || params === null) {
callback = params;
params = {};
options = {};
}
return [params, options, callback];
}
function getPort(port, protocol) {
if (port)
return port;
if (protocol === 'https:')
return '443';
if (protocol === 'http:')
return '80';
return '';
}
function getNetAttributes(url) {
const { port, protocol, hostname } = new URL(url);
return {
[semantic_conventions_1.SemanticAttributes.NET_TRANSPORT]: 'IP.TCP',
[semantic_conventions_1.SemanticAttributes.NET_PEER_NAME]: hostname,
[semantic_conventions_1.SemanticAttributes.NET_PEER_PORT]: getPort(port, protocol),
};
}
function onResponse(span, result, responseHook) {
span.setAttributes(Object.assign({}, getNetAttributes(result.meta.connection.url.toString())));
span.setStatus({
code: api_1.SpanStatusCode.OK,
});
if (responseHook) {
(0, instrumentation_1.safeExecuteInTheMiddle)(() => responseHook(span, result), (e) => {
if (e) {
api_1.diag.error('elasticsearch instrumentation: responseHook error', e);
}
}, true);
}
span.end();
}
function onError(span, err) {
span.recordException(err);
span.setStatus({
code: api_1.SpanStatusCode.ERROR,
message: err.message,
});
span.end();
}
const defaultDbStatementSerializer = (operation, params, options) => JSON.stringify({ params, options });
exports.defaultDbStatementSerializer = defaultDbStatementSerializer;
//# sourceMappingURL=utils.js.map