Skip to content

Commit afd1f60

Browse files
authored
Merge pull request #16 from codiewio/feat/metrics
Add Prometheus metrics
2 parents 033987b + 29b3cfe commit afd1f60

File tree

17 files changed

+226
-59
lines changed

17 files changed

+226
-59
lines changed

.github/workflows/lint.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- name: Lint Playground
3232
uses: golangci/golangci-lint-action@v6
3333
with:
34-
version: v1.63.4
34+
version: v1.64.5
3535

3636

3737
# Sandbox
@@ -42,5 +42,5 @@ jobs:
4242
- name: Lint sandbox
4343
uses: golangci/golangci-lint-action@v6
4444
with:
45-
version: v1.63.4
45+
version: v1.64.5
4646
working-directory: sandbox

.golangci.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ run:
66
# When enabled linter will skip directories: vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
77
# Skipping `examples` sounds scary to me but skipping `testdata` sounds ok.
88

9-
109
# This file contains only configs which differ from defaults.
1110
# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
1211
linters-settings:
@@ -172,7 +171,7 @@ linters:
172171
- gocritic # provides diagnostics that check for bugs, performance and style issues
173172
- gocyclo # computes and checks the cyclomatic complexity of functions
174173
# - godot # checks if comments end in a period
175-
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
174+
# - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
176175
# - mnd # detects magic numbers
177176
- gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod
178177
- 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
@@ -190,7 +189,7 @@ linters:
190189
# - nonamedreturns # reports all named returns
191190
- nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL
192191
- predeclared # finds code that shadows one of Go's predeclared identifiers
193-
- promlinter # checks Prometheus metrics naming via promlint
192+
# - promlinter # checks Prometheus metrics naming via promlint
194193
- reassign # checks that package variables are not reassigned
195194
# - revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
196195
- rowserrcheck # checks whether Err of rows is checked successfully

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Initial stage: download modules
2-
FROM golang:1.23 as modules
2+
FROM golang:1.24 as modules
33

44
ADD go.mod go.sum /m/
55
RUN cd /m && go mod download
66

77
# Intermediate stage: Build the binary
8-
FROM golang:1.23 as builder
8+
FROM golang:1.24 as builder
99

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

@@ -45,7 +45,7 @@ LABEL org.opencontainers.image.description="The open-source sandbox based on Doc
4545
LABEL org.opencontainers.image.licenses="Apache-2.0"
4646
LABEL org.opencontainers.image.revision="${LABEL_COMMIT}"
4747
LABEL org.opencontainers.image.source="https://github.com/codiewio/codenire"
48-
LABEL org.opencontainers.image.title="Codenire"
49-
LABEL org.opencontainers.image.url="https://codenire.com"
50-
#LABEL org.opencontainers.image.vendor="Codiew INC"
48+
LABEL org.opencontainers.image.title="Codenire Playground"
49+
LABEL org.opencontainers.image.url="https://codenire.io"
50+
LABEL org.opencontainers.image.vendor="I/E Maksim Fedorov"
5151
LABEL org.opencontainers.image.version="${LABEL_VERSION}"

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ require (
1515
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
1616
github.com/goccy/go-json v0.10.3 // indirect
1717
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
18-
github.com/google/go-cmp v0.5.9 // indirect
18+
github.com/google/go-cmp v0.6.0 // indirect
1919
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
2020
github.com/lestrrat-go/httpcc v1.0.1 // indirect
2121
github.com/lestrrat-go/httprc v1.0.6 // indirect
2222
github.com/lestrrat-go/iter v1.0.2 // indirect
2323
github.com/lestrrat-go/jwx/v2 v2.1.3 // indirect
2424
github.com/lestrrat-go/option v1.0.1 // indirect
2525
github.com/segmentio/asm v1.2.0 // indirect
26+
github.com/stretchr/testify v1.10.0 // indirect
2627
golang.org/x/crypto v0.32.0 // indirect
2728
golang.org/x/net v0.34.0 // indirect
2829
golang.org/x/sync v0.11.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
4343
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4444
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
4545
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
46-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
47-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
46+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
47+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4848
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4949
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
5050
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
@@ -70,8 +70,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
7070
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
7171
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
7272
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
73-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
74-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
73+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
74+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7575
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
7676
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
7777
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

internal/handler/server.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
package handler
66

77
import (
8+
"context"
89
"fmt"
910
"io"
1011
"net/http"
1112
"time"
1213

14+
"github.com/codiewio/codenire/internal/client"
1315
"github.com/go-chi/chi/v5"
1416
"github.com/go-chi/chi/v5/middleware"
1517
"github.com/go-chi/cors"
@@ -78,6 +80,36 @@ func NewServer(config *Config) (*http.Server, error) {
7880
})
7981
})
8082

