Skip to content

Commit

Permalink
feat: filter logs with go regex using pipemgr.filter
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Dec 17, 2024
1 parent 239e4df commit 7f77736
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

Send container logs to [pipe](https://pipe.pico.sh) using a docker image.

To enable, apply the `pipemgr` label to any service in your `docker-compose.yml` file:
See [docker-compose.yml](./docker-compose.yml) to see how to run `pipemgr`.

To enable, apply the `pipemgr` label to any service in your `docker-compose.yml`
file:

```yaml
services:
Expand All @@ -14,3 +17,18 @@ services:
This will send all stdout/stderr from `httpbin` to `pipe` or any other SSH
service.

## filtering

We support regex filtering with the `pipemgr.filter` label:

```yaml
services:
httpbin:
image: kennethreitz/httpbin
label:
pipemgr.enable: true
pipemgr.filter: "GET.+(404)"
```

In this example, we will only send log lines with a GET 404 response status.
21 changes: 20 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -58,6 +59,16 @@ func containerStart(ctx context.Context, logger *slog.Logger, client *client.Cli
if !isEnabled {
return nil
}

filterStr := ""
if filterRaw, ok := containerInfo.Config.Labels["pipemgr.filter"]; ok {
filterStr = strings.TrimSpace(filterRaw)
}
filter, err := regexp.Compile(filterStr)
if err != nil {
logger.Error("Invalid regex provided to pipemgr.filter", "err", err, "filter", filterStr)
}

logger.Info("connecting to logs", "container", containerID)
opts := ct.LogsOptions{
ShowStdout: true,
Expand All @@ -83,7 +94,15 @@ func containerStart(ctx context.Context, logger *slog.Logger, client *client.Cli
for {
for scanner.Scan() {
line := scanner.Text()
reconn.Write([]byte(line + "\n"))
if filter != nil {
if !filter.Match([]byte(line)) {
continue
}
}
_, err = reconn.Write([]byte(line + "\n"))
if err != nil {
logger.Error("could not write to pipe", "err", err)
}
}
}
}()
Expand Down

0 comments on commit 7f77736

Please sign in to comment.