in sdk/monitor/monitor-opentelemetry/src/metrics/quickpulse/utils.ts [281:394]
function getDependencyData(span: ReadableSpan): DependencyData {
const dependencyData: DependencyData = {
Target: "",
Duration: hrTimeToMilliseconds(span.duration),
Success: span.status.code !== SpanStatusCode.ERROR,
Name: span.name,
ResultCode: 0,
Type: "",
Data: "",
CustomDimensions: createCustomDimsFromAttributes(span.attributes),
};
if (span.kind === SpanKind.PRODUCER) {
dependencyData.Type = DependencyTypes.QueueMessage;
}
if (span.kind === SpanKind.INTERNAL && span.parentSpanId) {
dependencyData.Type = DependencyTypes.InProc;
}
const httpMethod = getHttpMethod(span.attributes);
const dbSystem = span.attributes[SEMATTRS_DB_SYSTEM];
const rpcSystem = span.attributes[SEMATTRS_RPC_SYSTEM];
// HTTP Dependency
if (httpMethod) {
const httpUrl = getHttpUrl(span.attributes);
if (httpUrl) {
if (URL.canParse(String(httpUrl))) {
const dependencyUrl = new URL(String(httpUrl));
dependencyData.Name = `${httpMethod} ${dependencyUrl.pathname}`;
} else {
Logger.getInstance().info("Dependency data sent to live metrics has no valid URL field.");
}
}
dependencyData.Type = DependencyTypes.Http;
dependencyData.Data = getUrl(span.attributes);
const httpStatusCode = getHttpStatusCode(span.attributes);
if (httpStatusCode) {
dependencyData.ResultCode = Number(httpStatusCode);
}
let target = getDependencyTarget(span.attributes);
if (target) {
try {
// Remove default port
const portRegex = new RegExp(/(https?)(:\/\/.*)(:\d+)(\S*)/);
const res = portRegex.exec(target);
if (res !== null) {
const protocol = res[1];
const port = res[3];
if (
(protocol === "https" && port === ":443") ||
(protocol === "http" && port === ":80")
) {
// Drop port
target = res[1] + res[2] + res[4];
}
}
} catch (ex: any) {
/* no-op */
}
dependencyData.Target = `${target}`;
}
}
// DB Dependency
else if (dbSystem) {
// TODO: Remove special logic when Azure UX supports OpenTelemetry dbSystem
if (String(dbSystem) === DBSYSTEMVALUES_MYSQL) {
dependencyData.Type = DependencyTypes.mysql;
} else if (String(dbSystem) === DBSYSTEMVALUES_POSTGRESQL) {
dependencyData.Type = DependencyTypes.postgresql;
} else if (String(dbSystem) === DBSYSTEMVALUES_MONGODB) {
dependencyData.Type = DependencyTypes.mongodb;
} else if (String(dbSystem) === DBSYSTEMVALUES_REDIS) {
dependencyData.Type = DependencyTypes.redis;
} else if (isSqlDB(String(dbSystem))) {
dependencyData.Type = DependencyTypes.Sql;
} else {
dependencyData.Type = String(dbSystem);
}
const dbStatement = span.attributes[SEMATTRS_DB_STATEMENT];
const dbOperation = span.attributes[SEMATTRS_DB_OPERATION];
if (dbStatement) {
dependencyData.Data = String(dbStatement);
} else if (dbOperation) {
dependencyData.Data = String(dbOperation);
}
const target = getDependencyTarget(span.attributes);
const dbName = span.attributes[SEMATTRS_DB_NAME];
if (target) {
dependencyData.Target = dbName ? `${target}|${dbName}` : `${target}`;
} else {
dependencyData.Target = dbName ? `${dbName}` : `${dbSystem}`;
}
}
// grpc Dependency
else if (rpcSystem) {
if (rpcSystem === DependencyTypes.Wcf) {
dependencyData.Type = DependencyTypes.Wcf;
} else {
dependencyData.Type = DependencyTypes.Grpc;
}
const grpcStatusCode = span.attributes[SEMATTRS_RPC_GRPC_STATUS_CODE];
if (grpcStatusCode) {
dependencyData.ResultCode = Number(grpcStatusCode);
}
const target = getDependencyTarget(span.attributes);
if (target) {
dependencyData.Target = `${target}`;
} else if (rpcSystem) {
dependencyData.Target = String(rpcSystem);
}
}
return dependencyData;
}