Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang rust binds #545

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ingestors/golang/gin/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,6 @@ github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.0 h1:ePTrnizCXI5HzKe4qpEfYlcxGOEyySOAi9EAmUfWoRU=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.0/go.mod h1:IrHJdeANVNi9deZW3NmZfCp8nk0I4ERfA+x+4DC1OnU=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.1 h1:r+B3lFyM+bl1GqBg9gNmDNZ1eQuSaRZuwujarTLczr8=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.1/go.mod h1:ZPUPd2/cfz4DKH/xJfN4F0xxSyAcPHVApeQSGd1aYW0=
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
Expand Down
66 changes: 46 additions & 20 deletions ingestors/golang/gin/metlo_gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const MAX_BODY int = 10 * 1024

type metloApp interface {
Send(data metlo.MetloTrace)
Block(req metlo.TraceReq, meta metlo.TraceMeta) bool
Allow() bool
}

Expand All @@ -29,6 +30,14 @@ type metloInstrumentation struct {
app metloApp
serverHost string
serverPort int
rejectFn func(*gin.Context)
}

type CustomInitParams struct {
app metloApp
serverHost string
serverPort int
rejectFn func(*gin.Context)
}

func Init(app metloApp) metloInstrumentation {
Expand All @@ -40,6 +49,7 @@ func CustomInit(app metloApp, serverHost string, serverPort int) metloInstrument
app: app,
serverHost: serverHost,
serverPort: serverPort,
rejectFn: nil,
}
}

Expand All @@ -59,6 +69,10 @@ func (w bodyLogWriter) Write(b []byte) (int, error) {
return w.ResponseWriter.Write(b)
}

func (m *metloInstrumentation) SetRejectFn(rejectFn func(*gin.Context)) {
m.rejectFn = rejectFn
}

