Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cloud logging #254

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package api
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"

Expand All @@ -17,6 +17,7 @@ type API struct {
hstspreload hstspreloadWrapper
preloadlist preloadlistWrapper
cache *cache
logger *log.Logger
}

const (
Expand All @@ -25,12 +26,13 @@ const (

// New creates a new API struct with the given database and the proper
// unexported fields.
func New(db database.Database) API {
func New(db database.Database, logger *log.Logger) API {
return API{
database: db,
hstspreload: actualHstspreload{},
preloadlist: actualPreloadlist{},
cache: cacheWithDuration(defaultCacheDuration),
logger: logger,
}
}

Expand All @@ -54,7 +56,7 @@ func (api API) CheckConnection() error {
_, err := api.database.StateForDomain("garron.net")
if err != nil {
if strings.Contains(err.Error(), "missing project/dataset id") {
fmt.Fprintf(os.Stderr, "Try running: make serve\n")
api.logger.Print("Try running: make serve")
}
return err
}
Expand Down
2 changes: 2 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"bytes"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -60,6 +61,7 @@ func mockAPI(cacheDuration time.Duration) (api API, mc *database.MockController,
hstspreload: h,
preloadlist: c,
cache: cacheWithDuration(cacheDuration),
logger: log.Default(),
}
return api, mc, h, c
}
Expand Down
19 changes: 9 additions & 10 deletions api/domain_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"fmt"
"log"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -429,23 +428,23 @@ func (api API) RemoveIneligibleDomains(w http.ResponseWriter, r *http.Request) {
// ineligible domain database
var deleteEligibleDomains []string

log.Print("Fetching domains...")
api.logger.Print("Fetching domains...")
// Get all domains
domains, err := api.database.AllDomainStates()
if err != nil {
msg := fmt.Sprintf("Internal error: could not retrieve domains. (%s)\n", err)
http.Error(w, msg, http.StatusInternalServerError)
return
}
log.Print("Filtering domains...")
api.logger.Print("Filtering domains...")

// Filter Domains
for _, d := range domains {
if d.Policy == preloadlist.Bulk18Weeks || d.Policy == preloadlist.Bulk1Year {
policyStates[d.Name] = d
}
}
log.Print("Getting ineligible domain states...")
api.logger.Print("Getting ineligible domain states...")

// call GetIneligibleDomainStates, add to map
states := make(map[string]database.IneligibleDomainState)
Expand All @@ -465,7 +464,7 @@ func (api API) RemoveIneligibleDomains(w http.ResponseWriter, r *http.Request) {
}
}

log.Printf("Starting to scan %d domains\n", len(policyStates))
api.logger.Printf("Starting to scan %d domains\n", len(policyStates))
statesAndIssues := make(chan DomainStateWithIssues)
domainStates := make(chan database.DomainState)
var wg sync.WaitGroup
Expand All @@ -482,20 +481,20 @@ func (api API) RemoveIneligibleDomains(w http.ResponseWriter, r *http.Request) {
}
}()
}
log.Printf("Started %d workers\n", numWorkers)
api.logger.Printf("Started %d workers\n", numWorkers)
go func() {
i := 0
for _, d := range policyStates {
i++
wg.Add(1)
domainStates <- d
if i % 1000 == 0 {
log.Printf("Sent %d domains to workers\n", i)
api.logger.Printf("Sent %d domains to workers\n", i)
}
}

wg.Wait()
log.Println("... all goroutines done, closing channels")
api.logger.Println("... all goroutines done, closing channels")
close(domainStates)
close(statesAndIssues)
}()
Expand Down Expand Up @@ -535,11 +534,11 @@ func (api API) RemoveIneligibleDomains(w http.ResponseWriter, r *http.Request) {
return
}

log.Printf("About to set %d domains as potentially ineligible\n", len(ineligibleDomains))
api.logger.Printf("About to set %d domains as potentially ineligible\n", len(ineligibleDomains))

// Add ineligible domains to the database
err = api.database.SetIneligibleDomainStates(ineligibleDomains, func(format string, args ...interface{}) {
log.Printf(format, args...)
api.logger.Printf(format, args...)
})
if err != nil {
msg := fmt.Sprintf("Internal error: could not set domains. (%s)\n", err)
Expand Down
5 changes: 2 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

const (
localProjectID = "hstspreload-local"
prodProjectID = "hstspreload"

batchSize = 450
timeout = 90 * time.Second
Expand Down Expand Up @@ -50,8 +49,8 @@ func TempLocalDatabase() (db DatastoreBacked, shutdown func() error, err error)

// ProdDatabase gives a Database that will call out to
// the real production instance of Google Cloud Datastore
func ProdDatabase() (db DatastoreBacked) {
return DatastoreBacked{gcd.NewProdBackend(), prodProjectID}
func ProdDatabase(projectID string) DatastoreBacked {
return DatastoreBacked{gcd.NewProdBackend(), projectID}
}

var blackholeLogf = func(format string, args ...interface{}) {}
Expand Down
4 changes: 2 additions & 2 deletions database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"fmt"
"log"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -29,8 +30,7 @@ func ExampleTempLocalDatabase() {
func TestMain(m *testing.M) {
localDatabase, shutdown, err := TempLocalDatabase()
if err != nil {
fmt.Fprintf(os.Stderr, "could not initialize local backend: %s", err)
os.Exit(1)
log.Fatalf("could not initialize local backend: %s", err)
}

testDB = localDatabase
Expand Down
14 changes: 4 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ module github.com/chromium/hstspreload.org
go 1.15

require (
cloud.google.com/go/datastore v1.12.1
cloud.google.com/go/datastore v1.15.0
github.com/chromium/hstspreload v0.0.0-20230717215815-2404b4b967f9
golang.org/x/net v0.23.0
google.golang.org/api v0.132.0
google.golang.org/grpc v1.56.3
google.golang.org/api v0.149.0
google.golang.org/grpc v1.59.0
)

require (
cloud.google.com/go v0.110.6 // indirect
cloud.google.com/go/compute v1.22.0 // indirect
google.golang.org/genproto v0.0.0-20230720185612-659f7aaaa771 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230720185612-659f7aaaa771 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230720185612-659f7aaaa771 // indirect
)
require cloud.google.com/go/logging v1.9.0
Loading
Loading