|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "time" |
| 6 | + "unsafe" |
| 7 | + |
| 8 | + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/githubreceiver" |
| 9 | + |
| 10 | + "go.opentelemetry.io/collector/scraper/scraperhelper" |
| 11 | +) |
| 12 | + |
| 13 | +type GithubConfig struct { |
| 14 | + InitialDelay time.Duration `alloy:"initial_delay,attr,optional"` |
| 15 | + CollectionInterval time.Duration `alloy:"collection_interval,attr,optional"` |
| 16 | + Scrapers map[string]ScraperConfig `alloy:"scraper,block"` |
| 17 | +} |
| 18 | + |
| 19 | +func (args GithubConfig) Convert() githubreceiver.Config { |
| 20 | + convertedScrapers := make(map[string]interface{}, len(args.Scrapers)) |
| 21 | + for name, scraper := range args.Scrapers { |
| 22 | + convertedScrapers[name] = scraper.Convert() |
| 23 | + } |
| 24 | + |
| 25 | + config := githubreceiver.Config{ |
| 26 | + ControllerConfig: scraperhelper.ControllerConfig{ |
| 27 | + InitialDelay: args.InitialDelay, |
| 28 | + CollectionInterval: args.CollectionInterval, |
| 29 | + }, |
| 30 | + } |
| 31 | + |
| 32 | + *(*map[string]interface{})(unsafe.Pointer(&config.Scrapers)) = convertedScrapers |
| 33 | + |
| 34 | + return config |
| 35 | +} |
| 36 | + |
| 37 | +func (args *GithubConfig) SetToDefault() { |
| 38 | + if args.InitialDelay == 0 { |
| 39 | + args.InitialDelay = 0 |
| 40 | + } |
| 41 | + if args.CollectionInterval == 0 { |
| 42 | + args.CollectionInterval = 60 * time.Second |
| 43 | + } |
| 44 | + |
| 45 | + for _, scraper := range args.Scrapers { |
| 46 | + scraper.SetToDefault() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (args GithubConfig) Validate() error { |
| 51 | + for _, scraper := range args.Scrapers { |
| 52 | + if err := scraper.Validate(); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +type ScraperConfig struct { |
| 61 | + GithubOrg string `alloy:"github_org,attr"` |
| 62 | + SearchQuery string `alloy:"search_query,attr"` |
| 63 | + Endpoint string `alloy:"endpoint,attr,optional"` |
| 64 | + Auth AuthConfig `alloy:"auth,block"` |
| 65 | + Metrics MetricsConfig `alloy:"metrics,block,optional"` |
| 66 | +} |
| 67 | + |
| 68 | +func (sc ScraperConfig) Convert() map[string]interface{} { |
| 69 | + return map[string]interface{}{ |
| 70 | + "github_org": sc.GithubOrg, |
| 71 | + "search_query": sc.SearchQuery, |
| 72 | + "endpoint": sc.Endpoint, |
| 73 | + "auth": sc.Auth.Convert(), |
| 74 | + "metrics": sc.Metrics.Convert(), |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func (sc *ScraperConfig) SetToDefault() { |
| 79 | + if sc.Metrics == (MetricsConfig{}) { |
| 80 | + sc.Metrics.SetToDefault() |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (sc *ScraperConfig) Validate() error { |
| 85 | + if sc.GithubOrg == "" { |
| 86 | + return errors.New("github_org is required") |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +type AuthConfig struct { |
| 92 | + Authenticator string `alloy:"authenticator,attr"` |
| 93 | +} |
| 94 | + |
| 95 | +func (ac AuthConfig) Convert() map[string]interface{} { |
| 96 | + return map[string]interface{}{ |
| 97 | + "authenticator": ac.Authenticator, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +type MetricConfig struct { |
| 102 | + Enabled bool `alloy:"enabled,attr"` |
| 103 | +} |
| 104 | + |
| 105 | +type MetricsConfig struct { |
| 106 | + VCSChangeCount MetricConfig `alloy:"vcs.change.count,block,optional"` |
| 107 | + VCSChangeDuration MetricConfig `alloy:"vcs.change.duration,block,optional"` |
| 108 | + VCSChangeTimeToApproval MetricConfig `alloy:"vcs.change.time_to_approval,block,optional"` |
| 109 | + VCSChangeTimeToMerge MetricConfig `alloy:"vcs.change.time_to_merge,block,optional"` |
| 110 | + VCSRefCount MetricConfig `alloy:"vcs.ref.count,block,optional"` |
| 111 | + VCSRefLinesDelta MetricConfig `alloy:"vcs.ref.lines_delta,block,optional"` |
| 112 | + VCSRefRevisionsDelta MetricConfig `alloy:"vcs.ref.revisions_delta,block,optional"` |
| 113 | + VCSRefTime MetricConfig `alloy:"vcs.ref.time,block,optional"` |
| 114 | + VCSRepositoryCount MetricConfig `alloy:"vcs.repository.count,block,optional"` |
| 115 | + VCSContributorCount MetricConfig `alloy:"vcs.contributor.count,block,optional"` |
| 116 | +} |
| 117 | + |
| 118 | +func (m *MetricsConfig) Convert() map[string]interface{} { |
| 119 | + if m == nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + return map[string]interface{}{ |
| 124 | + "vcs.change.count": m.VCSChangeCount.Convert(), |
| 125 | + "vcs.change.duration": m.VCSChangeDuration.Convert(), |
| 126 | + "vcs.change.time_to_approval": m.VCSChangeTimeToApproval.Convert(), |
| 127 | + "vcs.change.time_to_merge": m.VCSChangeTimeToMerge.Convert(), |
| 128 | + "vcs.ref.count": m.VCSRefCount.Convert(), |
| 129 | + "vcs.ref.lines_delta": m.VCSRefLinesDelta.Convert(), |
| 130 | + "vcs.ref.revisions_delta": m.VCSRefRevisionsDelta.Convert(), |
| 131 | + "vcs.ref.time": m.VCSRefTime.Convert(), |
| 132 | + "vcs.repository.count": m.VCSRepositoryCount.Convert(), |
| 133 | + "vcs.contributor.count": m.VCSContributorCount.Convert(), |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func (m *MetricConfig) Convert() map[string]interface{} { |
| 138 | + if m == nil { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + return map[string]interface{}{ |
| 143 | + "enabled": m.Enabled, |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func (mc *MetricsConfig) SetToDefault() { |
| 148 | + *mc = MetricsConfig{ |
| 149 | + VCSChangeCount: MetricConfig{Enabled: true}, |
| 150 | + VCSChangeDuration: MetricConfig{Enabled: true}, |
| 151 | + VCSChangeTimeToApproval: MetricConfig{Enabled: true}, |
| 152 | + VCSChangeTimeToMerge: MetricConfig{Enabled: true}, |
| 153 | + VCSRefCount: MetricConfig{Enabled: true}, |
| 154 | + VCSRefLinesDelta: MetricConfig{Enabled: true}, |
| 155 | + VCSRefRevisionsDelta: MetricConfig{Enabled: true}, |
| 156 | + VCSRefTime: MetricConfig{Enabled: true}, |
| 157 | + VCSRepositoryCount: MetricConfig{Enabled: true}, |
| 158 | + VCSContributorCount: MetricConfig{Enabled: false}, |
| 159 | + } |
| 160 | +} |
0 commit comments