Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
densikat-ea committed Sep 1, 2019
0 parents commit ad1de36
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM golang:latest as build

RUN mkdir -p /go/src/greet
WORKDIR /go/src/greet

RUN go get -d github.com/gorilla/mux && \
go get -d github.com/prometheus/client_golang/prometheus && \
go get -d github.com/prometheus/client_golang/prometheus/promhttp

COPY main.go .

RUN CGO_ENABLED=0 go build -a -installsuffix cgo --ldflags "-s -w" -o /usr/bin/server

FROM alpine

COPY --from=build /usr/bin/server /root/

EXPOSE 8080
WORKDIR /root/

CMD ["./server"]
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"log"
"time"
"net/http"

"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func SayHello(greeting_summary *prometheus.SummaryVec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer r.Body.Close()
code := 500

defer func() {
httpDuration := time.Since(start)
greeting_summary.WithLabelValues("duration").Observe(httpDuration.Seconds())
}()

code = http.StatusBadRequest
if r.Method == "GET" {
code = http.StatusOK
vars := mux.Vars(r)
name := vars["name"]
if (name == "slow") {
time.Sleep(10 * time.Second)
}

greet := fmt.Sprintf("Hello %s \n", name)
w.Write([]byte(greet))
} else {
w.WriteHeader(code)
}
}
}

func main() {
greeting_summary := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "greeting_seconds",
Help: "Time taken for greeting request",
}, []string{"greetings"})

router := mux.NewRouter()
router.Handle("/hello/{name}", SayHello(greeting_summary))
router.Handle("/metrics", promhttp.Handler())
prometheus.Register(greeting_summary)
log.Fatal(http.ListenAndServe(":8080", router))
}

0 comments on commit ad1de36

Please sign in to comment.