Skip to content

Commit

Permalink
feat: add support to send system hostname for identification
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoysrt committed Mar 18, 2024
1 parent 0e3c0ae commit c0ea45e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build go build -o goapp .

# final stage
FROM --platform=$BUILDPLATFORM ubuntu:22.04
RUN mkdir /app
RUN mkdir /data
RUN mkdir -p /app/etc
WORKDIR /app
COPY --from=build-env /src/goapp /app/goapp
RUN apt-get update && apt-get install -y ca-certificates && apt-get clean
Expand Down
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ It will collect system stats and usage stats of all running swarm services and w

**Volume Bind**
- bind **<host docker socket path>** to -> **/var/run/docker.sock**

- bind /etc/hostname to /app/etc/hostname (read only)
### License
Apache License 2.0
33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"
)

Expand All @@ -18,6 +19,9 @@ func main() {
* DOCKER_HOST: unix or tcp socket to connect to
* This will send the stats to the endpoint using the authorization header
*
* Configure Volume Mounts
* <docker socket of host>:/var/run/docker.sock
* /etc/hostname:/app/etc/hostname:ro
*/
submissionEndpoint := os.Getenv("SUBMISSION_ENDPOINT")
authorizationHeaderVal := os.Getenv("AUTHORIZATION_HEADER_VAL")
Expand All @@ -35,6 +39,12 @@ func main() {
log.Println("Error creating docker client:")
panic(err)
}
// fetch hostname
hostname, err := getHostName()
if err != nil {
log.Println("Error fetching hostname: ")
panic(err)
}
for {
<-time.After(1 * time.Minute)
// fetch stats
Expand All @@ -43,6 +53,8 @@ func main() {
log.Println("Error fetching stats: ", err)
continue
}
// set hostname
statsData.Hostname = hostname
// convert to json
jsonData, err := statsData.JSON()
if err != nil {
Expand All @@ -58,7 +70,7 @@ func main() {
}
}

// private function to send stats to the endpoint
// private functions
func sendStats(submissionEndpoint string, authorizationHeaderVal string, jsonData []byte) error {
// convert jsonData to a reader
body := bytes.NewReader(jsonData)
Expand All @@ -82,3 +94,22 @@ func sendStats(submissionEndpoint string, authorizationHeaderVal string, jsonDat
}(resp.Body)
return nil
}

func getHostName() (string, error) {
fileName := "/app/etc/hostname"
file, err := os.Open(fileName)
if err != nil {
return "", err
}
defer func(file *os.File) {
_ = file.Close()
}(file)
buf := make([]byte, 1000)
n, err := file.Read(buf)
if err != nil {
return "", err
}
h := string(buf[:n])
h = strings.TrimSpace(h)
return h, nil
}
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type StatsData struct {
Hostname string `json:"hostname"`
SystemStat host.ResourceStats `json:"system"`
ServiceStats map[string]*service.ResourceStats `json:"services"`
TimeStamp uint64 `json:"timestamp"`
Expand Down

0 comments on commit c0ea45e

Please sign in to comment.