Skip to content

Commit

Permalink
feat: Initial Commit
Browse files Browse the repository at this point in the history
Code based on krakend-logstash repo with gcp
severity level changes.
  • Loading branch information
rachirib committed Oct 10, 2024
1 parent b5acb3c commit 2bcb76c
Show file tree
Hide file tree
Showing 7 changed files with 493 additions and 2 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Go

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Test
run: go test -v .
31 changes: 31 additions & 0 deletions .github/workflows/lock-threads.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: 'Lock Threads'

on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:

permissions:
issues: write
pull-requests: write

concurrency:
group: lock

jobs:
action:
runs-on: ubuntu-latest
steps:
- uses: dessant/lock-threads@v3
with:
pr-inactive-days: '90'
issue-inactive-days: '90'
add-issue-labels: 'locked'
issue-comment: >
This issue was marked as resolved a long time ago and now has been
automatically locked as there has not been any recent activity after it.
You can still open a new issue and reference this link.
pr-comment: >
This pull request was marked as resolved a long time ago and now has been
automatically locked as there has not been any recent activity after it.
You can still open a new issue and reference this link.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# krakend-gcplogging
Krakend Log Formatting for GCP
# krakend-gcploggin
A KrakenD log formatter compatible with gcplogging

https://cloud.google.com/logging/docs/structured-logging

Based on the following repository
https://github.com/krakend/krakend-logstash

14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/rachirib/krakend-gcplogging/v2

go 1.17

require (
github.com/krakendio/krakend-gologging/v2 v2.0.1
github.com/luraproject/lura/v2 v2.0.5
)

require (
github.com/krakendio/flatmap v1.1.1 // indirect
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/krakendio/flatmap v1.1.1 h1:rGBNVpBY0pMk6cLOwerVzoKY4HELnpu0xvqB231lOCQ=
github.com/krakendio/flatmap v1.1.1/go.mod h1:KBuVkiH5BcBFRa5A1HdSHDn8a8LzsyRTKZArX0vqTbo=
github.com/krakendio/krakend-gologging/v2 v2.0.1 h1:QuBgXfo9AMbMW2KrACPKGMg+gcbkGQqbiYnGDSPapS8=
github.com/krakendio/krakend-gologging/v2 v2.0.1/go.mod h1:ar6rViGYhsxtiCfxGeh7b7Djbh5wMIBXsyBKudADLJs=
github.com/luraproject/lura/v2 v2.0.5 h1:Mc4uj37s7mv6qRLy+Uo983CiaITPSVJYooeUilbiD+k=
github.com/luraproject/lura/v2 v2.0.5/go.mod h1:r2N4j89Snm1j+Y9CCa9cYR1T2ETRL0E4y9P+DgymqX4=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
178 changes: 178 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Package logstash provides a logstash formatter for the krakend-gologging pkg
package gcplog

import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"time"

gologging "github.com/krakendio/krakend-gologging/v2"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
"github.com/luraproject/lura/v2/proxy"
)

const Namespace = "github.com/rachirib/krakend-gcplogging"

var (
ErrNothingToLog = errors.New("nothing to log")
ErrWrongConfig = fmt.Errorf("getting the extra config for the krakend-logstash module")
hostname = "localhost"
loggingPattern = "%{message}"
)

func init() {
name, err := os.Hostname()
if err == nil {
hostname = name
}
}

// NewLogger returns a krakend logger wrapping a gologging logger
func NewLogger(cfg config.ExtraConfig, ws ...io.Writer) (logging.Logger, error) {
_, ok := cfg[Namespace]
if !ok {
return nil, ErrWrongConfig
}
serviceName := "KRAKEND"
gologging.DefaultPattern = loggingPattern
if tmp, ok := cfg[gologging.Namespace]; ok {
if section, ok := tmp.(map[string]interface{}); ok {
if tmp, ok = section["prefix"]; ok {
if v, ok := tmp.(string); ok {
serviceName = v
}
delete(section, "prefix")
}
}
}

loggr, err := gologging.NewLogger(cfg, ws...)
if err != nil {
return nil, err
}

return &Logger{loggr, serviceName}, nil
}

// Logger is a wrapper over a github.com/devopsfaith/krakend/logging logger
type Logger struct {
logger logging.Logger
serviceName string
}

var now = time.Now

func (l *Logger) format(logLevel LogLevel, v ...interface{}) ([]byte, error) {
if len(v) == 0 {
return []byte{}, ErrNothingToLog
}

msg, ok := v[0].(string)
if !ok {
msg = fmt.Sprintf("%+v", v[0])
}
record := map[string]interface{}{}
if len(v) > 1 {
for _, ctx := range v[1:] {
switch value := ctx.(type) {
case map[string]interface{}:
for k, item := range value {
record[k] = item
}
case int:
msg = fmt.Sprintf("%s %d", msg, value)
case bool:
msg = fmt.Sprintf("%s %t", msg, value)
case float64:
msg = fmt.Sprintf("%s %f", msg, value)
case string:
msg += " " + value
case *proxy.Request:
record["proxy.Request"] = value
case *proxy.Response:
record["proxy.Response"] = value
default:
record[fmt.Sprintf("%T", ctx)] = ctx
}
}
}

record["@version"] = 1
record["@timestamp"] = now().Format(ISO_8601)
record["module"] = l.serviceName
record["host"] = hostname
record["message"] = msg
record["severity"] = logLevel

return json.Marshal(record)
}

// Debug implements the logger interface
func (l *Logger) Debug(v ...interface{}) {
data, err := l.format(LEVEL_DEBUG, v...)
if err != nil {
return
}
l.logger.Debug(string(data))
}

// Info implements the logger interface
func (l *Logger) Info(v ...interface{}) {
data, err := l.format(LEVEL_INFO, v...)
if err != nil {
return
}
l.logger.Info(string(data))
}

// Warning implements the logger interface
func (l *Logger) Warning(v ...interface{}) {
data, err := l.format(LEVEL_WARNING, v...)
if err != nil {
return
}
l.logger.Warning(string(data))
}

// Error implements the logger interface
func (l *Logger) Error(v ...interface{}) {
data, err := l.format(LEVEL_ERROR, v...)
if err != nil {
return
}
l.logger.Error(string(data))
}

// Critical implements the logger interface
func (l *Logger) Critical(v ...interface{}) {
data, err := l.format(LEVEL_CRITICAL, v...)
if err != nil {
return
}
l.logger.Critical(string(data))
}

// Fatal implements the logger interface
func (l *Logger) Fatal(v ...interface{}) {
data, err := l.format(LEVEL_CRITICAL, v...)
if err != nil {
return
}
l.logger.Fatal(string(data))
}

type LogLevel string

const (
LEVEL_DEBUG = "DEBUG"
LEVEL_INFO = "INFO"
LEVEL_WARNING = "WARN"
LEVEL_ERROR = "ERROR"
LEVEL_CRITICAL = "EMERGENCY"

ISO_8601 = "2006-01-02T15:04:05.000000-07:00"
)
Loading

0 comments on commit 2bcb76c

Please sign in to comment.