/
factory.go
135 lines (112 loc) · 4.06 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package gitproviderreceiver // import "github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver"
import (
"context"
"errors"
"fmt"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal/metadata"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal/scraper/githubscraper"
"github.com/liatrio/liatrio-otel-collector/pkg/receiver/gitproviderreceiver/internal/scraper/gitlabscraper"
)
// This file implements a factory for the git provider receiver
//const (
// defaultInterval = 30 * time.Second
// defaultTimeout = 15 * time.Second
//)
var (
scraperFactories = map[string]internal.ScraperFactory{
githubscraper.TypeStr: &githubscraper.Factory{},
gitlabscraper.TypeStr: &gitlabscraper.Factory{},
}
errConfigNotValid = errors.New("configuration is not valid for the git provider receiver")
)
// NewFactory creates a factory for the git provider receiver
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability),
)
}
// Gets a factory for defined scraper.
func getScraperFactory(key string) (internal.ScraperFactory, bool) {
if factory, ok := scraperFactories[key]; ok {
return factory, true
}
return nil, false
}
// Create the default config based on the const(s) defined above.
func createDefaultConfig() component.Config {
return &Config{
ScraperControllerSettings: scraperhelper.NewDefaultScraperControllerSettings(metadata.Type),
// TODO: metrics builder configuration may need to be in each sub scraper,
// TODO: for right now setting here because the metrics in this receiver will apply to all
// TODO: scrapers defined as a common set of gitprovider
// TODO: aqp completely remove these comments if the metrics build config
// needs to be defined in each scraper
// MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
}
// Create the metrics receiver according to the OTEL conventions taking in the
// context, receiver params, configuration from the component, and consumer (process or exporter)
func createMetricsReceiver(
ctx context.Context,
params receiver.CreateSettings,
cfg component.Config,
consumer consumer.Metrics,
) (receiver.Metrics, error) {
// check that the configuration is valid
conf, ok := cfg.(*Config)
if !ok {
return nil, errConfigNotValid
}
addScraperOpts, err := createAddScraperOpts(ctx, params, conf, scraperFactories)
if err != nil {
return nil, err
}
return scraperhelper.NewScraperControllerReceiver(
&conf.ScraperControllerSettings,
params,
consumer,
addScraperOpts...,
)
}
func createAddScraperOpts(
ctx context.Context,
params receiver.CreateSettings,
cfg *Config,
factories map[string]internal.ScraperFactory,
) ([]scraperhelper.ScraperControllerOption, error) {
scraperControllerOptions := make([]scraperhelper.ScraperControllerOption, 0, len(cfg.Scrapers))
for key, cfg := range cfg.Scrapers {
gitProviderScraper, err := createGitProviderScraper(ctx, params, key, cfg, factories)
if err != nil {
return nil, fmt.Errorf("failed to create scraper %q: %w", key, err)
}
scraperControllerOptions = append(scraperControllerOptions, scraperhelper.AddScraper(gitProviderScraper))
}
return scraperControllerOptions, nil
}
func createGitProviderScraper(
ctx context.Context,
params receiver.CreateSettings,
key string,
cfg internal.Config,
factories map[string]internal.ScraperFactory,
) (scraper scraperhelper.Scraper, err error) {
factory := factories[key]
if factory == nil {
return nil, fmt.Errorf("factory not found for scraper %q", key)
}
scraper, err = factory.CreateMetricsScraper(ctx, params, cfg)
if err != nil {
return nil, err
}
return
}