Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dudo committed Nov 28, 2023
0 parents commit 1624348
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 0 deletions.
Empty file added .dockerignore
Empty file.
Empty file added .env.sample
Empty file.
10 changes: 10 additions & 0 deletions .github/workflows/docker_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
on:
push:
branches:
- main

name: Publish Image

jobs:
image:
uses: orbservability/.github/.github/workflows/publish_image.yml@main
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.DS_Store

# Ignore node files
node_modules
dist
.cache

# Ignore editor workspace files
.vscode
.sublime-*
.atom
.editorconfig
.idea
.nyc_output
coverage
*.lcov
*.swp
*.swo

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore version control
.tool-versions
.*-version

# Ignore env
/.env
.env.local
compose.override.yml
51 changes: 51 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# First stage: build the executable.
# Start from the official Go image to create a build artifact.
# This is based on Debian and includes standard C libraries.
FROM golang:1.21 AS builder

# Set the working directory outside $GOPATH to enable the support for modules.
WORKDIR /usr/src/app

# Copy the Go Modules manifests and download the dependencies.
# This is done before copying the code to leverage Docker cache layers.
COPY go.* ./
RUN go mod download

# Copy the source code from the current directory to the working directory inside the container.
COPY . .

# Build the binary with full module support and without Cgo.
# Compile the binary statically including all dependencies.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -a -installsuffix cgo -o /go/bin/main .

# Second stage: build the runtime container.
# Start from a scratch image, which is an empty container.
FROM scratch AS runtime

WORKDIR /usr/src/app

# Create a non-root user and group with the user ID and group ID set to 10001
COPY --from=builder /etc/passwd /etc/group /etc/
RUN addgroup --system --gid 10001 nonroot && \
adduser --system --uid 10001 --ingroup nonroot nonroot

# Import the Certificate-Authority certificates for enabling HTTPS.
# This is important for applications that make external HTTPS calls.
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

# Import the compiled binary from the first stage.
COPY --from=builder /go/bin/main .

# Use the nonroot user to run the application
USER nonroot:nonroot

# Declare the environment variable for the application.
# For example, setting the port where the application will run.
# ENV PORT=8080

# Expose the application on port 8080.
# EXPOSE $PORT

# Define the entry point for the docker image.
# This is the command that will be run when the container starts.
ENTRYPOINT ["/usr/src/app/main"]
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Observer

Streams data sources to stdout.

## Usage

```sh
docker compose build
docker compose run --rm go mod tidy
```

## Reading

Learn about the various tech powering this application:

- [Pixie](https://docs.px.dev/about-pixie/what-is-pixie/)
- [eBPF](https://ebpf.io/)
17 changes: 17 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
go: &go
env_file: .env
tty: true
stdin_open: true
build:
context: .
target: builder
entrypoint: go
command: help
volumes:
- .:/usr/src/app:delegated
- gomod:/go/pkg/mod
- ${LOCAL_DEP_PATH:-..}:/local

volumes:
gomod: {}
27 changes: 27 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module orbservability/observer

go 1.21

require px.dev/pxapi v0.5.0

require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.0-20210816181553-5444fa50b93d // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/gofrs/uuid v4.0.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
github.com/lestrrat-go/blackmagic v1.0.0 // indirect
github.com/lestrrat-go/httpcc v1.0.0 // indirect
github.com/lestrrat-go/iter v1.0.1 // indirect
github.com/lestrrat-go/jwx v1.2.17 // indirect
github.com/lestrrat-go/option v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.43.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
197 changes: 197 additions & 0 deletions go.sum

Large diffs are not rendered by default.

93 changes: 93 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"context"
"fmt"
"io"

"px.dev/pxapi"
"px.dev/pxapi/errdefs"
"px.dev/pxapi/types"
)

// Define PxL script with one table output.
var (
pxl = `
import px
# Look at the http_events.
df = px.DataFrame(table='http_events')
# Grab the command line from the metadata.
df.cmdline = px.upid_to_cmdline(df.upid)
# Limit to the first 10.
df = df.head(10)
px.display(df)`
)

func main() {
// Create a Pixie client with local standalonePEM listening address
ctx := context.Background()
client, err := pxapi.NewClient(
ctx,
pxapi.WithDirectAddr("127.0.0.1:12345"),
pxapi.WithDirectCredsInsecure(),
)
if err != nil {
panic(err)
}
// Create a connection to the host.
hostID := "localhost"
vz, err := client.NewVizierClient(ctx, hostID)
if err != nil {
panic(err)
}
// Create TableMuxer to accept results table.
tm := &tableMux{}
// Execute the PxL script.
resultSet, err := vz.ExecuteScript(ctx, pxl, tm)
if err != nil && err != io.EOF {
panic(err)
}
// Receive the PxL script results.
defer resultSet.Close()
if err := resultSet.Stream(); err != nil {
if errdefs.IsCompilationError(err) {
fmt.Printf("Got compiler error: \n %s\n", err.Error())
} else {
fmt.Printf("Got error : %+v, while streaming\n", err)
}
}
// Get the execution stats for the script execution.
stats := resultSet.Stats()
fmt.Printf("Execution Time: %v\n", stats.ExecutionTime)
fmt.Printf("Bytes received: %v\n", stats.TotalBytes)
}

// Satisfies the TableRecordHandler interface.
type tablePrinter struct{}

func (t *tablePrinter) HandleInit(ctx context.Context, metadata types.TableMetadata) error {
return nil
}
func (t *tablePrinter) HandleRecord(ctx context.Context, r *types.Record) error {
for _, d := range r.Data {
fmt.Printf("%s ", d.String())
}
fmt.Printf("\n")
return nil
}

func (t *tablePrinter) HandleDone(ctx context.Context) error {
return nil
}

// Satisfies the TableMuxer interface.
type tableMux struct {
}

func (s *tableMux) AcceptTable(ctx context.Context, metadata types.TableMetadata) (pxapi.TableRecordHandler, error) {
return &tablePrinter{}, nil
}
3 changes: 3 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:build tools

package tools

0 comments on commit 1624348

Please sign in to comment.