Skip to content

Commit 8d4e5b0

Browse files
authored
Merge pull request #447 from OpenFn/http-logging
Add better HTTP logging
2 parents cdb50ad + 7b91dc7 commit 8d4e5b0

7 files changed

Lines changed: 35 additions & 9 deletions

File tree

.changeset/hungry-onions-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"apollo": patch
3+
---
4+
5+
Add HTTP logs to the server

.changeset/strict-maps-judge.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"apollo": patch
3+
---
4+
5+
echo: fix an issue which prevented the service from running

platform/src/bridge.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { rm } from "node:fs/promises";
1313
export const run = async (
1414
scriptName: string,
1515
port: number, // needed for self-calling services in pythonland
16-
args: JSON,
16+
args: any = {},
1717
onLog?: (str: string) => void,
1818
onEvent?: (type: string, payload: any /* string or json tbh */) => void
1919
) => {
@@ -25,10 +25,10 @@ export const run = async (
2525
const inputPath = tmpfile.replace("{}", "input");
2626
const outputPath = tmpfile.replace("{}", "output");
2727

28-
console.log("Initing input file at", inputPath);
28+
// console.log("Initing input file at", inputPath);
2929
await Bun.write(inputPath, JSON.stringify(args));
3030

31-
console.log("Initing output file at", outputPath);
31+
// console.log("Initing output file at", outputPath);
3232
await Bun.write(outputPath, "");
3333

3434
const proc = spawn(

platform/src/middleware/services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default async (app: Elysia, port: number) => {
3333

3434
// simple post
3535
app.post(name, async (ctx) => {
36-
console.log(`POST to /services/${name}`);
36+
console.log(`Calling /services/${name}`);
3737
const payload = ctx.body;
3838
const result = await callService(m, port, payload as any);
3939

@@ -51,7 +51,7 @@ export default async (app: Elysia, port: number) => {
5151

5252
// HTTP streaming
5353
app.post(`${name}/stream`, async (ctx) => {
54-
console.log(`HTTP stream connnected to ${name}`);
54+
console.log(`HTTP stream connected to ${name}`);
5555
const payload = ctx.body;
5656

5757
const stream = new ReadableStream({

platform/src/server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import setupDir from "./middleware/dir";
44
import setupHealthcheck from "./middleware/healthcheck";
55
import setupServices from "./middleware/services";
66
import { html } from "@elysiajs/html";
7+
import logRequest from "./util/log-request";
78

89
export default async (port: number | string = 3000) => {
910
const app = new Elysia();
1011

1112
app.use(html());
1213

14+
app.derive(() => ({ start: Date.now() }));
15+
app.onAfterHandle(logRequest);
16+
1317
await setupHealthcheck(app);
1418
await setupDir(app);
1519
await setupServices(app, +port);

platform/src/util/log-request.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default ({
2+
request,
3+
set,
4+
start,
5+
}: {
6+
request: Request;
7+
set: { status?: number | string };
8+
start: number;
9+
}) => {
10+
const duration = Date.now() - start;
11+
console.log(
12+
`http: ${request.method} ${new URL(request.url).pathname}${
13+
set.status ?? 200
14+
} in ${duration}ms`
15+
);
16+
};

services/echo/echo.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
from .log import log
2-
from util import EventLogger
3-
4-
evt = EventLogger()
52

63
# Simple python service to echo requests back to the caller
74
# Used in test
85
def main(x):
96
log(x)
10-
evt.send_status("{ msg: "+ str(x) +" }")
117
return x

0 commit comments

Comments
 (0)