-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
602 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Build uuid-annotator | ||
FROM golang:1.18-alpine as build | ||
RUN apk --no-cache add git | ||
COPY . /go/src/github.com/m-lab/tcp-info | ||
WORKDIR /go/src/github.com/m-lab/tcp-info | ||
RUN go get -v ./cmd/example-eventsocket-client && \ | ||
go install ./cmd/example-eventsocket-client | ||
|
||
# Put it in its own image. | ||
FROM alpine:3.16 | ||
COPY --from=build /go/bin/example-eventsocket-client /example-eventsocket-client | ||
WORKDIR / | ||
ENTRYPOINT ["/example-eventsocket-client"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// example-eventsocket-client is a minimal reference implementation of a tcpinfo | ||
// eventsocket client. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
"time" | ||
|
||
"github.com/m-lab/go/flagx" | ||
"github.com/m-lab/go/rtx" | ||
"github.com/m-lab/tcp-info/eventsocket" | ||
"github.com/m-lab/tcp-info/inetdiag" | ||
) | ||
|
||
var ( | ||
mainCtx, mainCancel = context.WithCancel(context.Background()) | ||
) | ||
|
||
// event contains fields for an open event. | ||
type event struct { | ||
timestamp time.Time | ||
uuid string | ||
id *inetdiag.SockID | ||
} | ||
|
||
// handler implements the eventsocket.Handler interface. | ||
type handler struct { | ||
events chan event | ||
} | ||
|
||
// Open is called by tcp-info synchronously for every TCP open event. | ||
func (h *handler) Open(ctx context.Context, timestamp time.Time, uuid string, id *inetdiag.SockID) { | ||
// NOTE: Until this function returns, tcp-info cannot send additional | ||
// events. So, immediately attempt to send the event on a channel that will | ||
// be read by an asynchronous processing goroutine. | ||
select { | ||
case h.events <- event{timestamp: timestamp, uuid: uuid, id: id}: | ||
log.Println("open ", "sent", uuid, timestamp, id) | ||
default: | ||
// If the write to the events channel would have blocked, discard this | ||
// event instead. | ||
log.Println("open ", "skipped", uuid, timestamp, id) | ||
} | ||
} | ||
|
||
// Close is called by tcp-info synchronously for every TCP close event. | ||
func (h *handler) Close(ctx context.Context, timestamp time.Time, uuid string) { | ||
log.Println("close", uuid, timestamp) | ||
} | ||
|
||
// ProcessOpenEvents reads and processes events received by the open handler. | ||
func (h *handler) ProcessOpenEvents(ctx context.Context) { | ||
for { | ||
select { | ||
case e := <-h.events: | ||
log.Println("processing", e) | ||
case <-ctx.Done(): | ||
log.Println("shutdown") | ||
return | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
defer mainCancel() | ||
|
||
flag.Parse() | ||
rtx.Must(flagx.ArgsFromEnv(flag.CommandLine), "could not get args from environment variables") | ||
|
||
if *eventsocket.Filename == "" { | ||
log.Fatal("-tcpinfo.eventsocket path is required") | ||
} | ||
|
||
h := &handler{events: make(chan event)} | ||
|
||
// Process events received by the eventsocket handler. The goroutine will | ||
// block until an open event occurs or the context is cancelled. | ||
go h.ProcessOpenEvents(mainCtx) | ||
|
||
// Begin listening on the eventsocket for new events, and dispatch them to | ||
// the given handler. | ||
go eventsocket.MustRun(mainCtx, *eventsocket.Filename, h) | ||
|
||
<-mainCtx.Done() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
version: '3.7' | ||
services: | ||
tcpinfo: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
volumes: | ||
- ./local:/local | ||
# NOTE: All service containers will use the same network and IP. All ports | ||
# must be configured on the first. | ||
ports: | ||
- target: 9990 | ||
published: 9990 | ||
protocol: tcp | ||
mode: bridge | ||
- target: 9991 | ||
published: 9991 | ||
protocol: tcp | ||
mode: bridge | ||
command: | ||
- -prometheusx.listen-address=:9990 | ||
- -output=/local/tcpinfo | ||
- -tcpinfo.eventsocket=/local/tcpevents.sock | ||
- -anonymize.ip=none | ||
|
||
example-eventsocket-client: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile.example | ||
volumes: | ||
- ./local:/local | ||
network_mode: "service:tcpinfo" | ||
deploy: | ||
# NOTE: container may fail on startup if it tries to read a | ||
# socket that the above services are not yet listening on. | ||
# So, allow restart. | ||
restart_policy: | ||
condition: any | ||
delay: 5s | ||
command: | ||
- -tcpinfo.eventsocket=/local/tcpevents.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module github.com/m-lab/tcp-info | ||
|
||
go 1.18 | ||
|
||
require ( | ||
github.com/go-test/deep v1.0.6 | ||
github.com/gocarina/gocsv v0.0.0-20200827134620-49f5c3fa2b3e | ||
github.com/m-lab/go v0.1.47 | ||
github.com/m-lab/uuid v0.0.0-20191115203855-549727171666 | ||
github.com/prometheus/client_golang v1.7.1 | ||
github.com/prometheus/client_model v0.2.0 | ||
github.com/vishvananda/netlink v1.1.0 | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a | ||
) | ||
|
||
require ( | ||
github.com/araddon/dateparse v0.0.0-20200409225146-d820a6159ab1 // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.1 // indirect | ||
github.com/golang/protobuf v1.4.2 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect | ||
github.com/prometheus/common v0.10.0 // indirect | ||
github.com/prometheus/procfs v0.1.3 // indirect | ||
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect | ||
google.golang.org/protobuf v1.23.0 // indirect | ||
) |
Oops, something went wrong.