upload

Imports

Imports #

"crypto/rand"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"os"
"path/filepath"
"strings"
"time"
"golang.org/x/telemetry/internal/config"
"golang.org/x/telemetry/internal/counter"
"golang.org/x/telemetry/internal/telemetry"
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"runtime/debug"
"strings"
"time"
"golang.org/x/telemetry/internal/configstore"
"golang.org/x/telemetry/internal/telemetry"
"bytes"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"golang.org/x/telemetry/internal/telemetry"
"fmt"
"os"
"sync"
"time"
"golang.org/x/telemetry/internal/counter"
"golang.org/x/telemetry/internal/telemetry"
"os"
"path/filepath"
"strings"

Constants & Variables

dateFormat var #

var dateFormat = telemetry.DateOnly

dateRE var #

var dateRE = *ast.CallExpr

distantPast var #

var distantPast = *ast.BinaryExpr

Structs

RunConfig struct #

RunConfig configures non-default behavior of a call to Run. All fields are optional, for testing or observability.

type RunConfig struct {
TelemetryDir string
UploadURL string
LogWriter io.Writer
Env []string
StartTime time.Time
}

parsedCache struct #

avoid parsing count files multiple times

type parsedCache struct {
mu sync.Mutex
m map[string]*counter.File
}

uploader struct #

uploader encapsulates a single upload operation, carrying parameters and shared state.

type uploader struct {
config *telemetry.UploadConfig
configVersion string
dir telemetry.Dir
uploadServerURL string
startTime time.Time
cache parsedCache
logFile *os.File
logger *log.Logger
}

work struct #

files to handle

type work struct {
countfiles []string
readyfiles []string
uploaded map[string]bool
}

Functions

Close method #

Close cleans up any resources associated with the uploader.

func (u *uploader) Close() error

Run function #

Run generates and uploads reports, as allowed by the mode file.

func Run(config RunConfig) error

Run method #

Run generates and uploads reports

func (u *uploader) Run() error

computeRandom function #

computeRandom returns a cryptographic random float64 in the range [0, 1], with 52 bits of precision.

func computeRandom() float64

counterDateSpan method #

counterDateSpan parses the counter file named fname and returns the (begin, end) span recorded in its metadata, or an error if this data could not be extracted.

func (u *uploader) counterDateSpan(fname string) (begin time.Time, end time.Time, _ error)

createReport method #

createReport creates local and upload report files by combining all the count files for the expiryDate, and returns the upload report file's path. It may delete the count files once local and upload report files are successfully created.

func (u *uploader) createReport(start time.Time, expiryDate string, countFiles []string, lastWeek string) (string, error)

debugLogFile function #

debugLogFile arranges to write a log file in the given debug directory, if it exists.

func debugLogFile(debugDir string) (*os.File, error)

deleteFiles method #

func (u *uploader) deleteFiles(files []string)

exclusiveWrite function #

exclusiveWrite attempts to create filename exclusively, and if successful, writes content to the resulting file handle. It returns a boolean indicating whether the exclusive handle was acquired, and an error indicating whether the operation succeeded. If the file already exists, exclusiveWrite returns (false, nil).

func exclusiveWrite(filename string, content []byte) (_ bool, rerr error)

findProgReport function #

return an existing ProgremReport, or create anew

func findProgReport(meta map[string]string, report *telemetry.Report) *telemetry.ProgramReport

findWork method #

find all the files that look like counter files or reports that need to be uploaded. (There may be unexpected leftover files and uploading is supposed to be idempotent.)

func (u *uploader) findWork() work

latestReport function #

latestReport returns the YYYY-MM-DD of the last report uploaded or the empty string if there are no reports.

func latestReport(uploaded map[string]bool) string

newUploader function #

newUploader creates a new uploader to use for running the upload for the given config. Uploaders should only be used for one call to [uploader.Run].

func newUploader(rcfg RunConfig) (*uploader, error)

notNeeded function #

notNeeded returns true if the report for date has already been created

func notNeeded(date string, todo work) bool

parseCountFile method #

func (u *uploader) parseCountFile(fname string) (*counter.File, error)

reports method #

reports generates reports from inactive count files

func (u *uploader) reports(todo *work) ([]string, error)

tooOld method #

reports that are too old (21 days) are not uploaded

func (u *uploader) tooOld(date string, uploadStartTime time.Time) bool

uploadReport method #

func (u *uploader) uploadReport(fname string)

uploadReportContents method #

try to upload the report, 'true' if successful

func (u *uploader) uploadReportContents(fname string, buf []byte) bool

uploadReportDate method #

uploadReportDate returns the date component of the upload file name, or "" if the date was unmatched.

func (u *uploader) uploadReportDate(fname string) time.Time

Generated with Arrow