Skip to content

Commit

Permalink
Add: Zerolog with FluentBit Integration
Browse files Browse the repository at this point in the history
This PR adds support for using a remote log aggregator using FluentBit.
FluentBit configuration is out of scope for this repo, but we do have a
sample configuration for it. We're currently using JSON formatted logs
with the following template:

```json
{"time":"${time_rfc3339}","request_id":"${request_id}","remote_ip":"${remote_ip}", "host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",
"status":"${status}","error":"${error}","latency":"${latency}","latency_human":"${latency_human}","bytes_in":"${bytes_in}","bytes_out":"${bytes_out}"}
```

Fields Included for logs (For all HTTP requests):
- Timestamp
- RequestID
- RemoteIP
- Host
- Method
- URI
- User Agent
- Status
- Error
- Latency
- Human Latency
- Bytes In
- Bytes Out

If we need, we can add more fields in the future and also connect with a
different log provider mechanism.

Signed-off-by: jay-dee7 <jasdeepsingh.uppal@gmail.com>
  • Loading branch information
jay-dee7 committed Nov 17, 2021
1 parent ac6a928 commit 1eb81a0
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 79 deletions.
2 changes: 2 additions & 0 deletions cache/register_for_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"regexp"

"github.com/containerish/OpenRegistry/types"
"github.com/dgraph-io/badger/v3"
"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -101,6 +102,7 @@ func (ds *dataStore) RegisterForBeta(ctx echo.Context) error {
func (ds *dataStore) GetAllEmail(ctx echo.Context) error {
bz, err := ds.Get([]byte("email"))
if err != nil && err != badger.ErrKeyNotFound {
ctx.Set(types.HttpEndpointErrorKey, err.Error())
return ctx.JSON(http.StatusInternalServerError, echo.Map{
"error": "couldn't get them all",
})
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ func main() {
return
}

router.Register(e, reg, authSvc, localCache)
router.Register(cfg, e, reg, authSvc, localCache)
log.Fatal().Msgf("error starting server: %s\n", e.Start(cfg.Address()))
}
14 changes: 8 additions & 6 deletions registry/v2/blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"

"github.com/containerish/OpenRegistry/types"
"github.com/fatih/color"
"github.com/labstack/echo/v4"
)
Expand Down Expand Up @@ -37,14 +38,15 @@ func (b *blobs) HEAD(ctx echo.Context) error {
"skynet": "skynet link not found",
}
errMsg := b.errorResponse(RegistryErrorCodeManifestBlobUnknown, err.Error(), details)
b.fluentbit.Send(errMsg)

ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
}

size, ok := b.registry.skynet.Metadata(layerRef.Skylink)
if !ok {
errMsg := b.errorResponse(RegistryErrorCodeManifestBlobUnknown, "Manifest does not exist", nil)
b.fluentbit.Send(errMsg)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusNotFound, errMsg)
}

Expand All @@ -65,7 +67,7 @@ func (b *blobs) UploadBlob(ctx echo.Context) error {
"stream upload after first write are not allowed",
nil,
)
b.fluentbit.Send(errMsg)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusBadRequest, errMsg)
}

Expand All @@ -88,13 +90,13 @@ func (b *blobs) UploadBlob(ctx echo.Context) error {
"contentRange": contentRange,
}
errMsg := b.errorResponse(RegistryErrorCodeBlobUploadUnknown, err.Error(), details)
b.fluentbit.Send(errMsg)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusRequestedRangeNotSatisfiable, errMsg)
}

if start != len(b.uploads[uuid]) {
errMsg := b.errorResponse(RegistryErrorCodeBlobUploadUnknown, "content range mismatch", nil)
b.fluentbit.Send(errMsg)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusRequestedRangeNotSatisfiable, errMsg)
}

Expand All @@ -106,7 +108,7 @@ func (b *blobs) UploadBlob(ctx echo.Context) error {
"error while creating new buffer from existing blobs",
nil,
)
b.fluentbit.Send(errMsg)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
return ctx.JSONBlob(http.StatusInternalServerError, errMsg)
} // 10
ctx.Request().Body.Close()
Expand Down
Loading

0 comments on commit 1eb81a0

Please sign in to comment.