Skip to content

Commit

Permalink
Merge pull request #16 from codiewio/feat/metrics
Browse files Browse the repository at this point in the history
Add Prometheus metrics
  • Loading branch information
halabooda authored Feb 23, 2025
2 parents 033987b + 29b3cfe commit afd1f60
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Lint Playground
uses: golangci/golangci-lint-action@v6
with:
version: v1.63.4
version: v1.64.5


# Sandbox
Expand All @@ -42,5 +42,5 @@ jobs:
- name: Lint sandbox
uses: golangci/golangci-lint-action@v6
with:
version: v1.63.4
version: v1.64.5
working-directory: sandbox
5 changes: 2 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ run:
# When enabled linter will skip directories: vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
# Skipping `examples` sounds scary to me but skipping `testdata` sounds ok.


# This file contains only configs which differ from defaults.
# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
linters-settings:
Expand Down Expand Up @@ -172,7 +171,7 @@ linters:
- gocritic # provides diagnostics that check for bugs, performance and style issues
- gocyclo # computes and checks the cyclomatic complexity of functions
# - godot # checks if comments end in a period
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
# - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
# - mnd # detects magic numbers
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
- gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
Expand All @@ -190,7 +189,7 @@ linters:
# - nonamedreturns # reports all named returns
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
- predeclared # finds code that shadows one of Go's predeclared identifiers
- promlinter # checks Prometheus metrics naming via promlint
# - promlinter # checks Prometheus metrics naming via promlint
- reassign # checks that package variables are not reassigned
# - revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
- rowserrcheck # checks whether Err of rows is checked successfully
Expand Down
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Initial stage: download modules
FROM golang:1.23 as modules
FROM golang:1.24 as modules

Check warning on line 2 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-docker

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

ADD go.mod go.sum /m/
RUN cd /m && go mod download

# Intermediate stage: Build the binary
FROM golang:1.23 as builder
FROM golang:1.24 as builder

Check warning on line 8 in Dockerfile

View workflow job for this annotation

GitHub Actions / build-docker

The 'as' keyword should match the case of the 'from' keyword

FromAsCasing: 'as' and 'FROM' keywords' casing do not match More info: https://docs.docker.com/go/dockerfile/rule/from-as-casing/

COPY --from=modules /go/pkg /go/pkg

Expand Down Expand Up @@ -45,7 +45,7 @@ LABEL org.opencontainers.image.description="The open-source sandbox based on Doc
LABEL org.opencontainers.image.licenses="Apache-2.0"
LABEL org.opencontainers.image.revision="${LABEL_COMMIT}"
LABEL org.opencontainers.image.source="https://github.com/codiewio/codenire"
LABEL org.opencontainers.image.title="Codenire"
LABEL org.opencontainers.image.url="https://codenire.com"
#LABEL org.opencontainers.image.vendor="Codiew INC"
LABEL org.opencontainers.image.title="Codenire Playground"
LABEL org.opencontainers.image.url="https://codenire.io"
LABEL org.opencontainers.image.vendor="I/E Maksim Fedorov"
LABEL org.opencontainers.image.version="${LABEL_VERSION}"
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.6 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/jwx/v2 v2.1.3 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/net v0.34.0 // indirect
golang.org/x/sync v0.11.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
Expand All @@ -70,8 +70,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
32 changes: 32 additions & 0 deletions internal/handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package handler

import (
"context"
"fmt"
"io"
"net/http"
"time"

"github.com/codiewio/codenire/internal/client"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
Expand Down Expand Up @@ -78,6 +80,36 @@ func NewServer(config *Config) (*http.Server, error) {
})
})

router.Get("/metrics", func(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequestWithContext(
context.Background(),
http.MethodGet,
handler.Config.BackendURL+"/metrics",
nil,
)

if err != nil {
http.Error(w, "sandbox client metrics request error", http.StatusInternalServerError)
return
}

resp, err := client.SandboxBackendClient().Do(req)
if err != nil {
http.Error(w, "Failed to fetch metrics from other service", http.StatusInternalServerError)
return
}
defer func() {
_ = resp.Body.Close()
}()

w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
_, err = io.Copy(w, resp.Body)
if err != nil {
http.Error(w, "Failed to write metrics to response", http.StatusInternalServerError)
return
}
})

