Skip to content

Commit

Permalink
[!] restructure code tree, closes #518 (#521)
Browse files Browse the repository at this point in the history
* [!] restructure code tree, closes #518
- get rid of `src` folder
- put `go.mod` to the root folder
- put packages in `internal`
- provide one public `api` package to allow import for rpc sinks
- put `main.go` under `cmd\pgwatch`
- move `utils\convert` to `cmd\pgwatch`
* [*] rename `MeasurementMessage` to `MeasurementEnvelope`
  • Loading branch information
pashagolub authored Aug 26, 2024
1 parent 55735e8 commit 364061e
Show file tree
Hide file tree
Showing 252 changed files with 226 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ updates:

# Maintain dependencies for Go modules
- package-ecosystem: gomod
directory: "src/"
directory: "/"
schedule:
interval: daily
time: "04:00"
Expand Down
23 changes: 10 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,34 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache-dependency-path: 'src/go.sum'
cache-dependency-path: 'go.sum'

- name: Cache npm packages
uses: actions/cache@v4
with:
path: |
src/webui/node_modules
internal/webui/node_modules
key: ${{ runner.os }}-npm-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Build
run: |
cd src/webui
cd internal/webui
yarn install --network-timeout 100000 && yarn build
cd ..
go build
cd -
go build ./cmd/pgwatch
- name: Upload webui as artifact
uses: actions/upload-artifact@v4
with:
name: webui-build
path: src/webui/build
path: internal/webui/build

- name: GolangCI-Lint
uses: golangci/golangci-lint-action@v6
with:
version: latest
working-directory: src

test:
if: true # false to skip job during debug
Expand All @@ -68,24 +67,22 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache-dependency-path: 'src/go.sum'
cache-dependency-path: 'go.sum'

- name: Download webui artifact
uses: actions/download-artifact@v4
with:
name: webui-build
path: src/webui/build
path: internal/webui/build

- name: Test
run: |
cd src
go test -failfast -v -timeout=300s -p 1 -coverprofile=profile.cov ./...
- name: Coveralls
uses: coverallsapp/github-action@v2
with:
base-path: src
file: src/profile.cov
file: profile.cov

goreleaser:
if: true # false to skip job during debug
Expand All @@ -101,7 +98,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache-dependency-path: 'src/go.sum'
cache-dependency-path: 'go.sum'

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ debug

# IntelliJ Idea settings
.idea/

# leftovers
src/metrics/sql/
File renamed without changes.
6 changes: 2 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ version: 2
before:
hooks:
# weirdly goreleaser doesn't all to include outside of the Go build "root" so need to copy
- cp -r grafana ./src/
- sh -c "cd ./src/webui && yarn install && yarn build"
- sh -c "cd ./internal/webui && yarn install && yarn build"

builds:
- dir: src
binary: pgwatch
- main: ./cmd/pgwatch
env:
- CGO_ENABLED=0
goos:
Expand Down
13 changes: 13 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api

import (
"github.com/cybertec-postgresql/pgwatch/v3/internal/metrics"
)

type (
// Metric represents a metric definition
Metric = metrics.Metric
// MeasurementEnvelope represents a collection of measurement messages wrapped up
// with metadata such as metric name, source type, etc.
MeasurementEnvelope = metrics.MeasurementEnvelope
)
File renamed without changes.
28 changes: 5 additions & 23 deletions src/main.go → cmd/pgwatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,12 @@ import (
"sync/atomic"
"syscall"

"github.com/cybertec-postgresql/pgwatch/cmdopts"
"github.com/cybertec-postgresql/pgwatch/log"
"github.com/cybertec-postgresql/pgwatch/reaper"
"github.com/cybertec-postgresql/pgwatch/v3/internal/cmdopts"
"github.com/cybertec-postgresql/pgwatch/v3/internal/log"
"github.com/cybertec-postgresql/pgwatch/v3/internal/reaper"
"github.com/cybertec-postgresql/pgwatch/v3/internal/webui"
)

// version output variables
var (
commit = "unknown"
version = "unknown"
date = "unknown"
dbapi = "00179"
)

func printVersion() {
fmt.Printf(`
Version info:
Version: %s
DB Schema: %s
Git Commit: %s
Built: %s
`, version, dbapi, commit, date)
}

// SetupCloseHandler creates a 'listener' on a new goroutine which will notify the
// program if it receives an interrupt from the OS. We then handle this by calling
// our clean up procedure and exiting the program.
Expand Down Expand Up @@ -108,7 +90,7 @@ func main() {
return
}

