Skip to content
Draft
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
2 changes: 0 additions & 2 deletions packages/core/src/otlpExporter/common/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class OtlpConfigContext {
/** @type {any} */
this._compiledSemConv = null;
/** @type {string | null} */
this._hostId = null;
/** @type {string | null} */
this._pid = null;
/** @type {string | null} */
this._serviceName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const MAPPINGS = {
SDK_VERSION: 'telemetry.sdk.version',
HOST_NAME: 'host.name',
HOST_ID: 'host.id',
PROCESS_PID: 'process.pid'
PROCESS_PID: 'process.pid',
OS_TYPE: 'os.type',
FAAS_NAME: 'faas.name'
},

metadata: {
Expand Down
77 changes: 69 additions & 8 deletions packages/core/src/otlpExporter/common/transformers/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
const os = require('os');
const ctx = require('../context');
const { INSTRUMENTATION_SCOPE_NAME } = require('../constants');
const { MAPPINGS } = require('../semconv/base/mappings');

const RESOURCE = MAPPINGS.resource;

let SDK_VERSION = '1.0.0';
try {
Expand All @@ -31,14 +34,27 @@ const INSTRUMENTATION_SCOPE = {
* @property {Record<string, any>} [f]
*/

/**
* Extracts faas.name from provider-specific span data fields.
*
* @param {RawPayload} rawPayload
* @returns {string | undefined}
*/
function getFaasNameFromSpanData(rawPayload) {
return (
// AWS Lambda
rawPayload.data?.lambda?.functionName
);
}

const resourceMapper = {
/**
* @param {RawPayload} rawPayload
* @returns {string | undefined}
*/
serviceName(rawPayload) {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
return resource['service.name'] || ctx.serviceName;
return resource[RESOURCE.SERVICE_NAME] || ctx.serviceName;
},

/**
Expand All @@ -47,7 +63,7 @@ const resourceMapper = {
*/
sdkLanguage(rawPayload) {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
return resource['telemetry.sdk.language'] || SDK_LANGUAGE;
return resource[RESOURCE.SDK_LANGUAGE] || SDK_LANGUAGE;
},

/**
Expand All @@ -56,7 +72,7 @@ const resourceMapper = {
*/
sdkName(rawPayload) {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
return resource['telemetry.sdk.name'] || SDK_NAME;
return resource[RESOURCE.SDK_NAME] || SDK_NAME;
},

/**
Expand All @@ -65,7 +81,7 @@ const resourceMapper = {
*/
sdkVersion(rawPayload) {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
return resource['telemetry.sdk.version'] || SDK_VERSION;
return resource[RESOURCE.SDK_VERSION] || SDK_VERSION;
},

/**
Expand All @@ -76,7 +92,7 @@ const resourceMapper = {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
const metadata = rawPayload.f || {};

const pid = resource['process.pid'] || metadata.e || ctx._pid;
const pid = resource[RESOURCE.PROCESS_PID] || metadata.e || ctx._pid;

if (pid === null || pid === undefined) {
return undefined;
Expand All @@ -92,7 +108,7 @@ const resourceMapper = {
*/
hostName(rawPayload) {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
let hostName = resource['host.name'];
let hostName = resource[RESOURCE.HOST_NAME];

if (!hostName) {
try {
Expand All @@ -114,9 +130,30 @@ const resourceMapper = {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
const metadata = rawPayload.f || {};

const hostId = resource['host.id'] || metadata.h || ctx._hostId;
return resource[RESOURCE.HOST_ID] || metadata.h;
},

return typeof hostId === 'string' ? hostId : undefined;
/**
* @param {RawPayload} rawPayload
* @returns {string | undefined}
*/
faasName(rawPayload) {
const resource = rawPayload.data?.resource || rawPayload.resource || {};
return resource[RESOURCE.FAAS_NAME] || getFaasNameFromSpanData(rawPayload) || undefined;
},

/**
* @param {RawPayload} rawPayload
* @returns {string | undefined}
*/
osType(rawPayload) {
const resource = rawPayload.data?.resource || rawPayload.resource || {};

if (resource[RESOURCE.OS_TYPE]) {
return resource[RESOURCE.OS_TYPE];
}

return normalizeOsType(os.platform());
}
};

Expand Down Expand Up @@ -157,10 +194,20 @@ function extractResourceAttributes(rawPayload) {
transform: resourceMapper.processId,
valueType: 'int'
},
{
otlp: OTLP.resource.OS_TYPE,
transform: resourceMapper.osType,
valueType: 'string'
},
{
otlp: OTLP.resource.HOST_NAME,
transform: resourceMapper.hostName,
valueType: 'string'
},
{
otlp: OTLP.resource.FAAS_NAME,
transform: resourceMapper.faasName,
valueType: 'string'
}
];

Expand All @@ -182,6 +229,20 @@ function extractResourceAttributes(rawPayload) {
return { attributes };
}

/**
* @param {string} nodePlatform
*/
function normalizeOsType(nodePlatform) {
switch (nodePlatform) {
case 'win32':
return 'windows';
case 'sunos':
return 'solaris';
default:
return nodePlatform;
}
}

module.exports = {
extractResourceAttributes,
INSTRUMENTATION_SCOPE
Expand Down
12 changes: 1 addition & 11 deletions packages/core/src/otlpExporter/metrics/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@

'use strict';

/**
* @param {Record<string, any>} from
* @returns {string}
*/
function getResourceKey(from) {
if (!from) return 'h:empty|e:empty';
return `h:${from.h || 'empty'}|e:${from.e || 'empty'}`;
}

/**
* @param {Record<string, any>} obj
* @param {string} [prefix]
Expand Down Expand Up @@ -90,6 +81,5 @@ function normalizeObject(metricsObj) {

module.exports = {
flattenObject,
normalizeMetrics,
getResourceKey
normalizeMetrics
};
Loading