Skip to content

Commit

Permalink
Use statik to generate statusz template as code. (#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 <[email protected]>
  • Loading branch information
fifiug and jkohen authored Feb 5, 2020
1 parent b0ee70e commit 2548e8a
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 <[email protected]>"

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 2548e8a

Please sign in to comment.