diff options
author | Ben Burwell <ben@benburwell.com> | 2020-07-13 23:33:32 -0400 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2020-07-13 23:37:24 -0400 |
commit | e8b79478b1807b8f8828c51e989d83c5ae7d6d89 (patch) | |
tree | 5017c98ebc221bf8582def77e4dcdad005c16a71 /collector/collector.go |
Initial commit
Diffstat (limited to 'collector/collector.go')
-rw-r--r-- | collector/collector.go | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/collector/collector.go b/collector/collector.go new file mode 100644 index 0000000..2706fcf --- /dev/null +++ b/collector/collector.go @@ -0,0 +1,100 @@ +package collector + +import ( + "log" + "net/http" + "strconv" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" + + "bnbl.io/csp_exporter/csp" +) + +type Collector interface { + Collect(app string, report *csp.Report) + Handler() http.Handler +} + +type collector struct { + reportCounter *prometheus.CounterVec +} + +const ( + labelApp = "app" + labelBlockedURI = "blocked_uri" + labelDisposition = "disposition" + labelDocumentURI = "document_uri" + labelEffectiveDirective = "effective_directive" + labelOriginalPolicy = "original_policy" + labelReferrer = "referrer" + labelScriptSample = "script_sample" + labelStatusCode = "status_code" + labelViolatedDirective = "violated_directive" + labelSourceFile = "source_file" + labelLineNumber = "line_number" + labelColumnNumber = "column_number" +) + +func NewCollector() (Collector, error) { + c := &collector{ + reportCounter: prometheus.NewCounterVec(prometheus.CounterOpts{ + Namespace: "http", + Subsystem: "csp", + Name: "violations_count", + Help: "Count of CSP violation reports.", + }, []string{ + labelApp, + labelBlockedURI, + labelDisposition, + labelDocumentURI, + labelEffectiveDirective, + labelOriginalPolicy, + labelReferrer, + labelScriptSample, + labelStatusCode, + labelViolatedDirective, + labelSourceFile, + labelLineNumber, + labelColumnNumber, + }), + } + if err := prometheus.Register(c.reportCounter); err != nil { + log.Fatalf("could not register report counter with prometheus: %v", err) + } + return c, nil +} + +func (c *collector) Collect(app string, report *csp.Report) { + labels := prometheus.Labels{ + labelApp: app, + labelBlockedURI: report.BlockedURI, + labelDisposition: report.Disposition, + labelDocumentURI: report.DocumentURI, + labelEffectiveDirective: report.EffectiveDirective, + labelOriginalPolicy: report.OriginalPolicy, + labelReferrer: report.Referrer, + labelScriptSample: report.ScriptSample, + labelViolatedDirective: report.ViolatedDirective, + labelSourceFile: report.SourceFile, + labelStatusCode: "", + labelLineNumber: "", + labelColumnNumber: "", + } + + if report.StatusCode > 0 { + labels[labelStatusCode] = strconv.Itoa(report.StatusCode) + } + if report.LineNumber > 0 { + labels[labelLineNumber] = strconv.Itoa(report.LineNumber) + } + if report.ColumnNumber > 0 { + labels[labelColumnNumber] = strconv.Itoa(report.ColumnNumber) + } + + c.reportCounter.With(labels).Inc() +} + +func (c *collector) Handler() http.Handler { + return promhttp.Handler() +} |