Skip to content

Commit

Permalink
feat: log usage metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jul 15, 2024
1 parent da820b1 commit d5ade49
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ jobs:
working-directory: client
run: ./coap-client -udp6 -udpPort 5683 -dTLSPort 5684

- name: Get log file
run: |
LOG_FILES=`docker exec ${{ env.CONTAINER_ID }} bash -c 'ls /var/log/academy/'`
for file in "${LOG_FILES[@]}"; do
docker cp ${{ env.CONTAINER_ID }}:/var/log/academy/$file ./
done
- name: Test log file
run: |
LOG_FILE=(*-coap.log)
if [[ -f $LOG_FILE ]]; then
echo "Log file exists: $LOG_FILE"
else
echo "Log file does not exist: $LOG_FILE"
# exit 1
fi
grep -E "^20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+[^,]+,dTLS:udp6,request$" $LOG_FILE
grep -E "^20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+[^,]+,UDP:udp6,request$" $LOG_FILE
grep -E "^20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+[^,]+,dTLS:udp4,request$" $LOG_FILE
grep -E "^20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+[^,]+,UDP:udp4,request$" $LOG_FILE
- uses: actions/upload-artifact@v4
with:
name: metric-logs-${{ github.sha }}
path: |
*-coap.log
- name: Docker logs
if: always()
run: docker container logs ${{ env.CID }}
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN <<EOT
update-ca-certificates
apt-get -y clean
rm -rf /var/lib/apt/lists/*
mkdir /var/log/academy
EOT

RUN useradd -rm -d /home/coap -s /bin/bash -u 1002 coap
Expand Down
36 changes: 34 additions & 2 deletions server/internal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"fmt"
"io"
"log"
"time"
"os"
"strings"
"time"

"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
Expand Down Expand Up @@ -87,10 +88,41 @@ func dynamicResource(client *azblob.Client, containerName string) func(mux.Respo
}
}

/**
* Log usage metrics in the format that is supported by Log Analytics agent in Azure Monitor
*
* See https://learn.microsoft.com/en-us/azure/azure-monitor/agents/data-sources-custom-logs
*/
func logMetrics (dtls bool, network string) func (next mux.Handler) mux.Handler {
return func (next mux.Handler) mux.Handler {
return mux.HandlerFunc(func(w mux.ResponseWriter, r *mux.Message) {
currentTime := time.Now().Format("2006-01-02T15:04:05Z07:00")
var protocol string
if dtls {
protocol = "dTLS"
} else {
protocol = "UDP"
}
metricLog := fmt.Sprintf("%s,%s:%s,request", currentTime, protocol, network)
metricLogsFilePath := fmt.Sprintf("/var/log/academy/%s-coap.log", time.Now().Format("2006-01-02"))
metricLogsFile, err := os.OpenFile(metricLogsFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("cannot open log file: %v", err)
}
defer metricLogsFile.Close()
_, err = metricLogsFile.WriteString(metricLog + "\n")
if err != nil {
log.Printf("cannot write to log file: %v", err)
}
next.ServeCOAP(w, r)
})
}
}

func NewServer(client *azblob.Client, containerName string) *mux.Router {
func NewServer(client *azblob.Client, containerName string, dtls bool, network string) *mux.Router {
r := mux.NewRouter()
r.Use(loggingMiddleware)
r.Use(logMetrics(dtls, network))
r.Handle("/static/hello", mux.HandlerFunc(helloResource))
r.Handle("/{res:[^\\/]+}", mux.HandlerFunc(dynamicResource(client, containerName)))
return r
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {
}
log.Printf("Container name: %s\n", containerName)

r := internal.NewServer(storageClient, containerName)
r := internal.NewServer(storageClient, containerName, *dtls, *network)

// old ports
go launch.Server(flagValues, ports.OldUdpPort, ports.OldDtlsPort, r)
Expand Down

0 comments on commit d5ade49

Please sign in to comment.