Skip to content

Commit

Permalink
get tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
j-white committed May 6, 2024
1 parent 5abd360 commit 62e38c9
Show file tree
Hide file tree
Showing 6 changed files with 1,595 additions and 26 deletions.
6 changes: 4 additions & 2 deletions features/step_definitions/cf_mock_server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import http from "http";
import {AddressInfo} from "net";
import fs from "fs";

export class CloudflareMockServer {
server: http.Server | undefined;

start() {
let self = this;
const fileContents = fs.readFileSync('./features/step_definitions/worker_query_response.json').toString()
this.server = http.createServer((req, res) => {
var body = "";
req.on('readable', function() {
Expand All @@ -16,8 +18,8 @@ export class CloudflareMockServer {
});
req.on('end', function() {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('OK');
res.setHeader('Content-Type', 'application/json');
res.end(fileContents);
});
});
this.server.listen(() => {
Expand Down
5 changes: 3 additions & 2 deletions features/step_definitions/o11y_steps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {After, Given, When, Then} from '@cucumber/cucumber';
import {cloudflareMockServer, mf, otelServer} from "./state";
import {expect} from "chai";
import {Utils} from "./utils";

Given('Worker is configured to point to mock Cloudflare API', function () {
cloudflareMockServer.start();
Expand All @@ -17,14 +18,14 @@ When('Worker is triggered', async function () {
});

Then('Worker metrics are published', async function () {
await new Promise(r => setTimeout(r, 5000));
await Utils.waitUntil(() => otelServer.getMetrics().length > 0);
let metrics = otelServer.getMetrics();
expect(metrics).to.have.length.gte(1);
});

Then('Meter name should include {string}', function (metricName: string) {
let metricNames = otelServer.getMetricNames();
expect(metricNames).to.contain(metricName);
expect(metricNames).to.include(metricName);
});

After(async function () {
Expand Down
15 changes: 7 additions & 8 deletions features/step_definitions/otel_server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import http from 'http';
import {IExportMetricsServiceRequest} from "@opentelemetry/otlp-transformer";
import {IExportMetricsServiceRequest, IResourceMetrics} from "@opentelemetry/otlp-transformer";
import {AddressInfo} from "net";

export class OpenTelemetryServer {
Expand Down Expand Up @@ -40,16 +40,15 @@ export class OpenTelemetryServer {
indexMetrics() {
let self = this;
this.metricNames.clear();
console.log("Indexing metrics", this.metrics);
for (let metrics of this.metrics) {
for (let resourceMetrics of metrics.resourceMetrics) {
for (let scopeMetrics of resourceMetrics.scopeMetrics) {
for (let metric of scopeMetrics.metrics) {
self.metricNames.set(metric.name, 1);
}
let resourceMetrics = metrics.resourceMetrics as unknown as IResourceMetrics;
for (let scopeMetrics of resourceMetrics.scopeMetrics) {
for (let metric of scopeMetrics.metrics) {
self.metricNames.set(metric.name, 1);
}
}
}
console.log("Indexed metrics", self.metricNames);
}

metricsUrl() {
Expand All @@ -69,6 +68,6 @@ export class OpenTelemetryServer {
}

getMetricNames() {
return this.metricNames.keys();
return Array.from(this.metricNames.keys());
}
}
20 changes: 20 additions & 0 deletions features/step_definitions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export class Utils {
static waitUntil = (condition:any) => {
return new Promise((resolve:any, reject) => {
const interval = setInterval(() => {
if (!condition()) {
return;
}

clearInterval(interval);
resolve();
}, 100);

setTimeout(() => {
clearInterval(interval);
reject('condition was not resolved in time');
}, 4500);
});
};

}
Loading

0 comments on commit 62e38c9

Please sign in to comment.