if err = opts.InitWebUI(webuifs, logger); err != nil {
if err = opts.InitWebUI(webui.WebUIFs, logger); err != nil {
exitCode.Store(cmdopts.ExitCodeWebUIError)
logger.Error(err)
return
Expand Down
22 changes: 22 additions & 0 deletions cmd/pgwatch/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

// version output variables
var (
commit = "unknown"
version = "unknown"
date = "unknown"
dbapi = "00179"
)

func printVersion() {
fmt.Printf(`
Version info:
Version: %s
DB Schema: %s
Git Commit: %s
Built: %s
`, version, dbapi, commit, date)
}
10 changes: 5 additions & 5 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 1. Build Web UI
# ----------------------------------------------------------------
FROM node:21 AS uibuilder
ADD src/webui /webui
ADD internal/webui /webui
RUN cd webui && yarn install --network-timeout 100000 && yarn build

# ----------------------------------------------------------------
Expand All @@ -14,9 +14,9 @@ ARG VERSION
ARG GIT_HASH
ARG GIT_TIME

ADD src /pgwatch
COPY --from=uibuilder /webui/build /pgwatch/webui/build
RUN cd /pgwatch && CGO_ENABLED=0 go build -ldflags "-X 'main.commit=${GIT_HASH}' -X 'main.date=${GIT_TIME}' -X 'main.version=${VERSION}'"
COPY . /pgwatch
COPY --from=uibuilder /webui/build /pgwatch/internal/webui/build
RUN cd /pgwatch && CGO_ENABLED=0 go build -ldflags "-X 'main.commit=${GIT_HASH}' -X 'main.date=${GIT_TIME}' -X 'main.version=${VERSION}'" ./cmd/pgwatch

# ----------------------------------------------------------------
# 3. Build the final image
Expand All @@ -25,7 +25,7 @@ FROM alpine

# Copy over the compiled gatherer
COPY --from=builder /pgwatch/pgwatch /pgwatch/
COPY src/metrics/metrics.yaml /pgwatch/metrics/metrics.yaml
COPY internal/metrics/metrics.yaml /pgwatch/metrics/metrics.yaml

# Admin UI for configuring servers to be monitored
EXPOSE 8080
Expand Down
10 changes: 5 additions & 5 deletions docker/demo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 1. Build Web UI
# ----------------------------------------------------------------
FROM node:21 AS uibuilder
ADD src/webui /webui
COPY internal/webui /webui
RUN cd webui && yarn install --network-timeout 100000 && yarn build

# ----------------------------------------------------------------
Expand All @@ -14,9 +14,9 @@ ARG VERSION
ARG GIT_HASH
ARG GIT_TIME

ADD src /pgwatch
COPY --from=uibuilder /webui/build /pgwatch/webui/build
RUN cd /pgwatch && CGO_ENABLED=0 go build -ldflags "-X 'main.commit=${GIT_HASH}' -X 'main.date=${GIT_TIME}' -X 'main.version=${VERSION}'"
COPY . /pgwatch
COPY --from=uibuilder /webui/build /pgwatch/internal/webui/build
RUN cd /pgwatch && CGO_ENABLED=0 go build -ldflags "-X 'main.commit=${GIT_HASH}' -X 'main.date=${GIT_TIME}' -X 'main.version=${VERSION}'" ./cmd/pgwatch

# ----------------------------------------------------------------
# 3. Build the final image
Expand All @@ -25,7 +25,7 @@ FROM postgres:16-bullseye AS releaser

# Copy over the compiled gatherer
COPY --from=builder /pgwatch/pgwatch /pgwatch/
COPY src/metrics/metrics.yaml /pgwatch/metrics/metrics.yaml
COPY internal/metrics/metrics.yaml /pgwatch/metrics/metrics.yaml

# Install dependencies
ENV DEBIAN_FRONTEND=noninteractive
Expand Down
6 changes: 3 additions & 3 deletions docs/advanced_features.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ config (for Config DB based setups) looks like:
```

For YAML based setups an example can be found from the
[instances.yaml](https://github.com/cybertec-postgresql/pgwatch/blob/master/src/sources/sample.sources.yaml#L21)
[instances.yaml](https://github.com/cybertec-postgresql/pgwatch/blob/master/internal/sources/sample.sources.yaml#L21)
file.

If Patroni is powered by *etcd*, then also username, password, ca_file,
Expand All @@ -55,7 +55,7 @@ database server logs for errors. Out-of-the-box it will though only work
when logs are written in **CSVLOG** format. For other formats user needs
to specify a regex that parses out named groups of following fields:
*database_name*, *error_severity*. See
[here](https://github.com/cybertec-postgresql/pgwatch/blob/master/src/metrics/logparse.go#L27)
[here](https://github.com/cybertec-postgresql/pgwatch/blob/master/internal/metrics/logparse.go#L27)
for an example regex.

Note that only the event counts are stored, no error texts, usernames or
Expand All @@ -65,7 +65,7 @@ the whole instance. The metric name to enable log parsing is
destination / setting to work, the monitoring user needs superuser /
pg_monitor privileges - if this is not possible then log settings need
to be specified manually under "Host config" as seen for example
[here](https://github.com/cybertec-postgresql/pgwatch/blob/master/src/sources/sample.sources.yaml).
[here](https://github.com/cybertec-postgresql/pgwatch/blob/master/internal/sources/sample.sources.yaml).

**Sample configuration if not using CSVLOG logging:**

Expand Down
2 changes: 1 addition & 1 deletion docs/custom_installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ syntax differences.
daemon

git clone https://github.com/cybertec-postgresql/pgwatch.git
cd pgwatch/src/webui
cd pgwatch/internal/webui
yarn install --network-timeout 100000 && yarn build
cd ..
go build
Expand Down
2 changes: 1 addition & 1 deletion docs/installation_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The default Docker images use this approach.
From v1.4.0 one can deploy the gatherer daemon(s) decentrally with
*hosts to be monitored* defined in simple YAML files. In that case there
is no need for the central Postgres "config DB". See the sample
[instances.yaml](https://github.com/cybertec-postgresql/pgwatch/blob/master/src/sources/sample.sources.yaml)
[instances.yaml](https://github.com/cybertec-postgresql/pgwatch/blob/master/internal/sources/sample.sources.yaml)
config file for an example. Note that in this mode you also need to
point out the path to metric definition SQL files when starting the
gatherer. Also note that the configuration system also supports multiple
Expand Down
2 changes: 1 addition & 1 deletion src/go.mod → go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/cybertec-postgresql/pgwatch
module github.com/cybertec-postgresql/pgwatch/v3

go 1.22.0

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/cmdopts/cmdconfig.go → internal/cmdopts/cmdconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"errors"
"fmt"

"github.com/cybertec-postgresql/pgwatch/metrics"
"github.com/cybertec-postgresql/pgwatch/sources"
"github.com/cybertec-postgresql/pgwatch/v3/internal/metrics"
"github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
)

type ConfigCommand struct {
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/cmdopts/cmdoptions.go → internal/cmdopts/cmdoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"os"
"time"

"github.com/cybertec-postgresql/pgwatch/log"
"github.com/cybertec-postgresql/pgwatch/metrics"
"github.com/cybertec-postgresql/pgwatch/sinks"
"github.com/cybertec-postgresql/pgwatch/sources"
"github.com/cybertec-postgresql/pgwatch/webserver"
"github.com/cybertec-postgresql/pgwatch/v3/internal/log"
"github.com/cybertec-postgresql/pgwatch/v3/internal/metrics"
"github.com/cybertec-postgresql/pgwatch/v3/internal/sinks"
"github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
"github.com/cybertec-postgresql/pgwatch/v3/internal/webserver"
"github.com/jackc/pgx/v5"
flags "github.com/jessevdk/go-flags"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"testing"

"github.com/cybertec-postgresql/pgwatch/log"
"github.com/cybertec-postgresql/pgwatch/v3/internal/log"
flags "github.com/jessevdk/go-flags"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion src/cmdopts/cmdsource.go → internal/cmdopts/cmdsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

"github.com/cybertec-postgresql/pgwatch/sources"
"github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
)

type SourceCommand struct {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/db/bootstrap.go → internal/db/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

"github.com/cybertec-postgresql/pgwatch/log"
"github.com/cybertec-postgresql/pgwatch/v3/internal/log"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
Expand Down
2 changes: 1 addition & 1 deletion src/db/bootstrap_test.go → internal/db/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cybertec-postgresql/pgwatch/db"
"github.com/cybertec-postgresql/pgwatch/v3/internal/db"
testcontainers "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/db/conn_test.go → internal/db/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

"github.com/cybertec-postgresql/pgwatch/db"
"github.com/cybertec-postgresql/pgwatch/v3/internal/db"
)

func TestMarshallParam(t *testing.T) {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/log/formatter.go → internal/log/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
}

func trimFilename(s string) string {
const sub = "src/"
const sub = "internal/"
idx := strings.Index(s, sub)
if idx == -1 {
return s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"testing"

formatter "github.com/cybertec-postgresql/pgwatch/log"
formatter "github.com/cybertec-postgresql/pgwatch/v3/internal/log"
"github.com/sirupsen/logrus"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 364061e

Please sign in to comment.