From 35062d0fa224bbf4eb3675aa5191801b25d7e587 Mon Sep 17 00:00:00 2001 From: Milosz Marcinkowski Date: Tue, 8 Oct 2024 13:22:41 +0200 Subject: [PATCH 1/2] add generic template literal type for OTel agent names --- .../src/agent_names.ts | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/packages/kbn-elastic-agent-utils/src/agent_names.ts b/packages/kbn-elastic-agent-utils/src/agent_names.ts index 0405da9cf2193..44b29ef5fd558 100644 --- a/packages/kbn-elastic-agent-utils/src/agent_names.ts +++ b/packages/kbn-elastic-agent-utils/src/agent_names.ts @@ -38,34 +38,10 @@ export const ELASTIC_AGENT_NAMES: ElasticAgentName[] = [ ]; export type OpenTelemetryAgentName = - | 'otlp' | 'opentelemetry' - | 'opentelemetry/cpp' - | 'opentelemetry/dotnet' - | 'opentelemetry/erlang' - | 'opentelemetry/go' - | 'opentelemetry/java' - | 'opentelemetry/nodejs' - | 'opentelemetry/php' - | 'opentelemetry/python' - | 'opentelemetry/ruby' - | 'opentelemetry/rust' - | 'opentelemetry/swift' - | 'opentelemetry/android' - | 'opentelemetry/webjs' - | 'otlp/cpp' - | 'otlp/dotnet' - | 'otlp/erlang' - | 'otlp/go' - | 'otlp/java' - | 'otlp/nodejs' - | 'otlp/php' - | 'otlp/python' - | 'otlp/ruby' - | 'otlp/rust' - | 'otlp/swift' - | 'otlp/android' - | 'otlp/webjs'; + | 'otlp' + | `opentelemetry/${string}` + | `otlp/${string}`; export const OPEN_TELEMETRY_AGENT_NAMES: OpenTelemetryAgentName[] = [ 'otlp', 'opentelemetry', From f3f0ca81a6c7479daf5fe28da847387a9ebefd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Tue, 8 Oct 2024 17:53:19 +0200 Subject: [PATCH 2/2] Add support for LiteralTemplates in `@kbn/telemetry-tools` --- .../__fixture__/telemetry_collectors/constants.ts | 3 +++ .../kbn-telemetry-tools/src/tools/serializer.test.ts | 7 +++++++ packages/kbn-telemetry-tools/src/tools/serializer.ts | 11 ++++++++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts b/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts index c9b0cbfe0281a..ec8b935912cd7 100644 --- a/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts +++ b/packages/kbn-telemetry-tools/src/tools/__fixture__/telemetry_collectors/constants.ts @@ -60,6 +60,9 @@ export interface MappedTypes { mappedTypeWithOneInlineProp: { [key in 'prop3']: number; }; + mappedTypeWithLiteralTemplates: { + [key in MappedTypeProps | `templated_prop/${string}`]: number; + }; } export type RecordWithKnownProps = Record; diff --git a/packages/kbn-telemetry-tools/src/tools/serializer.test.ts b/packages/kbn-telemetry-tools/src/tools/serializer.test.ts index 4301ab30fd09e..86f897767a025 100644 --- a/packages/kbn-telemetry-tools/src/tools/serializer.test.ts +++ b/packages/kbn-telemetry-tools/src/tools/serializer.test.ts @@ -105,6 +105,13 @@ describe('getDescriptor', () => { mappedTypeWithOneInlineProp: { prop3: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, }, + mappedTypeWithLiteralTemplates: { + prop1: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, + prop2: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, + // ideally, it'd be `templated_prop/@@INDEX@@` to be more explicit. But we're going with the fuzzier approach + // for now as it may require more changes downstream that are not worth it. + '@@INDEX@@': { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' }, + }, }); }); diff --git a/packages/kbn-telemetry-tools/src/tools/serializer.ts b/packages/kbn-telemetry-tools/src/tools/serializer.ts index 2523bc246d2a8..0269dc446b4a0 100644 --- a/packages/kbn-telemetry-tools/src/tools/serializer.ts +++ b/packages/kbn-telemetry-tools/src/tools/serializer.ts @@ -99,10 +99,15 @@ export function getConstraints(node: ts.Node, program: ts.Program): any { return node.literal.text; } - if (ts.isStringLiteral(node)) { + if (ts.isStringLiteral(node) || ts.isStringLiteralLike(node)) { return node.text; } + // template literals such as `smth/${string}` + if (ts.isTemplateLiteralTypeNode(node) || ts.isTemplateExpression(node)) { + return '@@INDEX@@'; // just map it to any kind of string. We can enforce it further in the future if we see fit. + } + if (ts.isImportSpecifier(node) || ts.isExportSpecifier(node)) { const source = node.getSourceFile(); const importedModuleName = getModuleSpecifier(node); @@ -180,9 +185,9 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor | const constraintsArray = Array.isArray(constraints) ? constraints : [constraints]; if (typeof constraintsArray[0] === 'string') { return constraintsArray.reduce((acc, c) => { - (acc as Record)[c] = descriptor; + acc[c] = descriptor; return acc; - }, {}); + }, {} as Record); } } return { '@@INDEX@@': descriptor };