Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 6a67c7b

Browse files
authored
Merge pull request #436 from aissarmurad/feat/add-golang-example
feat(examples): add golang example
2 parents 88a081a + e126a63 commit 6a67c7b

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

tools/docker-compose/docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ services:
6666
- 8084:8080
6767
networks:
6868
- phlare
69+
golang:
70+
build:
71+
context: golang
72+
dockerfile: Dockerfile
73+
ports:
74+
- 8085:6060
75+
networks:
76+
- phlare
6977

7078
volumes:
7179
data:
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM golang as builder
2+
3+
WORKDIR /app
4+
5+
# Copy the source-code and Compile the code.
6+
COPY main.go .
7+
COPY go.mod .
8+
RUN CGO_ENABLED=0 GOOS=linux go build
9+
10+
FROM alpine
11+
12+
# Copy over the directory containing the compiled binary for the profiling.
13+
COPY --from=builder /app/golang /golang
14+
RUN ls -al
15+
16+
EXPOSE 6060
17+
# Run the application when the docker image is run, using either CMD (as is done
18+
# here) or ENTRYPOINT.
19+
CMD ./golang

tools/docker-compose/golang/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module golang
2+
3+
go 1.19
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
_ "net/http/pprof"
8+
"time"
9+
)
10+
11+
// generate a range to iterate over it
12+
func makeRange(min, max int) []int {
13+
a := make([]int, max-min+1)
14+
for i := range a {
15+
a[i] = min + i
16+
}
17+
return a
18+
}
19+
20+
// list all primeNumber from 1 to n
21+
func primeNumberFrom1To(to int) []int {
22+
if to == 0 {
23+
to = 100
24+
}
25+
26+
a := [0]int{}
27+
prime_numbers := a[:]
28+
29+
r1 := makeRange(1, to)
30+
for _, i := range r1 {
31+
if i > 1 {
32+
r2 := makeRange(2, i)
33+
for _, j := range r2 {
34+
if i != j {
35+
if divisible(i, j) {
36+
break
37+
}
38+
} else {
39+
prime_numbers = append(prime_numbers, i)
40+
break
41+
}
42+
}
43+
}
44+
}
45+
return prime_numbers
46+
}
47+
48+
// test if i is divisible by j (integer division)
49+
func divisible(i, j int) bool {
50+
return i%j == 0
51+
52+
}
53+
54+
func main() {
55+
// goroutine to start the server profile
56+
go func() {
57+
log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
58+
}()
59+
60+
// loop to continually search for prime numbers
61+
to := 10_000
62+
for {
63+
res := primeNumberFrom1To(to)
64+
fmt.Printf("there are %d prime numbers from 1 to %d\n", len(res), to)
65+
time.Sleep(500 * time.Millisecond)
66+
}
67+
}

tools/docker-compose/phlare.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ scrape_configs:
4848
mutex: { enabled: false }
4949
memory:
5050
path: /debug/pprof/heap
51+
- job_name: "golang"
52+
scrape_interval: "15s"
53+
static_configs:
54+
- targets: ["golang:6060"]

0 commit comments

Comments
 (0)