Skip to content

Commit

Permalink
Add PINECONE_DEBUG request middleware (#136)
Browse files Browse the repository at this point in the history
## Problem

Sometimes it is helpful to see exactly what requests are being made
under the hood.

## Solution

Define new middleware functions that log out helpful information.

Set new env vars for extra output:

```sh
PINECONE_DEBUG=true
PINECONE_DEBUG_CURL=true
```

<img width="1496" alt="Screenshot 2023-10-06 at 5 32 44 PM"
src="https://github.com/pinecone-io/pinecone-ts-client/assets/1326365/4054f44e-6eb0-4216-8bad-ff5c8cfeffa3">

## Type of Change

- [x] None of the above: developer-oriented feature

## Test Plan

Describe specific steps for validating this change.
  • Loading branch information
jhamon authored Oct 9, 2023
1 parent edd85a8 commit b18fc12
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
73 changes: 72 additions & 1 deletion src/utils/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,78 @@
import { ResponseError } from '../pinecone-generated-ts-fetch';
import { Middleware, ResponseError } from '../pinecone-generated-ts-fetch';
import { handleApiError } from '../errors';

const debugMiddleware: Middleware[] = [];

const chalk = (str, color) => {
const colors = {
blue: '\x1b[34m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
};

return colors[color] + str + '\x1b[39m';
};

/**
* Enable the `PINECONE_DEBUG` environment variable to print the request and
* response bodies for each request.
*
* Api-Key headers will be redacted.
*/
if (process && process.env && process.env.PINECONE_DEBUG) {
const debugLogMiddleware = {
pre: async (context) => {
console.debug(
chalk(`>>> Request: ${context.init.method} ${context.url}`, 'blue')
);

const headers = JSON.parse(JSON.stringify(context.init.headers));
headers['Api-Key'] = '***REDACTED***';
console.debug(chalk(`>>> Headers: ${JSON.stringify(headers)}`, 'blue'));

if (context.init.body) {
console.debug(chalk(`>>> Body: ${context.init.body}`, 'blue'));
}
console.debug('');
},

post: async (context) => {
console.debug(chalk(`<<< Status: ${context.response.status}`, 'green'));
console.debug(
chalk(`<<< Body: ${await context.response.text()}`, 'green')
);
console.debug('');
},
};

debugMiddleware.push(debugLogMiddleware);
}

/**
* Enable the `PINECONE_DEBUG_CURL` environment variable to print the equivalent
* curl commands for each request. These commands will include the API key and
* other sensitive information, so be careful when using this option.
*/
if (process && process.env && process.env.PINECONE_DEBUG_CURL) {
const debugCurlMiddleware = {
post: async (context) => {
let headers = `-H "Api-Key: ${(context.init.headers || {})['Api-Key']}"`;
if (context.init.headers && context.init.headers['Content-Type']) {
headers += ` -H "Content-Type: ${context.init.headers['Content-Type']}"`;
}
const cmd = `curl -X ${context.init.method} ${context.url} ${headers} ${
context.init.body ? `-d '${context.init.body}'` : ''
}`;
console.debug(chalk(cmd, 'red'));
console.debug('');
},
};
debugMiddleware.push(debugCurlMiddleware);
}

export const middleware = [
...debugMiddleware,
{
onError: async (context) => {
const err = await handleApiError(context.error);
Expand Down
11 changes: 11 additions & 0 deletions src/utils/testHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ResponseError } from '../pinecone-generated-ts-fetch';

export const responseError = (status: number, message: string) => {
return new ResponseError(
{
status,
text: async () => message,
} as Response,
'oops'
);
};

0 comments on commit b18fc12

Please sign in to comment.