|
| 1 | +// Copyright 2020, OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package hostmetricsreceiver |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "runtime" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/spf13/viper" |
| 25 | + |
| 26 | + "github.com/open-telemetry/opentelemetry-collector/component" |
| 27 | + "github.com/open-telemetry/opentelemetry-collector/config/configerror" |
| 28 | + "github.com/open-telemetry/opentelemetry-collector/config/configmodels" |
| 29 | + "github.com/open-telemetry/opentelemetry-collector/consumer" |
| 30 | + "github.com/open-telemetry/opentelemetry-collector/receiver/hostmetricsreceiver/scraper" |
| 31 | +) |
| 32 | + |
| 33 | +// This file implements Factory for HostMetrics receiver. |
| 34 | + |
| 35 | +const ( |
| 36 | + // The value of "type" key in configuration. |
| 37 | + typeStr = "hostmetrics" |
| 38 | + scrapersKey = "scrapers" |
| 39 | +) |
| 40 | + |
| 41 | +// Factory is the Factory for receiver. |
| 42 | +type Factory struct { |
| 43 | + ScraperFactories map[string]scraper.Factory |
| 44 | +} |
| 45 | + |
| 46 | +// Type gets the type of the Receiver config created by this Factory. |
| 47 | +func (f *Factory) Type() string { |
| 48 | + return typeStr |
| 49 | +} |
| 50 | + |
| 51 | +// CustomUnmarshaler returns custom unmarshaler for this config. |
| 52 | +func (f *Factory) CustomUnmarshaler() component.CustomUnmarshaler { |
| 53 | + return func(componentViperSection *viper.Viper, intoCfg interface{}) error { |
| 54 | + |
| 55 | + // load the non-dynamic config normally |
| 56 | + |
| 57 | + err := componentViperSection.UnmarshalExact(intoCfg) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + cfg, ok := intoCfg.(*Config) |
| 63 | + if !ok { |
| 64 | + return fmt.Errorf("config type not hostmetrics.Config") |
| 65 | + } |
| 66 | + |
| 67 | + // dynamically load the individual collector configs based on the key name |
| 68 | + |
| 69 | + cfg.Scrapers = map[string]scraper.Config{} |
| 70 | + |
| 71 | + scrapersViperSection := componentViperSection.Sub(scrapersKey) |
| 72 | + if scrapersViperSection == nil || len(scrapersViperSection.AllKeys()) == 0 { |
| 73 | + return fmt.Errorf("must specify at least one scraper when using hostmetrics receiver") |
| 74 | + } |
| 75 | + |
| 76 | + for key := range componentViperSection.GetStringMap(scrapersKey) { |
| 77 | + factory, ok := f.ScraperFactories[key] |
| 78 | + if !ok { |
| 79 | + return fmt.Errorf("invalid hostmetrics scraper key: %s", key) |
| 80 | + } |
| 81 | + |
| 82 | + collectorCfg := factory.CreateDefaultConfig() |
| 83 | + collectorViperSection := scrapersViperSection.Sub(key) |
| 84 | + if collectorViperSection != nil { |
| 85 | + err := collectorViperSection.UnmarshalExact(collectorCfg) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("error reading settings for hostmetric scraper type %q: %v", key, err) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + cfg.Scrapers[key] = collectorCfg |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// CreateDefaultConfig creates the default configuration for receiver. |
| 99 | +func (f *Factory) CreateDefaultConfig() configmodels.Receiver { |
| 100 | + return &Config{ |
| 101 | + ReceiverSettings: configmodels.ReceiverSettings{ |
| 102 | + TypeVal: typeStr, |
| 103 | + NameVal: typeStr, |
| 104 | + }, |
| 105 | + DefaultCollectionInterval: 10 * time.Second, |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// CreateTraceReceiver creates a trace receiver based on provided config. |
| 110 | +func (f *Factory) CreateTraceReceiver( |
| 111 | + ctx context.Context, |
| 112 | + params component.ReceiverCreateParams, |
| 113 | + cfg configmodels.Receiver, |
| 114 | + consumer consumer.TraceConsumer, |
| 115 | +) (component.TraceReceiver, error) { |
| 116 | + // Host Metrics does not support traces |
| 117 | + return nil, configerror.ErrDataTypeIsNotSupported |
| 118 | +} |
| 119 | + |
| 120 | +// CreateMetricsReceiver creates a metrics receiver based on provided config. |
| 121 | +func (f *Factory) CreateMetricsReceiver( |
| 122 | + ctx context.Context, |
| 123 | + params component.ReceiverCreateParams, |
| 124 | + cfg configmodels.Receiver, |
| 125 | + consumer consumer.MetricsConsumer, |
| 126 | +) (component.MetricsReceiver, error) { |
| 127 | + |
| 128 | + if runtime.GOOS != "windows" { |
| 129 | + return nil, errors.New("hostmetrics receiver is currently only supported on windows") |
| 130 | + } |
| 131 | + |
| 132 | + config := cfg.(*Config) |
| 133 | + |
| 134 | + hmr, err := NewHostMetricsReceiver(ctx, params.Logger, config, f.ScraperFactories, consumer) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + return hmr, nil |
| 140 | +} |
0 commit comments