func (m *metloInstrumentation) Middleware(c *gin.Context) {
var bytesWritten *int = new(int)
*bytesWritten = 0
Expand Down Expand Up @@ -88,39 +102,51 @@ func (m *metloInstrumentation) Middleware(c *gin.Context) {
log.Println("Metlo couldn't find source port for incoming request")
}

c.Next()
req := metlo.TraceReq{
Url: metlo.TraceUrl{
Host: c.Request.Host,
Path: c.Request.URL.Path,
Parameters: queryParams,
},
Headers: reqHeaders,
Body: string(body),
Method: c.Request.Method,
}

meta := metlo.TraceMeta{
Environment: "production",
Incoming: true,
Source: c.ClientIP(),
SourcePort: sourcePort,
Destination: m.serverHost,
DestinationPort: m.serverPort,
MetloSource: "go/gin",
}

if m.app.Block(req, meta) {
if m.rejectFn != nil {
m.rejectFn(c)
} else {
c.String(403, "Forbidden")
}
c.Abort()
} else {
c.Next()
}
resHeaderMap := c.Writer.Header()
resHeaders := make([]metlo.NV, 0)
for k := range resHeaderMap {
resHeaders = append(resHeaders, metlo.NV{Name: k, Value: strings.Join(resHeaderMap[k], ",")})
}

tr := metlo.MetloTrace{
Request: metlo.TraceReq{
Url: metlo.TraceUrl{
Host: c.Request.Host,
Path: c.Request.URL.Path,
Parameters: queryParams,
},
Headers: reqHeaders,
Body: string(body),
Method: c.Request.Method,
},
Request: req,
Response: metlo.TraceRes{
Status: blw.Status(),
Body: blw.body.String(),
Headers: resHeaders,
},
Meta: metlo.TraceMeta{
Environment: "production",
Incoming: true,
Source: c.ClientIP(),
SourcePort: sourcePort,
Destination: m.serverHost,
DestinationPort: m.serverPort,
MetloSource: "go/gin",
},
Meta: meta,
}

go m.app.Send(tr)
Expand Down
5 changes: 1 addition & 4 deletions ingestors/golang/gorilla/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/metlo-labs/metlo/ingestors/golang/gorilla

go 1.11

require (
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.1
golang.org/x/net v0.10.0 // indirect
)
require github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.1
2 changes: 0 additions & 2 deletions ingestors/golang/gorilla/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,6 @@ github.com/lyft/protoc-gen-star/v2 v2.0.1/go.mod h1:RcCdONR2ScXaYnQC5tUzxzlpA3WV
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.0 h1:ePTrnizCXI5HzKe4qpEfYlcxGOEyySOAi9EAmUfWoRU=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.0/go.mod h1:IrHJdeANVNi9deZW3NmZfCp8nk0I4ERfA+x+4DC1OnU=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.1 h1:r+B3lFyM+bl1GqBg9gNmDNZ1eQuSaRZuwujarTLczr8=
github.com/metlo-labs/metlo/ingestors/golang/metlo v1.0.1/go.mod h1:ZPUPd2/cfz4DKH/xJfN4F0xxSyAcPHVApeQSGd1aYW0=
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
Expand Down
56 changes: 36 additions & 20 deletions ingestors/golang/gorilla/metlo_gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const MAX_BODY int = 10 * 1024

type metloApp interface {
Send(data metlo.MetloTrace)
Block(req metlo.TraceReq, meta metlo.TraceMeta) bool
Allow() bool
}

Expand All @@ -30,6 +31,7 @@ type metloInstrumentation struct {
app metloApp
serverHost string
serverPort int
rejectFn func(http.ResponseWriter, http.Handler)
}

func Init(app metloApp) metloInstrumentation {
Expand Down Expand Up @@ -79,7 +81,6 @@ func (m *metloInstrumentation) Middleware(next http.Handler) http.Handler {
r.Body = ioutil.NopCloser(bytes.NewReader(body))

if m.app.Allow() {
next.ServeHTTP(logRespWriter, r)
reqHeaders := make([]metlo.NV, 0)
for k := range r.Header {
reqHeaders = append(reqHeaders, metlo.NV{Name: k, Value: strings.Join(r.Header[k], ",")})
Expand All @@ -105,36 +106,51 @@ func (m *metloInstrumentation) Middleware(next http.Handler) http.Handler {
log.Println("Metlo couldn't find source port for incoming request")
}

request := metlo.TraceReq{
Url: metlo.TraceUrl{
Host: r.Host,
Path: r.URL.Path,
Parameters: queryParams,
},
Headers: reqHeaders,
Body: string(body),
Method: r.Method,
}

meta := metlo.TraceMeta{
Environment: "production",
Incoming: true,
Source: sourceIp,
SourcePort: sourcePort,
Destination: m.serverHost,
DestinationPort: m.serverPort,
MetloSource: "go/gorilla",
}

if m.app.Block(request, meta) {
if m.rejectFn != nil {
m.rejectFn(logRespWriter, next)
} else {
logRespWriter.Write([]byte("Forbidden"))
logRespWriter.statusCode = 403
}
} else {
next.ServeHTTP(logRespWriter, r)
}

statusCode := logRespWriter.statusCode
if statusCode == 0 {
statusCode = 200
}

tr := metlo.MetloTrace{
Request: metlo.TraceReq{
Url: metlo.TraceUrl{
Host: r.Host,
Path: r.URL.Path,
Parameters: queryParams,
},
Headers: reqHeaders,
Body: string(body),
Method: r.Method,
},
Request: request,
Response: metlo.TraceRes{
Status: statusCode,
Body: logRespWriter.buf.String(),
Headers: resHeaders,
},
Meta: metlo.TraceMeta{
Environment: "production",
Incoming: true,
Source: sourceIp,
SourcePort: sourcePort,
Destination: m.serverHost,
DestinationPort: m.serverPort,
MetloSource: "go/gorilla",
},
Meta: meta,
}

go m.app.Send(tr)
Expand Down
11 changes: 11 additions & 0 deletions ingestors/golang/metlo/go_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "structs.h"

unsigned char Metlo_block_trace(Metlo_ExchangeStruct data);
void Metlo_ingest_trace(Metlo_ApiTrace trace);
unsigned char Metlo_startup(
char *metlo_url,
char *api_key,
unsigned short backend_port,
unsigned short collector_port,
char *log_level,
char *encryption_key);
Loading