scorecard/cron/internal/monitoring/exporter.go
Spencer Schrock f979097a1f
🌱 cron: generalize and expose worker (6/n) (#2317)
* WIP

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Appease linter.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Update Makefile for worker

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Extract already completed request sanity check.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Add worker test.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* remove logger from worker interface

Signed-off-by: Spencer Schrock <sschrock@google.com>

* move cron data and worker out of cron/internal

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Move config out of internal.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Document worker interface.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Fix typo which prevented metadata from going to cron job.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Address feedback.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Revert "Fix typo which prevented metadata from going to cron job."

This reverts commit 876acb062e.

Will send separate PR.

Signed-off-by: Spencer Schrock <sschrock@google.com>

* Fix linter.

Signed-off-by: Spencer Schrock <sschrock@google.com>

Signed-off-by: Spencer Schrock <sschrock@google.com>
2022-10-19 21:01:42 +00:00

86 lines
2.7 KiB
Go

// Copyright 2021 Security Scorecard 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 monitoring defines exporters to be used by opencensus package in the cron job.
package monitoring
import (
"errors"
"fmt"
"time"
"contrib.go.opencensus.io/exporter/stackdriver"
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource/gcp"
"go.opencensus.io/stats/view"
"github.com/ossf/scorecard/v4/cron/config"
)
var errorUndefinedExporter = errors.New("unsupported exporterType")
type exporterType string
const (
stackdriverMetricPrefix = "scorecard-cron"
stackdriverTimeSeriesQuota = 200
stackdriverTimeoutMinutes = 10
stackDriver exporterType = "stackdriver"
printer exporterType = "printer"
)
// Exporter interface is a custom wrapper to represent an opencensus exporter.
type Exporter interface {
ExportView(viewData *view.Data)
StartMetricsExporter() error
StopMetricsExporter()
Flush()
}
// GetExporter defines a factory for returning opencensus Exporter.
// Ensure config.ReadConfig() is called at some point before this function.
func GetExporter() (Exporter, error) {
exporter, err := config.GetMetricExporter()
if err != nil {
return nil, fmt.Errorf("error during GetMetricExporter: %w", err)
}
switch exporterType(exporter) {
case stackDriver:
return newStackDriverExporter()
case printer:
return new(printerExporter), nil
default:
return nil, fmt.Errorf("%w: %s", errorUndefinedExporter, exporter)
}
}
func newStackDriverExporter() (*stackdriver.Exporter, error) {
projectID, err := config.GetProjectID()
if err != nil {
return nil, fmt.Errorf("error getting ProjectID: %w", err)
}
exporter, err := stackdriver.NewExporter(stackdriver.Options{
ProjectID: projectID,
MetricPrefix: stackdriverMetricPrefix,
MonitoredResource: gcp.Autodetect(),
Timeout: stackdriverTimeoutMinutes * time.Minute,
// Stackdriver specific quotas based on https://cloud.google.com/monitoring/quotas
// `Time series included in a request`
BundleCountThreshold: stackdriverTimeSeriesQuota,
})
if err != nil {
return nil, fmt.Errorf("error during stackdriver.NewExporter: %w", err)
}
return exporter, nil
}