Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Apr 26, 2024
1 parent 73b5ab7 commit 4225a44
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 111 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,7 @@ export interface GetPluginsOptions {
appKey: string;
};
}

export interface GetPluginsOptionsWithCWD extends GetPluginsOptions {
cwd: string;
}
25 changes: 14 additions & 11 deletions packages/factory/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { GetPluginsOptions } from '@datadog/build-plugins-core/types';
import type {
GetPluginsOptions,
GetPluginsOptionsWithCWD,
} from '@datadog/build-plugins-core/types';
import type { OptionsWithTelemetryEnabled, TelemetryOptions } from '@dd/telemetry-plugins/types';
import {
getPlugins as getTelemetryPlugins,
Expand All @@ -12,22 +15,22 @@ export interface Options extends GetPluginsOptions {
[TELEMETRY_CONFIG_KEY]?: TelemetryOptions;
}

// This remains internal as we inject the cwd part only from here.
interface OptionsWithCWD extends Options, GetPluginsOptionsWithCWD {}

export const buildPluginFactory = (): UnpluginInstance<Options, true> => {
return createUnplugin((userOptions: Options, unpluginMetaContext: UnpluginContextMeta) => {
// Parse/Read/Use user configuration.
// Implement config overrides with environment variables.
if (userOptions) {
console.log('Got options', userOptions);
}
// TODO: Implement config overrides with environment variables.
const options: OptionsWithCWD = {
cwd: process.cwd(),
...userOptions,
};

const plugins: UnpluginOptions[] = [];

// Based on configuration add corresponding plugin.
if (
userOptions[TELEMETRY_CONFIG_KEY] &&
userOptions[TELEMETRY_CONFIG_KEY].disabled !== true
) {
plugins.push(...getTelemetryPlugins(userOptions as OptionsWithTelemetryEnabled));
if (options[TELEMETRY_CONFIG_KEY] && options[TELEMETRY_CONFIG_KEY].disabled !== true) {
plugins.push(...getTelemetryPlugins(options as OptionsWithTelemetryEnabled));
}

return plugins;
Expand Down
33 changes: 17 additions & 16 deletions packages/plugins/telemetry/src/common/aggregator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import type {
EsbuildStats,
} from '@datadog/build-plugins-core/types';

import type { Metric, MetricToSend, GetMetricsOptions } from '../types';
import { CONFIG_KEY } from '../constants';
import type { Metric, MetricToSend, OptionsWithTelemetryEnabled } from '../types';

import { getMetric } from './helpers';
import { getMetric, getOptionsDD } from './helpers';
import {
getGenerals,
getGeneralReport,
Expand All @@ -21,27 +22,27 @@ import {
import * as es from './metrics/esbuild';
import * as wp from './metrics/webpack';

const getWebpackMetrics = (statsJson: StatsJson, opts: GetMetricsOptions) => {
const getWebpackMetrics = (statsJson: StatsJson, cwd: string) => {
const metrics: Metric[] = [];
const indexed = wp.getIndexed(statsJson, opts.context);
metrics.push(...wp.getModules(statsJson, indexed, opts.context));
const indexed = wp.getIndexed(statsJson, cwd);
metrics.push(...wp.getModules(statsJson, indexed, cwd));
metrics.push(...wp.getChunks(statsJson, indexed));
metrics.push(...wp.getAssets(statsJson, indexed));
metrics.push(...wp.getEntries(statsJson, indexed));
return metrics;
};

const getEsbuildMetrics = (stats: EsbuildStats, opts: GetMetricsOptions) => {
const getEsbuildMetrics = (stats: EsbuildStats, cwd: string) => {
const metrics: Metric[] = [];
const indexed = es.getIndexed(stats, opts.context);
metrics.push(...es.getModules(stats, indexed, opts.context));
metrics.push(...es.getAssets(stats, indexed, opts.context));
metrics.push(...es.getEntries(stats, indexed, opts.context));
const indexed = es.getIndexed(stats, cwd);
metrics.push(...es.getModules(stats, indexed, cwd));
metrics.push(...es.getAssets(stats, indexed, cwd));
metrics.push(...es.getEntries(stats, indexed, cwd));
return metrics;
};

export const getMetrics = (
opts: GetMetricsOptions,
opts: OptionsWithTelemetryEnabled,
report: Report,
bundler: BundlerStats,
): MetricToSend[] => {
Expand All @@ -65,26 +66,26 @@ export const getMetrics = (

if (bundler.webpack) {
const statsJson = bundler.webpack.toJson({ children: false });
metrics.push(...getWebpackMetrics(statsJson, opts));
metrics.push(...getWebpackMetrics(statsJson, opts.cwd));
}

if (bundler.esbuild) {
metrics.push(...getEsbuildMetrics(bundler.esbuild, opts));
metrics.push(...getEsbuildMetrics(bundler.esbuild, opts.cwd));
}

// Format metrics to be DD ready and apply filters
const metricsToSend: MetricToSend[] = metrics
.map((m) => {
let metric: Metric | null = m;
if (opts.filters.length) {
for (const filter of opts.filters) {
if (opts[CONFIG_KEY].datadog?.filters?.length) {
for (const filter of opts[CONFIG_KEY].datadog.filters) {
// Could have been filtered out by an early filter.
if (metric) {
metric = filter(metric);
}
}
}
return metric ? getMetric(metric, opts) : null;
return metric ? getMetric(metric, getOptionsDD(opts)) : null;
})
.filter((m) => m !== null) as MetricToSend[];

Expand Down
7 changes: 3 additions & 4 deletions packages/plugins/telemetry/src/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright 2019-Present Datadog, Inc.

import { CONFIG_KEY } from '../constants';
import type { OptionsWithTelemetryEnabled, Metric, Options, MetricToSend } from '../types';
import type { OptionsWithTelemetryEnabled, Metric, OptionsDD, MetricToSend } from '../types';

const filterTreeMetrics = (metric: Metric): Metric | null =>
// Remove tree metrics because way too verbose
Expand Down Expand Up @@ -59,7 +59,7 @@ export const defaultFilters: ((metric: Metric) => Metric | null)[] = [
filterMetricsOnThreshold,
];

export const getMetric = (metric: Metric, opts: Options): MetricToSend => ({
export const getMetric = (metric: Metric, opts: OptionsDD): MetricToSend => ({
type: 'gauge',
tags: [...metric.tags, ...opts.tags],
metric: `${opts.prefix ? `${opts.prefix}.` : ''}${metric.metric}`,
Expand All @@ -70,7 +70,7 @@ export const flattened = (arr: any[]) => [].concat(...arr);

export const getType = (name: string) => (name.includes('.') ? name.split('.').pop() : 'unknown');

export const getOptionsDD = (opt: OptionsWithTelemetryEnabled) => {
export const getOptionsDD = (opt: OptionsWithTelemetryEnabled): OptionsDD => {
const options = opt[CONFIG_KEY];
return {
timestamp: Math.floor((options.datadog?.timestamp || Date.now()) / 1000),
Expand All @@ -79,6 +79,5 @@ export const getOptionsDD = (opt: OptionsWithTelemetryEnabled) => {
endPoint: options.datadog?.endPoint || 'app.datadoghq.com',
prefix: options.datadog?.prefix || '',
filters: options.datadog?.filters || defaultFilters,
context: options.context || '',
};
};
45 changes: 24 additions & 21 deletions packages/plugins/telemetry/src/common/metrics/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export const getInputsDependencies = (
return deps;
};

const getModulePath = (fullPath: string, context: string): string => {
const filePath = fullPath.replace('pnp:', '').replace(context, '');
return getDisplayName(path.resolve(context, filePath), context);
const getModulePath = (fullPath: string, cwd: string): string => {
const filePath = fullPath.replace('pnp:', '').replace(cwd, '');
return getDisplayName(path.resolve(cwd, filePath), cwd);
};

// Get some indexed data to ease the metrics aggregation.
export const getIndexed = (stats: EsbuildStats, context: string): EsbuildIndexedObject => {
export const getIndexed = (stats: EsbuildStats, cwd: string): EsbuildIndexedObject => {
const inputsDependencies: { [key: string]: Set<string> } = {};
const outputsDependencies: { [key: string]: Set<string> } = {};

Expand All @@ -40,21 +40,21 @@ export const getIndexed = (stats: EsbuildStats, context: string): EsbuildIndexed
// We don't have an indexed object as entry, so we can't get an entry name from it.
for (const entry of stats.entrypoints) {
const fullPath = entry && typeof entry === 'object' ? entry.in : entry;
const realEntry = getModulePath(fullPath, context);
const realEntry = getModulePath(fullPath, cwd);
entryNames.set(realEntry, realEntry);
}
} else if (stats.entrypoints) {
const entrypoints = stats.entrypoints ? Object.entries(stats.entrypoints) : [];
for (const [entryName, entryPath] of entrypoints) {
entryNames.set(getModulePath(entryPath, context), entryName);
entryNames.set(getModulePath(entryPath, cwd), entryName);
}
}

console.log('EntryNames', entryNames);
// First loop to index inputs dependencies.
const outputs = stats.outputs ? Object.entries(stats.outputs) : [];
for (const [outputName, output] of outputs) {
if (output.entryPoint) {
const entryName = entryNames.get(getDisplayName(output.entryPoint, context));
const entryName = entryNames.get(getDisplayName(output.entryPoint, cwd));
const inputs = output.inputs ? Object.keys(output.inputs) : [];
inputsDependencies[entryName] = new Set(inputs);
outputsDependencies[entryName] = new Set([outputName]);
Expand All @@ -70,11 +70,14 @@ export const getIndexed = (stats: EsbuildStats, context: string): EsbuildIndexed
}
}

console.log('Deps', inputsDependencies, outputsDependencies, entryNames);

// Second loop to index output dependencies.
// Input dependencies are needed, hence the second loop.
for (const [outputName, output] of outputs) {
// Check which entry has generated this output.
const inputs = output.inputs ? Object.keys(output.inputs) : [];
console.log('Inputs', inputs, Object.entries(inputsDependencies));
for (const inputName of inputs) {
for (const [entryName, entry] of Object.entries(inputsDependencies)) {
if (entry.has(inputName)) {
Expand All @@ -91,18 +94,18 @@ export const getIndexed = (stats: EsbuildStats, context: string): EsbuildIndexed
};
};

export const formatEntryTag = (entryName: string, context: string): string => {
return `entryName:${getDisplayName(entryName, context)}`;
export const formatEntryTag = (entryName: string, cwd: string): string => {
return `entryName:${getDisplayName(entryName, cwd)}`;
};

export const getEntryTags = (module: string, indexed: EsbuildIndexedObject, context: string) => {
export const getEntryTags = (module: string, indexed: EsbuildIndexedObject, cwd: string) => {
const tags: string[] = [];
const inputsDependencies = indexed.inputsDependencies
? Object.entries(indexed.inputsDependencies)
: [];
for (const [entryName, entryDeps] of inputsDependencies) {
if (entryDeps.has(module)) {
tags.push(formatEntryTag(entryName, context));
tags.push(formatEntryTag(entryName, cwd));
}
}
return tags;
Expand All @@ -111,14 +114,14 @@ export const getEntryTags = (module: string, indexed: EsbuildIndexedObject, cont
export const getModules = (
stats: EsbuildStats,
indexed: EsbuildIndexedObject,
context: string,
cwd: string,
): Metric[] => {
const metrics: Metric[] = [];

const inputs = stats.inputs ? Object.entries(stats.inputs) : [];
for (const [rawModuleName, module] of inputs) {
const moduleName = getDisplayName(rawModuleName, context);
const entryTags = getEntryTags(rawModuleName, indexed, context);
const moduleName = getDisplayName(rawModuleName, cwd);
const entryTags = getEntryTags(rawModuleName, indexed, cwd);

metrics.push({
metric: 'module.size',
Expand All @@ -134,16 +137,16 @@ export const getModules = (
export const getAssets = (
stats: EsbuildStats,
indexed: EsbuildIndexedObject,
context: string,
cwd: string,
): Metric[] => {
const outputs = stats.outputs ? Object.entries(stats.outputs) : [];
return outputs.map(([rawAssetName, asset]) => {
const assetName = getDisplayName(rawAssetName, context);
const assetName = getDisplayName(rawAssetName, cwd);
const entryTags = Array.from(
new Set(
flattened(
Object.keys(asset.inputs).map((modulePath) =>
getEntryTags(modulePath, indexed, context),
getEntryTags(modulePath, indexed, cwd),
),
),
),
Expand All @@ -161,16 +164,16 @@ export const getAssets = (
export const getEntries = (
stats: EsbuildStats,
indexed: EsbuildIndexedObject,
context: string,
cwd: string,
): Metric[] => {
const metrics: Metric[] = [];
const outputs = stats.outputs ? Object.entries(stats.outputs) : [];
for (const [, output] of outputs) {
if (output.entryPoint) {
const entryName = indexed.entryNames.get(getModulePath(output.entryPoint, context));
const entryName = indexed.entryNames.get(getModulePath(output.entryPoint, cwd));
if (entryName) {
const inputs = getInputsDependencies(stats.inputs, output.entryPoint);
const tags = [formatEntryTag(entryName, context)];
const tags = [formatEntryTag(entryName, cwd)];
console.log(entryName, Object.keys(indexed.outputsDependencies));
metrics.push(
{
Expand Down
14 changes: 7 additions & 7 deletions packages/plugins/telemetry/src/common/metrics/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const getChunksFromModule = (
const getMetricsFromModule = (
stats: StatsJson,
indexed: WebpackIndexedObject,
context: string,
cwd: string,
module: Module,
) => {
const chunks = getChunksFromModule(stats, indexed.chunksPerId, module);
Expand All @@ -100,7 +100,7 @@ const getMetricsFromModule = (
}
const chunkTags = getChunkTags(chunks);
const entryTags = getEntryTags(entries);
const moduleName = getDisplayName(module.name, context);
const moduleName = getDisplayName(module.name, cwd);

return [
{
Expand All @@ -120,11 +120,11 @@ const getMetricsFromModule = (
export const getModules = (
stats: StatsJson,
indexed: WebpackIndexedObject,
context: string,
cwd: string,
): Metric[] => {
return flattened(
Object.values(indexed.modulesPerName).map((module) => {
return getMetricsFromModule(stats, indexed, context, module);
return getMetricsFromModule(stats, indexed, cwd, module);
}),
);
};
Expand Down Expand Up @@ -227,7 +227,7 @@ export const getEntries = (stats: StatsJson, indexed: WebpackIndexedObject): Met
}),
);

export const getIndexed = (stats: StatsJson, context: string): WebpackIndexedObject => {
export const getIndexed = (stats: StatsJson, cwd: string): WebpackIndexedObject => {
// Gather all modules.
const modulesPerName: { [key: string]: Module } = {};
const chunksPerId: { [key: string]: Chunk } = {};
Expand All @@ -240,15 +240,15 @@ export const getIndexed = (stats: StatsJson, context: string): WebpackIndexedObj
return;
}
// No duplicates.
if (modulesPerName[formatModuleName(module.name, context)]) {
if (modulesPerName[formatModuleName(module.name, cwd)]) {
return;
}
// Modules are sometimes registered with their loader.
if (module.name.includes('!')) {
return;
}

modulesPerName[formatModuleName(module.name, context)] = module;
modulesPerName[formatModuleName(module.name, cwd)] = module;
};

for (const [name, entry] of Object.entries(stats.entrypoints)) {
Expand Down
Loading

0 comments on commit 4225a44

Please sign in to comment.