Skip to content

Commit

Permalink
Use statik to generate statusz template as code. (Stackdriver#206)
Browse files Browse the repository at this point in the history
* Moving var statuszTmpl to func ServeHTTP

When trying to run $GOPATH/bin/stackdriver-prometheus-sidecar --help ,I get error panic: open statusz-tmpl.html: no such file or directory.
Moving the intialization of the statuszTmpl to func ServeHTTP which allows the use of --help

* Moving statusz-tmpl.html to a const in statusz.go and updating related files.

* Using statik as a code generator

* Updating statusz.go

* Updating statusz.go and main.go

* Updating statusz.go

* Adding dependcies from running go mod vendor

* Updating statik

Co-authored-by: Javier Kohen <jkohen@gmail.com>
  • Loading branch information
2 people authored and msukruth committed Jun 2, 2020
1 parent 206c03e commit a959b79
Show file tree
Hide file tree
Showing 9 changed files with 539 additions and 2 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ FROM gcr.io/distroless/static:latest
LABEL maintainer "Stackdriver Engineering <engineering@stackdriver.com>"

COPY stackdriver-prometheus-sidecar /bin/stackdriver-prometheus-sidecar
COPY cmd/stackdriver-prometheus-sidecar/statusz-tmpl.html /statusz-tmpl.html

EXPOSE 9091
ENTRYPOINT [ "/bin/stackdriver-prometheus-sidecar" ]
12 changes: 12 additions & 0 deletions cmd/stackdriver-prometheus-sidecar/statik/statik.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion cmd/stackdriver-prometheus-sidecar/statusz.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:generate statik -f -src=./ -include=*.html
package main

import (
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"

_ "github.com/Stackdriver/stackdriver-prometheus-sidecar/cmd/stackdriver-prometheus-sidecar/statik"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/common/version"
"github.com/rakyll/statik/fs"
)

var (
serverStart = time.Now()
statuszTmpl = template.Must(template.ParseFiles("statusz-tmpl.html"))
statuszTmpl *template.Template
)

type statuszHandler struct {
Expand All @@ -36,6 +41,25 @@ type statuszHandler struct {
cfg *mainConfig
}

func init() {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
statuszFile, err := statikFS.Open("/statusz-tmpl.html")
if err != nil {
panic(err)
}
contents, err := ioutil.ReadAll(statuszFile)
if err != nil {
panic(err)
}
statuszTmpl, err = template.New("statusz-tmpl.html").Parse(string(contents))
if err != nil {
panic(err)
}
}

func (h *statuszHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var data struct {
ServerName string
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ require (
github.com/prometheus/procfs v0.0.3 // indirect
github.com/prometheus/prometheus v0.0.0-20190710134608-e5b22494857d
github.com/prometheus/tsdb v0.10.0
github.com/rakyll/statik v0.1.6
github.com/rogpeppe/go-internal v1.5.0 // indirect
github.com/samuel/go-zookeeper v0.0.0-20190801204459-3c104360edc8 // indirect
github.com/shopspring/decimal v0.0.0-20191125035519-b054a8dfd10d // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ github.com/prometheus/prometheus v0.0.0-20190710134608-e5b22494857d/go.mod h1:11
github.com/prometheus/tsdb v0.9.1/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/prometheus/tsdb v0.10.0 h1:If5rVCMTp6W2SiRAQFlbpJNgVlgMEd+U2GZckwK38ic=
github.com/prometheus/tsdb v0.10.0/go.mod h1:oi49uRhEe9dPUTlS3JRZOwJuVi6tmh10QSgwXEyGCt4=
github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rlmcpherson/s3gof3r v0.5.0/go.mod h1:s7vv7SMDPInkitQMuZzH615G7yWHdrU2r/Go7Bo71Rs=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down
202 changes: 202 additions & 0 deletions vendor/github.com/rakyll/statik/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a959b79

Please sign in to comment.