Skip to content

Commit

Permalink
Using statik as a code generator
Browse files Browse the repository at this point in the history
  • Loading branch information
fifiug committed Jan 26, 2020
1 parent e1bb312 commit 32608ad
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 129 deletions.
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.

124 changes: 124 additions & 0 deletions cmd/stackdriver-prometheus-sidecar/statusz-tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!DOCTYPE html>
<html>
<head>
<title>Status for {{.ServerName}}</title>
<style>
body {
font-family: sans-serif;
}
h1 {
clear: both;
width: 100%;
text-align: center;
font-size: 120%;
background: #eef;
}
h2 {
font-size: 110%;
}
.lefthand {
float: left;
width: 80%;
}
.righthand {
text-align: right;
}
td, th {
background-color: rgba(0, 0, 0, 0.05);
}
th {
text-align: left;
}
</style>
</head>

<body>
<h1>Status for {{.ServerName}}</h1>

<div>
<div class="lefthand">
Started: {{.StartTime}}<br>
Up {{.Uptime}}<br>
Version: {{.VersionInfo}}<br>
Build context: {{.BuildContext}}<br>
Host details: {{.Uname}}<br>
FD limits: {{.FdLimits}}<br>
{{if (and .GKEInfo.ProjectId .GKEInfo.ClusterLocation .GKEInfo.ClusterName .PodName .NodeName .NamespaceName)}}
<p>
Pod <a href="https://console.cloud.google.com/kubernetes/pod/{{.GKEInfo.ClusterLocation}}/{{.GKEInfo.ClusterName}}/{{if .NamespaceName}}{{.NamespaceName}}{{else}}default{{end}}/{{.PodName}}?project={{.GKEInfo.ProjectId}}">{{.PodName}}</a><br>
Node <a href="https://console.cloud.google.com/kubernetes/node/{{.GKEInfo.ClusterLocation}}/{{.GKEInfo.ClusterName}}/{{.NodeName}}?project={{.GKEInfo.ProjectId}}">{{.NodeName}}</a><br>
Cluster <a href="https://console.cloud.google.com/kubernetes/clusters/details/{{.GKEInfo.ClusterLocation}}/{{.GKEInfo.ClusterName}}?project={{.GKEInfo.ProjectId}}">{{.GKEInfo.ClusterName}}</a>
</p>
{{end}}
</div>
<div class="righthand">
View <a href="/metrics">metrics</a><br>
</div>
</div>

<h1>Parsed configuration</h1>

{{with .Config}}

<table>
<tr><th>Config filename</th><td>{{.ConfigFilename}}</td></tr>
<tr><th>Filters</th><td>{{.Filters}}</td></tr>
<tr><th>Filter sets</th><td>{{.Filtersets}}</td></tr>
<tr><th>Generic labels: location</th><td>{{.GenericLabels.Location}}</td></tr>
<tr><th>Generic labels: namespace</th><td>{{.GenericLabels.Namespace}}</td></tr>
<tr><th>Kubernetes labels: cluster name</th><td>{{.KubernetesLabels.ClusterName}}</td></tr>
<tr><th>Kubernetes labels: location</th><td>{{.KubernetesLabels.Location}}</td></tr>
<tr><th>Listen address</th><td>{{.ListenAddress}}</td></tr>
<tr><th>Log level</th><td>{{.PromlogConfig.Level}}</td></tr>
<tr><th>Log format</th><td>{{.PromlogConfig.Format}}</td></tr>
<tr><th>Metrics prefix</th><td>{{.MetricsPrefix}}</td></tr>
<tr><th>Monitoring backends</th><td>{{.MonitoringBackends}}</td></tr>
<tr><th>Project ID resource</th><td>{{.ProjectIDResource}}</td></tr>
<tr><th>Prometheus URL</th><td>{{.PrometheusURL}}</td></tr>
<tr><th>Stackdriver address</th><td>{{.StackdriverAddress}}</td></tr>
<tr><th>Store in files directory</th><td>{{.StoreInFilesDirectory}}</td></tr>
<tr><th>Use GKE resource</th><td>{{.UseGKEResource}}</td></tr>
<tr><th>Use restricted IPs</th><td>{{.UseRestrictedIPs}}</td></tr>
<tr><th>WAL directory</th><td>{{.WALDirectory}}</td></tr>
</table>

<h2>Aggregations</h2>
{{if .Aggregations}}
<table>
<tr><th>metric</th><th>matchers</th></tr>
{{range $metric, $metricConfig := .Aggregations}}
<tr><td>{{$metric}}</td><td>{{$metricConfig.Matchers}}</td></tr>
{{end}}
</table>
{{else}}
none
{{end}}

<h2>Metric renames</h2>
{{if .MetricRenames}}
<table>
<tr><th>from</th><th>to</th></tr>
{{range $from, $to := .MetricRenames}}
<tr><td>{{$from}}</td><td>{{$to}}</td></tr>
{{end}}
</table>
{{else}}
none
{{end}}

<h2>Static metadata</h2>
{{if .StaticMetadata}}
<table>
<tr><th>metric</th><th>type</th><th>unit</th></tr>
{{range .StaticMetadata}}
<tr><td>{{.Metric}}</td><td>{{.MetricType}}</td><td>{{.ValueType}}</td></tr>
{{end}}
</table>
{{else}}
none
{{end}}

{{end}}

</body>
</html>
155 changes: 26 additions & 129 deletions cmd/stackdriver-prometheus-sidecar/statusz.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,151 +11,29 @@
// 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 -src=./ -include=*.html
package main