return &http.Server{
Addr: ":" + config.Port,
ReadHeaderTimeout: 5 * time.Second,
Expand Down
17 changes: 10 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ import (
)

var (
backendURL = flag.String("backend-url", "http://sandbox_dev", "URL for sandbox backend that runs Go binaries.")
Port = flag.String("port", "8081", "URL for sandbox backend that runs Go binaries.")
PluginHookPath = flag.String("hooks-plugins", "", "URL for sandbox backend that runs Go binaries.")
FileHooksDir = flag.String("hooks-dir", "", "Directory to search for available hooks scripts")
backendURL = flag.String("backend-url", "http://sandbox_dev", "URL for sandbox backend that runs Go binaries.")
Port = flag.String("port", "8081", "URL for sandbox backend that runs Go binaries.")
PluginHookPath = flag.String("hooks-plugins", "", "URL for sandbox backend that runs Go binaries.")
FileHooksDir = flag.String("hooks-dir", "", "Directory to search for available hooks scripts")

// deprecated
ExternalTemplates = flag.String("external-templates", "", "Comma separated list of templates which will handled externally (plugin for example)")
ThrottleLimit = flag.Int("throttle-limit", 15, "currently processed requests at a time across all users")
JWTSecretKey = flag.String("jwt-secret-key", "", "secret key to enable authentication")
dev = flag.Bool("dev", false, "run in dev mode")

ThrottleLimit = flag.Int("throttle-limit", 15, "currently processed requests at a time across all users")
JWTSecretKey = flag.String("jwt-secret-key", "", "secret key to enable authentication")
dev = flag.Bool("dev", false, "run in dev mode")

CorsAllowOrigin = flag.String("cors-allow-origin", "*", "Regular expression used to determine if the Origin header is allowed. If not, no CORS headers will be sent. By default, all origins are allowed.")
CorsAllowCredentials = flag.Bool("cors-allow-credentials", false, "Allow credentials by setting Access-Control-Allow-Credentials: true")
Expand Down
14 changes: 12 additions & 2 deletions sandbox/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
# environment so the sandbox server can connect to the host's
# docker daemon, which has the gvisor "runsc" runtime available.

FROM golang:1.22 AS modules
FROM golang:1.24 AS modules

ADD go.mod go.sum /m/
RUN cd /m && go mod download


# Intermediate stage: Build the binary
FROM golang:1.22 as builder
FROM golang:1.24 as builder

COPY --from=modules /go/pkg /go/pkg

Expand Down Expand Up @@ -57,3 +57,13 @@ COPY --from=builder /app/bin/sandbox /usr/local/bin/sandbox
ADD dockerfiles /dockerfiles

ENTRYPOINT ["/usr/local/bin/sandbox"]


LABEL org.opencontainers.image.description="The open-source sandbox based on Docker containers and Google gVisor."
LABEL org.opencontainers.image.licenses="Apache-2.0"
LABEL org.opencontainers.image.revision="${LABEL_COMMIT}"
LABEL org.opencontainers.image.source="https://github.com/codiewio/codenire"
LABEL org.opencontainers.image.title="Codenire Sandbox"
LABEL org.opencontainers.image.url="https://codenire.io"
LABEL org.opencontainers.image.vendor="I/E Maksim Fedorov"
LABEL org.opencontainers.image.version="${LABEL_VERSION}"
2 changes: 1 addition & 1 deletion sandbox/dockerfiles/golang_1_23/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"ContainerOptions": {
"CompileTTL": 30,
"RunTTL": 5,
"MemoryLimit": 524288000
"MemoryLimit": 314572800
},
"IsSupportPackage": true,

Expand Down
2 changes: 1 addition & 1 deletion sandbox/dockerfiles/golang_1_24/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"ContainerOptions": {
"CompileTTL": 30,
"RunTTL": 5,
"MemoryLimit": 524288000
"MemoryLimit": 314572800
},
"IsSupportPackage": true,

Expand Down
1 change: 1 addition & 0 deletions sandbox/dockerfiles/run_php.http
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Content-Type: application/json
},
"stdin": "123",
"externalOptions": {
"CompileCmd": "curl -I https://4.ident.me"
}
}
18 changes: 13 additions & 5 deletions sandbox/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module sandbox

go 1.22
go 1.24

require (
github.com/alitto/pond/v2 v2.1.4
Expand All @@ -9,6 +9,7 @@ require (
github.com/docker/docker v27.3.1+incompatible
github.com/go-chi/chi/v5 v5.1.0
github.com/jackc/pgx/v4 v4.18.3
github.com/prometheus/client_golang v1.21.0
go.opencensus.io v0.24.0
go.uber.org/automaxprocs v1.6.0
)
Expand All @@ -24,6 +25,8 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.12 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.12 // indirect
github.com/aws/smithy-go v1.22.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
Expand All @@ -40,24 +43,29 @@ require (
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.57.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
)
Loading

0 comments on commit afd1f60

Please sign in to comment.