|
| 1 | +// Copyright 2019, OpenCensus 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 vmmetricsreceiver |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "runtime" |
| 22 | + "sync" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/spf13/viper" |
| 26 | + |
| 27 | + "github.com/census-instrumentation/opencensus-service/consumer" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + errAlreadyStarted = errors.New("already started") |
| 32 | + errAlreadyStopped = errors.New("already stopped") |
| 33 | + errNilMetricsConsumer = errors.New("expecting a non-nil MetricsConsumer") |
| 34 | +) |
| 35 | + |
| 36 | +// Configuration defines the behavior and targets of the VM metrics scrapers. |
| 37 | +type Configuration struct { |
| 38 | + scrapeInterval time.Duration `mapstructure:"scrape_interval"` |
| 39 | + mountPoint string `mapstructure:"mount_point"` |
| 40 | + metricPrefix string `mapstructure:"metric_prefix"` |
| 41 | +} |
| 42 | + |
| 43 | +// Receiver is the type used to handle metrics from VM metrics. |
| 44 | +type Receiver struct { |
| 45 | + mu sync.Mutex |
| 46 | + |
| 47 | + vmc *VMMetricsCollector |
| 48 | + |
| 49 | + stopOnce sync.Once |
| 50 | + startOnce sync.Once |
| 51 | +} |
| 52 | + |
| 53 | +// New creates a new vmmetricsreceiver.Receiver reference. |
| 54 | +func New(v *viper.Viper, consumer consumer.MetricsConsumer) (*Receiver, error) { |
| 55 | + if consumer == nil { |
| 56 | + return nil, errNilMetricsConsumer |
| 57 | + } |
| 58 | + |
| 59 | + var cfg Configuration |
| 60 | + |
| 61 | + // Unmarshal our config values (using viper's mapstructure) |
| 62 | + err := v.Unmarshal(&cfg) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("vmmetrics receiver failed to parse config: %s", err) |
| 65 | + } |
| 66 | + |
| 67 | + vmc, err := NewVMMetricsCollector(cfg.scrapeInterval, cfg.mountPoint, cfg.metricPrefix, consumer) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + vmr := &Receiver{ |
| 73 | + vmc: vmc, |
| 74 | + } |
| 75 | + return vmr, nil |
| 76 | +} |
| 77 | + |
| 78 | +// StartMetricsReception scrapes VM metrics based on the OS platform. |
| 79 | +func (vmr *Receiver) StartMetricsReception(ctx context.Context, asyncErrorChan chan<- error) error { |
| 80 | + vmr.mu.Lock() |
| 81 | + defer vmr.mu.Unlock() |
| 82 | + |
| 83 | + var err = errAlreadyStarted |
| 84 | + vmr.startOnce.Do(func() { |
| 85 | + switch runtime.GOOS { |
| 86 | + case "linux": |
| 87 | + vmr.vmc.StartCollection() |
| 88 | + case "darwin", "freebsd", "windows": |
| 89 | + // TODO: add support for other platforms. |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + err = nil |
| 94 | + }) |
| 95 | + return err |
| 96 | +} |
| 97 | + |
| 98 | +// StopMetricsReception stops and cancels the underlying VM metrics scrapers. |
| 99 | +func (vmr *Receiver) StopMetricsReception(ctx context.Context) error { |
| 100 | + vmr.mu.Lock() |
| 101 | + defer vmr.mu.Unlock() |
| 102 | + |
| 103 | + var err = errAlreadyStopped |
| 104 | + vmr.stopOnce.Do(func() { |
| 105 | + vmr.vmc.StopCollection() |
| 106 | + err = nil |
| 107 | + }) |
| 108 | + return err |
| 109 | +} |
0 commit comments