forked from sigstore/fulcio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap the server with the Prometheus so we get metrics + add an e2e te…
…st for it. I wrapped it in a job, since otherwise it will be trickier to get access to the metrics from outside the kind cluster. Fix the dangling pointer from sigstore#262 and fix a tiny typo, though I kind of do like exposing a singingCert that might come handy in karaoke? Signed-off-by: Ville Aikas <vaikas@chainguard.dev>
- Loading branch information
Showing
7 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// 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. | ||
// | ||
|
||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
"github.com/prometheus/common/expfmt" | ||
) | ||
|
||
func fatal(err error) { | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
func parseMF(url string) (map[string]*dto.MetricFamily, error) { | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var parser expfmt.TextParser | ||
return parser.TextToMetricFamilies(resp.Body) | ||
} | ||
|
||
func main() { | ||
f := flag.String("url", "http://fulcio-server.fulcio-dev.svc:2112/metrics", "set url to fetch metrics from") | ||
flag.Parse() | ||
|
||
mf, err := parseMF(*f) | ||
fatal(err) | ||
|
||
// Just grab the api_latency metric, make sure it's a histogram | ||
// and just make sure there is at least one 200, and no errors there. | ||
latency, ok := mf["fulcio_api_latency"] | ||
if !ok || latency == nil { | ||
log.Fatal("Did not get fulcio_api_latency metric") | ||
} | ||
if err := checkLatency(latency); err != nil { | ||
log.Fatalf("fulcio_api_latency metric failed: %s", err) | ||
} | ||
|
||
// Then make sure the cert counter went up. | ||
certCount, ok := mf["fulcio_new_certs"] | ||
if !ok || certCount == nil { | ||
log.Fatal("Did not get fulcio_new_certs metric") | ||
} | ||
if err := checkCertCount(certCount); err != nil { | ||
log.Fatalf("fulcio_new_certs metric failed: %s", err) | ||
} | ||
} | ||
|
||
// Make sure latency is a Histogram, and it has a POST with a 200. | ||
func checkLatency(latency *dto.MetricFamily) error { | ||
if *latency.Type != *dto.MetricType_HISTOGRAM.Enum() { | ||
return fmt.Errorf("Wrong type, wanted %+v, got: %+v", dto.MetricType_HISTOGRAM.Enum(), latency.Type) | ||
} | ||
if len(latency.Metric) != 1 { | ||
return fmt.Errorf("Got multiple entries, or none for metric, wanted one, got: %+v", latency.Metric) | ||
} | ||
// Make sure there's a 'post' and it's a 200. | ||
var code string | ||
var method string | ||
for _, value := range latency.Metric[0].Label { | ||
if *value.Name == "code" { | ||
code = *value.Value | ||
} | ||
if *value.Name == "method" { | ||
method = *value.Value | ||
} | ||
} | ||
if code != "200" { | ||
return fmt.Errorf("unexpected code, wanted 200, got %s", code) | ||
} | ||
if method != "post" { | ||
return fmt.Errorf("unexpected method, wanted post, got %s", method) | ||
} | ||
|
||
if *latency.Metric[0].Histogram.SampleCount != 1 { | ||
return fmt.Errorf("Unexpected samplecount, wanted 1, got %d", *latency.Metric[0].Histogram.SampleCount) | ||
} | ||
return nil | ||
} | ||
|
||
func checkCertCount(certCount *dto.MetricFamily) error { | ||
if *certCount.Type != *dto.MetricType_COUNTER.Enum() { | ||
return fmt.Errorf("Wrong type, wanted %+v, got: %+v", dto.MetricType_COUNTER.Enum(), certCount.Type) | ||
} | ||
if len(certCount.Metric) != 1 { | ||
return fmt.Errorf("Got multiple entries, or none for metric, wanted one, got: %+v", certCount.Metric) | ||
} | ||
if *certCount.Metric[0].Counter.Value < 1 { | ||
return fmt.Errorf("Got incorrect cert count, wanted one, got: %f", *certCount.Metric[0].Counter.Value) | ||
} | ||
return nil | ||
} |