Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic template literal type for OTel agent names #3

Closed
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
30 changes: 3 additions & 27 deletions packages/kbn-elastic-agent-utils/src/agent_names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export interface MappedTypes {
mappedTypeWithOneInlineProp: {
[key in 'prop3']: number;
};
mappedTypeWithLiteralTemplates: {
[key in MappedTypeProps | `templated_prop/${string}`]: number;
};
}

export type RecordWithKnownProps = Record<MappedTypeProps, number>;
Expand Down
7 changes: 7 additions & 0 deletions packages/kbn-telemetry-tools/src/tools/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
},
});
});

Expand Down
11 changes: 8 additions & 3 deletions packages/kbn-telemetry-tools/src/tools/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<string, unknown>)[c] = descriptor;
acc[c] = descriptor;
return acc;
}, {});
}, {} as Record<string, unknown>);
}
}
return { '@@INDEX@@': descriptor };
Expand Down