import (
"fmt"
"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"
)

const(
statuszHtml = `
<!DOCTYPE html>
<html>
<head>
<title>Status for {{.ServerName}}</title>
<style>
body {
font-family: sans-serif;
}
h1 {
clear: both;
width: 100%;
text-align: center;
font-size: 120%;
background: #eef;
}
h2 {
font-size: 110%;
}
.lefthand {
float: left;
width: 80%;
}
.righthand {
text-align: right;
}
td, th {
background-color: rgba(0, 0, 0, 0.05);
}
th {
text-align: left;
}
</style>
</head>
<body>
<h1>Status for {{.ServerName}}</h1>
<div>
<div class="lefthand">
Started: {{.StartTime}}<br>
Up {{.Uptime}}<br>
Version: {{.VersionInfo}}<br>
Build context: {{.BuildContext}}<br>
Host details: {{.Uname}}<br>
FD limits: {{.FdLimits}}<br>
{{if (and .GKEInfo.ProjectId .GKEInfo.ClusterLocation .GKEInfo.ClusterName .PodName .NodeName .NamespaceName)}}
<p>
Pod <a href="https://console.cloud.google.com/kubernetes/pod/{{.GKEInfo.ClusterLocation}}/{{.GKEInfo.ClusterName}}/{{if .NamespaceName}}{{.NamespaceName}}{{else}}default{{end}}/{{.PodName}}?project={{.GKEInfo.ProjectId}}">{{.PodName}}</a><br>
Node <a href="https://console.cloud.google.com/kubernetes/node/{{.GKEInfo.ClusterLocation}}/{{.GKEInfo.ClusterName}}/{{.NodeName}}?project={{.GKEInfo.ProjectId}}">{{.NodeName}}</a><br>
Cluster <a href="https://console.cloud.google.com/kubernetes/clusters/details/{{.GKEInfo.ClusterLocation}}/{{.GKEInfo.ClusterName}}?project={{.GKEInfo.ProjectId}}">{{.GKEInfo.ClusterName}}</a>
</p>
{{end}}
</div>
<div class="righthand">
View <a href="/metrics">metrics</a><br>
</div>
</div>
<h1>Parsed configuration</h1>
{{with .Config}}
<table>
<tr><th>Config filename</th><td>{{.ConfigFilename}}</td></tr>
<tr><th>Filters</th><td>{{.Filters}}</td></tr>
<tr><th>Filter sets</th><td>{{.Filtersets}}</td></tr>
<tr><th>Generic labels: location</th><td>{{.GenericLabels.Location}}</td></tr>
<tr><th>Generic labels: namespace</th><td>{{.GenericLabels.Namespace}}</td></tr>
<tr><th>Kubernetes labels: cluster name</th><td>{{.KubernetesLabels.ClusterName}}</td></tr>
<tr><th>Kubernetes labels: location</th><td>{{.KubernetesLabels.Location}}</td></tr>
<tr><th>Listen address</th><td>{{.ListenAddress}}</td></tr>
<tr><th>Log level</th><td>{{.PromlogConfig.Level}}</td></tr>
<tr><th>Log format</th><td>{{.PromlogConfig.Format}}</td></tr>
<tr><th>Metrics prefix</th><td>{{.MetricsPrefix}}</td></tr>
<tr><th>Monitoring backends</th><td>{{.MonitoringBackends}}</td></tr>
<tr><th>Project ID resource</th><td>{{.ProjectIDResource}}</td></tr>
<tr><th>Prometheus URL</th><td>{{.PrometheusURL}}</td></tr>
<tr><th>Stackdriver address</th><td>{{.StackdriverAddress}}</td></tr>
<tr><th>Store in files directory</th><td>{{.StoreInFilesDirectory}}</td></tr>
<tr><th>Use GKE resource</th><td>{{.UseGKEResource}}</td></tr>
<tr><th>Use restricted IPs</th><td>{{.UseRestrictedIPs}}</td></tr>
<tr><th>WAL directory</th><td>{{.WALDirectory}}</td></tr>
</table>
<h2>Aggregations</h2>
{{if .Aggregations}}
<table>
<tr><th>metric</th><th>matchers</th></tr>
{{range $metric, $metricConfig := .Aggregations}}
<tr><td>{{$metric}}</td><td>{{$metricConfig.Matchers}}</td></tr>
{{end}}
</table>
{{else}}
none
{{end}}
<h2>Metric renames</h2>
{{if .MetricRenames}}
<table>
<tr><th>from</th><th>to</th></tr>
{{range $from, $to := .MetricRenames}}
<tr><td>{{$from}}</td><td>{{$to}}</td></tr>
{{end}}
</table>
{{else}}
none
{{end}}
<h2>Static metadata</h2>
{{if .StaticMetadata}}
<table>
<tr><th>metric</th><th>type</th><th>unit</th></tr>
{{range .StaticMetadata}}
<tr><td>{{.Metric}}</td><td>{{.MetricType}}</td><td>{{.ValueType}}</td></tr>
{{end}}
</table>
{{else}}
none
{{end}}
{{end}}
</body>
</html>
`
)
var (
serverStart = time.Now()
statuszTmpl = template.Must(template.New("statusz-tmpl.html").Parse(statuszHtml))
statuszTmpl = template.New("statusz-tmpl.html")
)

type statuszHandler struct {
Expand All @@ -164,6 +42,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 = statuszTmpl.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

0 comments on commit 32608ad

Please sign in to comment.