83+
router.Get("/metrics", func(w http.ResponseWriter, r *http.Request) {
84+
req, err := http.NewRequestWithContext(
85+
context.Background(),
86+
http.MethodGet,
87+
handler.Config.BackendURL+"/metrics",
88+
nil,
89+
)
90+
91+
if err != nil {
92+
http.Error(w, "sandbox client metrics request error", http.StatusInternalServerError)
93+
return
94+
}
95+
96+
resp, err := client.SandboxBackendClient().Do(req)
97+
if err != nil {
98+
http.Error(w, "Failed to fetch metrics from other service", http.StatusInternalServerError)
99+
return
100+
}
101+
defer func() {
102+
_ = resp.Body.Close()
103+
}()
104+
105+
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
106+
_, err = io.Copy(w, resp.Body)
107+
if err != nil {
108+
http.Error(w, "Failed to write metrics to response", http.StatusInternalServerError)
109+
return
110+
}
111+
})
112+
81113
return &http.Server{
82114
Addr: ":" + config.Port,
83115
ReadHeaderTimeout: 5 * time.Second,

main.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,17 @@ import (
4545
)
4646

4747
var (
48-
backendURL = flag.String("backend-url", "http://sandbox_dev", "URL for sandbox backend that runs Go binaries.")
49-
Port = flag.String("port", "8081", "URL for sandbox backend that runs Go binaries.")
50-
PluginHookPath = flag.String("hooks-plugins", "", "URL for sandbox backend that runs Go binaries.")
51-
FileHooksDir = flag.String("hooks-dir", "", "Directory to search for available hooks scripts")
48+
backendURL = flag.String("backend-url", "http://sandbox_dev", "URL for sandbox backend that runs Go binaries.")
49+
Port = flag.String("port", "8081", "URL for sandbox backend that runs Go binaries.")
50+
PluginHookPath = flag.String("hooks-plugins", "", "URL for sandbox backend that runs Go binaries.")
51+
FileHooksDir = flag.String("hooks-dir", "", "Directory to search for available hooks scripts")
52+
53+
// deprecated
5254
ExternalTemplates = flag.String("external-templates", "", "Comma separated list of templates which will handled externally (plugin for example)")
53-
ThrottleLimit = flag.Int("throttle-limit", 15, "currently processed requests at a time across all users")
54-
JWTSecretKey = flag.String("jwt-secret-key", "", "secret key to enable authentication")
55-
dev = flag.Bool("dev", false, "run in dev mode")
55+
56+
ThrottleLimit = flag.Int("throttle-limit", 15, "currently processed requests at a time across all users")
57+
JWTSecretKey = flag.String("jwt-secret-key", "", "secret key to enable authentication")
58+
dev = flag.Bool("dev", false, "run in dev mode")
5659

5760
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.")
5861
CorsAllowCredentials = flag.Bool("cors-allow-credentials", false, "Allow credentials by setting Access-Control-Allow-Credentials: true")

sandbox/Dockerfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
# environment so the sandbox server can connect to the host's
55
# docker daemon, which has the gvisor "runsc" runtime available.
66

7-
FROM golang:1.22 AS modules
7+
FROM golang:1.24 AS modules
88

99
ADD go.mod go.sum /m/
1010
RUN cd /m && go mod download
1111

1212

1313
# Intermediate stage: Build the binary
14-
FROM golang:1.22 as builder
14+
FROM golang:1.24 as builder
1515

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

@@ -57,3 +57,13 @@ COPY --from=builder /app/bin/sandbox /usr/local/bin/sandbox
5757
ADD dockerfiles /dockerfiles
5858

5959
ENTRYPOINT ["/usr/local/bin/sandbox"]
60+
61+
62+
LABEL org.opencontainers.image.description="The open-source sandbox based on Docker containers and Google gVisor."
63+
LABEL org.opencontainers.image.licenses="Apache-2.0"
64+
LABEL org.opencontainers.image.revision="${LABEL_COMMIT}"
65+
LABEL org.opencontainers.image.source="https://github.com/codiewio/codenire"
66+
LABEL org.opencontainers.image.title="Codenire Sandbox"
67+
LABEL org.opencontainers.image.url="https://codenire.io"
68+
LABEL org.opencontainers.image.vendor="I/E Maksim Fedorov"
69+
LABEL org.opencontainers.image.version="${LABEL_VERSION}"

sandbox/dockerfiles/golang_1_23/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"ContainerOptions": {
88
"CompileTTL": 30,
99
"RunTTL": 5,
10-
"MemoryLimit": 524288000
10+
"MemoryLimit": 314572800
1111
},
1212
"IsSupportPackage": true,
1313

sandbox/dockerfiles/golang_1_24/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"ContainerOptions": {
88
"CompileTTL": 30,
99
"RunTTL": 5,
10-
"MemoryLimit": 524288000
10+
"MemoryLimit": 314572800
1111
},
1212
"IsSupportPackage": true,
1313

0 commit comments

Comments
 (0)