|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2026-present Datadog, Inc. |
| 5 | + |
| 6 | +//go:build windows |
| 7 | + |
| 8 | +package modules |
| 9 | + |
| 10 | +import ( |
| 11 | + "time" |
| 12 | + |
| 13 | + "go.uber.org/atomic" |
| 14 | + |
| 15 | + "github.com/DataDog/datadog-agent/comp/core/sysprobeconfig" |
| 16 | + "github.com/DataDog/datadog-agent/comp/core/telemetry" |
| 17 | + "github.com/DataDog/datadog-agent/pkg/system-probe/api/module" |
| 18 | + "github.com/DataDog/datadog-agent/pkg/system-probe/config" |
| 19 | + sysconfigtypes "github.com/DataDog/datadog-agent/pkg/system-probe/config/types" |
| 20 | + "github.com/DataDog/datadog-agent/pkg/util/log" |
| 21 | + "github.com/DataDog/datadog-agent/pkg/windowsdriver/ddinjector" |
| 22 | + "github.com/prometheus/client_golang/prometheus" |
| 23 | +) |
| 24 | + |
| 25 | +func init() { registerModule(Injector) } |
| 26 | + |
| 27 | +var _ module.Module = &injectorModule{} |
| 28 | + |
| 29 | +// Injector Factory |
| 30 | +var Injector = &module.Factory{ |
| 31 | + Name: config.InjectorModule, |
| 32 | + ConfigNamespaces: []string{}, |
| 33 | + Fn: func(_ *sysconfigtypes.Config, deps module.FactoryDependencies) (module.Module, error) { |
| 34 | + log.Infof("Creating Windows Injector module") |
| 35 | + |
| 36 | + m := &injectorModule{ |
| 37 | + telemetry: deps.Telemetry, |
| 38 | + sysProbeConfig: deps.SysprobeConfig, |
| 39 | + } |
| 40 | + |
| 41 | + m.initializeMetrics() |
| 42 | + |
| 43 | + return m, nil |
| 44 | + }, |
| 45 | +} |
| 46 | + |
| 47 | +type injectorModule struct { |
| 48 | + lastCheck atomic.Int64 |
| 49 | + telemetry telemetry.Component |
| 50 | + counters ddinjector.InjectorCounters |
| 51 | + sysProbeConfig sysprobeconfig.Component |
| 52 | +} |
| 53 | + |
| 54 | +// Register registers the endpoint for this module |
| 55 | +func (m *injectorModule) Register(httpMux *module.Router) error { |
| 56 | + if m.sysProbeConfig.GetBool("injector.enable_telemetry") { |
| 57 | + m.telemetry.RegisterCollector(m) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// Close cleans up module resources |
| 64 | +func (m *injectorModule) Close() { |
| 65 | + if m.sysProbeConfig.GetBool("injector.enable_telemetry") { |
| 66 | + m.telemetry.UnregisterCollector(m) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// GetStats prints the injector stats as part of /debug/stats. |
| 71 | +func (m *injectorModule) GetStats() map[string]interface{} { |
| 72 | + // Sanity check in case the metrics have not been initialized. |
| 73 | + if m.counters.ProcessesAddedToInjectionTracker == nil { |
| 74 | + return map[string]interface{}{ |
| 75 | + "last_check_timestamp": m.lastCheck.Load(), |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return map[string]interface{}{ |
| 80 | + "last_check_timestamp": m.lastCheck.Load(), |
| 81 | + "processes_added_to_injection_tracker": m.counters.ProcessesAddedToInjectionTracker.Get(), |
| 82 | + "processes_removed_from_injection_tracker": m.counters.ProcessesRemovedFromInjectionTracker.Get(), |
| 83 | + "processes_skipped_subsystem": m.counters.ProcessesSkippedSubsystem.Get(), |
| 84 | + "processes_skipped_container": m.counters.ProcessesSkippedContainer.Get(), |
| 85 | + "processes_skipped_protected": m.counters.ProcessesSkippedProtected.Get(), |
| 86 | + "processes_skipped_system": m.counters.ProcessesSkippedSystem.Get(), |
| 87 | + "processes_skipped_excluded": m.counters.ProcessesSkippedExcluded.Get(), |
| 88 | + "injection_attempts": m.counters.InjectionAttempts.Get(), |
| 89 | + "injection_attempt_failures": m.counters.InjectionAttemptFailures.Get(), |
| 90 | + "injection_max_time_us": m.counters.InjectionMaxTimeUs.Get(), |
| 91 | + "injection_successes": m.counters.InjectionSuccesses.Get(), |
| 92 | + "injection_failures": m.counters.InjectionFailures.Get(), |
| 93 | + "pe_caching_failures": m.counters.PeCachingFailures.Get(), |
| 94 | + "import_directory_restoration_failures": m.counters.ImportDirectoryRestorationFailures.Get(), |
| 95 | + "pe_memory_allocation_failures": m.counters.PeMemoryAllocationFailures.Get(), |
| 96 | + "pe_injection_context_allocated": m.counters.PeInjectionContextAllocated.Get(), |
| 97 | + "pe_injection_context_cleanedup": m.counters.PeInjectionContextCleanedup.Get(), |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +//////////////////////////////////////////////////// |
| 102 | +// RAR/prometheus/telemetry related implementations |
| 103 | + |
| 104 | +func (m *injectorModule) initializeMetrics() { |
| 105 | + const subsystem = "injector" |
| 106 | + |
| 107 | + m.counters.ProcessesAddedToInjectionTracker = m.telemetry.NewSimpleGauge( |
| 108 | + subsystem, "processes_added_to_injection_tracker", |
| 109 | + "Number of processes added to injection tracker") |
| 110 | + |
| 111 | + m.counters.ProcessesRemovedFromInjectionTracker = m.telemetry.NewSimpleGauge( |
| 112 | + subsystem, "processes_removed_from_injection_tracker", |
| 113 | + "Number of processes removed from injection tracker") |
| 114 | + |
| 115 | + m.counters.ProcessesSkippedSubsystem = m.telemetry.NewSimpleGauge( |
| 116 | + subsystem, "processes_skipped_subsystem", |
| 117 | + "Number of skipped subsystem processes") |
| 118 | + |
| 119 | + m.counters.ProcessesSkippedContainer = m.telemetry.NewSimpleGauge( |
| 120 | + subsystem, "processes_skipped_container", |
| 121 | + "Number of skipped container processes") |
| 122 | + |
| 123 | + m.counters.ProcessesSkippedProtected = m.telemetry.NewSimpleGauge( |
| 124 | + subsystem, "processes_skipped_protected", |
| 125 | + "Number of skipped protected processes") |
| 126 | + |
| 127 | + m.counters.ProcessesSkippedSystem = m.telemetry.NewSimpleGauge( |
| 128 | + subsystem, "processes_skipped_system", |
| 129 | + "Number of skipped system processes") |
| 130 | + |
| 131 | + m.counters.ProcessesSkippedExcluded = m.telemetry.NewSimpleGauge( |
| 132 | + subsystem, "processes_skipped_excluded", |
| 133 | + "Number of skipped processes due to exclusion") |
| 134 | + |
| 135 | + m.counters.InjectionAttempts = m.telemetry.NewSimpleGauge( |
| 136 | + subsystem, "injection_attempts", |
| 137 | + "Number of injection attempts") |
| 138 | + |
| 139 | + m.counters.InjectionAttemptFailures = m.telemetry.NewSimpleGauge( |
| 140 | + subsystem, "injection_attempt_failures", |
| 141 | + "Number of injection attempt failures") |
| 142 | + |
| 143 | + m.counters.InjectionMaxTimeUs = m.telemetry.NewSimpleGauge( |
| 144 | + subsystem, "injection_max_time_us", |
| 145 | + "Maximum injection time in microseconds") |
| 146 | + |
| 147 | + m.counters.InjectionSuccesses = m.telemetry.NewSimpleGauge( |
| 148 | + subsystem, "injection_successes", |
| 149 | + "Number of successful injections") |
| 150 | + |
| 151 | + m.counters.InjectionFailures = m.telemetry.NewSimpleGauge( |
| 152 | + subsystem, "injection_failures", |
| 153 | + "Number of failed injections") |
| 154 | + |
| 155 | + m.counters.PeCachingFailures = m.telemetry.NewSimpleGauge( |
| 156 | + subsystem, "pe_caching_failures", |
| 157 | + "Number of PE caching failures") |
| 158 | + |
| 159 | + m.counters.ImportDirectoryRestorationFailures = m.telemetry.NewSimpleGauge( |
| 160 | + subsystem, "import_directory_restoration_failures", |
| 161 | + "Number of import directory restoration failures") |
| 162 | + |
| 163 | + m.counters.PeMemoryAllocationFailures = m.telemetry.NewSimpleGauge( |
| 164 | + subsystem, "pe_memory_allocation_failures", |
| 165 | + "Number of PE memory allocation failures") |
| 166 | + |
| 167 | + m.counters.PeInjectionContextAllocated = m.telemetry.NewSimpleGauge( |
| 168 | + subsystem, "pe_injection_context_allocated", |
| 169 | + "Number of PE injection contexts allocated") |
| 170 | + |
| 171 | + m.counters.PeInjectionContextCleanedup = m.telemetry.NewSimpleGauge( |
| 172 | + subsystem, "pe_injection_context_cleanedup", |
| 173 | + "Number of PE injection contexts cleaned up") |
| 174 | +} |
| 175 | + |
| 176 | +// Describe implements prometheus.Collector - no-op for dynamic metrics |
| 177 | +func (m *injectorModule) Describe(ch chan<- *prometheus.Desc) { |
| 178 | +} |
| 179 | + |
| 180 | +// Collect implements prometheus.Collector. Fetches stats from the injector. |
| 181 | +func (m *injectorModule) Collect(ch chan<- prometheus.Metric) { |
| 182 | + if m.telemetry == nil { |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + // Query the driver for current counters |
| 187 | + injector, err := ddinjector.NewInjector() |
| 188 | + if err != nil { |
| 189 | + log.Debugf("unable to open Windows Injector: %v", err) |
| 190 | + return |
| 191 | + } |
| 192 | + defer injector.Close() |
| 193 | + |
| 194 | + m.lastCheck.Store(time.Now().Unix()) |
| 195 | + |
| 196 | + err = injector.GetCounters(&m.counters) |
| 197 | + if err != nil { |
| 198 | + log.Debugf("error getting Injector counters: %v", err) |
| 199 | + return |
| 200 | + } |
| 201 | +} |
0 commit comments