feat: Add skip function on dexter service
code0x9 opened this issue · 8 comments
Summary
What:
Add skip function on dexter logging service
Why:
Want to skip access log from health check
Acceptance Criteria
Below is a list of tasks that must be completed before this issue can be closed.
- Write documentation
- Write unit tests
- Write integration tests
Example Pseudo Code (for implementation)
const dexter = new DexterService({
skip: function (request, _response) {
const url = new URL(request.url)
if (url.pathname === '/health_check') {
return true
}
return false
}
});
Can't you implement Dexter only for the resources you want with resource level services? That is, if you don't want Dexter on /health_check
, add Dexter to all other resources except /health_check
.
of course your suggestion will work. I just wanted to enable dexter on all resources except some special endpoints.
for example, I want to run drash on K8S and with proves. K8S will keep calling prove endpoints and I don't want K8S to fill up my log storage.
by skip feature, I can add dexter by server-level to reduce duplicate resource-level dexter service configurations.
If you want to add to Dexter to all resources more automatically:
export abstract class MyBaseResourceWithBetterName extends Drash.Resource {
public services = {
ALL: [
new DexterService(),
],
};
}
class HomeResource extends MyBaseResourceWithBetterName {
public paths = ["/home", "/foo"];
// Uses dexter
}
class HealthCheck extends Drash.Resource {
public paths = ["/health_check"];
// Doesn't use dexter
}
Will this solve your issue? The problem here is bloating the service with extra config props that are not really required. If we accept this one, more will follow.
skip
feature is just a utility function which can solve some problems without class inheritance or code duplicates. morgan provides this(https://github.com/expressjs/morgan#skip) and it's quite useful.
And personally, I prefer Composition over inheritance principle so I'd like to solve this by non-inheritance way.
There can be more scenario like "I want to skip log from internal IP" or "I want to skip log when response code is below 400" which skip
feature can help.
@code0x9 , i do agree that a skip feature would help and bloating the logs degrades developer ux when you're debugging and stuff. however, a lot of the stuff in drash land is made to be extensible which is why a lot of the modules are class-based. i know you don't want to go the inheritance way, but unfortunately there's no other way to accomplish what you want unless:
- you go the inheritance way; or
- @Guergeiro, @ebebbington and i discuss adding the new feature and getting it released (which could take time and we don't want to be a blocker in your development efforts)
@Guergeiro provided the resource inheritance way, but there's also another way: class MyService extends DexterService { ... }
. this way it keeps you from having to modify resources and extending resources in case that's not your jam. here's a simple example (and you can copy-paste this code and it should work when you run it):
import * as Drash from "https://deno.land/x/drash@v2.6.0/mod.ts";
import { DexterService } from "https://deno.land/x/drash@v2.6.0/src/services/dexter/dexter.ts";
/**
* Class to override DexterService that can skip the logging process for
* specific requests.
*/
class SkippableDexterService extends DexterService {
/**
* Write logs after a resource is hit.
* @param request
* @param response
*/
runAfterResource(request: Drash.Request, response: Drash.Response): void {
if (this.#skip(request)) {
return;
}
super.runAfterResource(request, response);
}
/**
* Write logs before a resource is hit.
* @param request
* @param response
*/
public runBeforeResource(
request: Drash.Request,
response: Drash.Response,
): void {
if (this.#skip(request)) {
return;
}
super.runBeforeResource(request, response);
}
/**
* Should logging be skipped for the request in question?
* @param request
* @returns True if yes, false if no.
*/
#skip(request: Drash.Request): boolean {
const url = new URL(request.url);
if (url.pathname === "/health_check") {
return true;
}
return false;
}
}
// Create the skippable version of DexterService to be plugged into the server
const dexter = new SkippableDexterService({
enabled: true,
method: true,
url: true,
});
/**
* Some basic resource.
*/
class HomeResource extends Drash.Resource {
public paths = ["/", "/home"];
public GET(request: Drash.Request, response: Drash.Response): void {
return response.text("Hello");
}
}
/**
* Some resource that has a path that matches SkippableDexterService#skip list.
*/
class SkipsDexterServiceResource extends Drash.Resource {
public paths = ["/health_check"];
public GET(request: Drash.Request, response: Drash.Response): void {
return response.text("Hello");
}
}
// Create your server and plug in the skippable dexter service
const server = new Drash.Server({
resources: [
HomeResource,
SkipsDexterServiceResource,
],
services: [
dexter,
],
hostname: "0.0.0.0",
port: 1447,
protocol: "http",
});
server.run();
console.log(`Server running at ${server.address}`);
console.log(`Try it out:\n - /home will result in logs\n - /health_check will skip logging`);
@crookse Thanks for your explanation and sample code. I'll try to go with inheritance as you recommended.
@code0x9 feel free to let us know how you get on!
closing since there hasn't been activity on this for a while. anyone feel free to open for discussion again!