in cmd/core_plugin/diagnostics/diagnostics_windows.go [133:209]
func (mod *diagnosticsModule) handleDiagnosticsRequest(ctx context.Context, config *cfg.Sections, desc *metadata.Descriptor) error {
defer func() { mod.prevMetadata = desc }()
// If there is an existing job running, reject the request.
if mod.isDiagnosticsRunning.Load() {
galog.Infof("Diagnostics: reject the request, as an existing process is collecting logs from the system")
return nil
}
// Ignore/return if diagnostics configuration is disabled or the
// metadata flags haven't changed.
if !mod.diagnosticsEnabled(desc, config) || !mod.metadataChanged(desc) {
return nil
}
galog.Infof("Diagnostics: logs export requested.")
// Fetch from the registry the list of the existing/seen request entries.
regEntries, err := reg.ReadMultiString(diagnosticsRegKey, diagnosticsRegKey)
if err != nil && !errors.Is(err, registry.ErrNotExist) {
return fmt.Errorf("failed to read diagnostics registry key: %v", err)
}
// Check if we've dealt with this entry already.
metadataNewEntry := desc.Instance().Attributes().Diagnostics()
if slices.Contains(regEntries, metadataNewEntry) {
galog.Debugf("Diagnostics: request already seen %q, ignoring.", metadataNewEntry)
return nil
}
// Unmarshall the new entry to extract the request details.
var entry diagnosticsEntry
if err := json.Unmarshal([]byte(metadataNewEntry), &entry); err != nil {
return fmt.Errorf("failed to unmarshal diagnostics entry: %w", err)
}
expired, err := ssh.CheckExpired(entry.ExpireOn)
if err != nil {
return fmt.Errorf("failed to check diagnostics request expiration(%v): %w", entry, err)
}
// Has the request already expired or is it malformed (no signed URL)?
if entry.SignedURL == "" || expired {
return fmt.Errorf("diagnostics: request %v is malformed or expired, ignoring", metadataNewEntry)
}
cmd := []string{diagnosticsCmd, "-signedUrl", entry.SignedURL}
if entry.Trace {
cmd = append(cmd, "-trace")
}
// Set flag job is running only when it is about to start.
mod.isDiagnosticsRunning.Store(true)
go func() {
galog.Infof("Diagnostics: collecting logs from the system.")
// Job is done, unblock the upcoming requests.
defer func() { mod.isDiagnosticsRunning.Swap(false) }()
// Actually run the diagnostics command.
opts := run.Options{Name: cmd[0], Args: cmd[1:], OutputType: run.OutputCombined}
res, err := run.WithContext(ctx, opts)
if err != nil {
galog.Errorf("Error collecting logs: %v", err)
return
}
galog.Infof(res.Output)
}()
regEntries = append(regEntries, metadataNewEntry)
if err := reg.WriteMultiString(reg.GCEKeyBase, diagnosticsRegKey, regEntries); err != nil {
return fmt.Errorf("failed to write diagnostics registry key: %v", err)
}
return nil
}