Skip to content
Open
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
139 changes: 124 additions & 15 deletions README.md

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions src/formatters/edge-cases-formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
EdgeCasesDocument,
EdgeCasesMetrics,
} from "../types/edge-cases-types";
import { FormatterResult } from "../types/simulator-types";
import { getPhaseConfig } from "../simulators/edge-cases-template-builder";
import { formatValueForFieldType } from "./histogram-helpers";

export class EdgeCasesFormatter {
formatMetrics(
metrics: EdgeCasesMetrics,
): FormatterResult<EdgeCasesDocument>[] {
const phaseConfig = getPhaseConfig(metrics.dataStream, metrics.phase);
const metricValue = formatValueForFieldType(
phaseConfig.metricFieldType,
metrics.counterValue,
);

const doc: EdgeCasesDocument = {
"@timestamp": metrics.timestamp.toISOString(),
"metric.name": metrics.dataStream,
"test.data_stream": metrics.dataStream,
[metrics.dataStream]: metricValue,
};
if (metrics.dimensions) {
for (const [key, value] of Object.entries(metrics.dimensions)) {
doc[key] = value;
}
}
return [{ documents: [doc], format: "edge-cases" }];
}
}
65 changes: 65 additions & 0 deletions src/formatters/histogram-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Shared histogram value builders for Elasticsearch histogram field types.
*/

export function buildHistogramValue(value: number): {
values: number[];
counts: number[];
} {
const base = Math.abs(value % 1000) || 1;
return {
values: [base * 0.5, base * 0.75, base, base * 1.25, base * 1.5],
counts: [1, 2, 4, 2, 1],
};
}

export function buildTdigestValue(value: number): {
centroids: number[];
counts: number[];
} {
const base = Math.abs(value % 1000) || 1;
return {
centroids: [base * 0.5, base * 0.75, base, base * 1.25, base * 1.5],
counts: [1, 2, 4, 2, 1],
};
}

export function buildExponentialHistogramValue(
value: number,
): Record<string, unknown> {
const base = Math.abs(value % 1000) || 1;
return {
scale: 3,
sum: base * 10,
min: base * 0.5,
max: base * 1.5,
zero: { threshold: 0, count: 0 },
positive: {
indices: [0, 1, 2, 3, 4],
counts: [1, 2, 4, 2, 1],
},
negative: {
indices: [],
counts: [],
},
};
}

/**
* Formats a numeric value into the correct shape for a given Elasticsearch field type.
*/
export function formatValueForFieldType(
fieldType: string,
value: number,
): unknown {
switch (fieldType) {
case "exponential_histogram":
return buildExponentialHistogramValue(value);
case "tdigest":
return buildTdigestValue(value);
case "histogram":
return buildHistogramValue(value);
default:
return value;
}
}
34 changes: 34 additions & 0 deletions src/formatters/same-metrics-formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
SameMetricsDocument,
SameMetricsMetrics,
} from "../types/same-metrics-types";
import { FormatterResult } from "../types/simulator-types";
import { STREAM_TEMPLATE_CONFIGS } from "../simulators/same-metrics-template-builder";
import { formatValueForFieldType } from "./histogram-helpers";

const METRIC_NAME = "request_duration";

export class SameMetricsFormatter {
formatMetrics(
metrics: SameMetricsMetrics,
): FormatterResult<SameMetricsDocument>[] {
const streamConfig = STREAM_TEMPLATE_CONFIGS[metrics.dataStream];
const fieldType = streamConfig?.metricFieldType ?? "double";

const metricValue = formatValueForFieldType(fieldType, metrics.counterValue);

const doc: SameMetricsDocument = {
"@timestamp": metrics.timestamp.toISOString(),
"metric.name": METRIC_NAME,
request_duration: metricValue,
"test.scenario": metrics.scenario,
"test.data_stream": metrics.dataStream,
};
if (metrics.dimensions) {
for (const [key, value] of Object.entries(metrics.dimensions)) {
doc[key] = value;
}
}
return [{ documents: [doc], format: "same-metrics" }];
}
}
Loading