• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

elastic / cloudbeat / 8596844961

08 Apr 2024 08:19AM UTC coverage: 75.999% (-0.008%) from 76.007%
8596844961

push

github

web-flow
Merge ca9e98def into 61b21ecb2

4 of 8 new or added lines in 3 files covered. (50.0%)

7324 of 9637 relevant lines covered (76.0%)

18.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/internal/flavors/posture.go
1
// Licensed to Elasticsearch B.V. under one or more contributor
2
// license agreements. See the NOTICE file distributed with
3
// this work for additional information regarding copyright
4
// ownership. Elasticsearch B.V. licenses this file to you under
5
// the Apache License, Version 2.0 (the "License"); you may
6
// not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
//     http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17

18
package flavors
19

20
import (
21
        "context"
22
        "fmt"
23

24
        "github.com/elastic/beats/v7/libbeat/beat"
25
        agentconfig "github.com/elastic/elastic-agent-libs/config"
26
        "github.com/elastic/elastic-agent-libs/logp"
27

28
        "github.com/elastic/cloudbeat/internal/config"
29
        "github.com/elastic/cloudbeat/internal/flavors/benchmark"
30
        "github.com/elastic/cloudbeat/internal/flavors/benchmark/builder"
31
        _ "github.com/elastic/cloudbeat/internal/processor" // Add cloudbeat default processors.
32
)
33

34
type posture struct {
35
        flavorBase
36
        benchmark builder.Benchmark
37
}
38

39
// NewPosture creates an instance of posture.
40
func NewPosture(b *beat.Beat, agentConfig *agentconfig.C) (beat.Beater, error) {
×
41
        cfg, err := config.New(agentConfig)
×
42
        if err != nil {
×
43
                return nil, fmt.Errorf("error reading config file: %w", err)
×
44
        }
×
45
        return newPostureFromCfg(b, cfg)
×
46
}
47

48
// NewPosture creates an instance of posture.
49
func newPostureFromCfg(b *beat.Beat, cfg *config.Config) (*posture, error) {
×
50
        log := logp.NewLogger("posture")
×
51
        log.Info("Config initiated with cycle period of ", cfg.Period)
×
52
        ctx, cancel := context.WithCancel(context.Background())
×
53

×
NEW
54
        strategy, err := benchmark.GetStrategy(cfg, log)
×
55
        if err != nil {
×
56
                cancel()
×
57
                return nil, err
×
58
        }
×
59

60
        log.Infof("Creating benchmark %T", strategy)
×
61
        bench, err := strategy.NewBenchmark(ctx, log, cfg)
×
62
        if err != nil {
×
63
                cancel()
×
64
                return nil, err
×
65
        }
×
66

67
        client, err := NewClient(b.Publisher, cfg.Processors)
×
68
        if err != nil {
×
69
                cancel()
×
70
                return nil, fmt.Errorf("failed to init client: %w", err)
×
71
        }
×
72
        log.Infof("posture configured %d processors", len(cfg.Processors))
×
73

×
74
        publisher := NewPublisher(log, flushInterval, eventsThreshold, client)
×
75

×
76
        return &posture{
×
77
                flavorBase: flavorBase{
×
78
                        ctx:       ctx,
×
79
                        cancel:    cancel,
×
80
                        publisher: publisher,
×
81
                        config:    cfg,
×
82
                        log:       log,
×
83
                },
×
84
                benchmark: bench,
×
85
        }, nil
×
86
}
87

88
// Run starts posture.
89
func (bt *posture) Run(*beat.Beat) error {
×
90
        bt.log.Info("posture is running! Hit CTRL-C to stop it")
×
91
        eventsCh, err := bt.benchmark.Run(bt.ctx)
×
92
        if err != nil {
×
93
                return err
×
94
        }
×
95

96
        bt.publisher.HandleEvents(bt.ctx, eventsCh)
×
97
        bt.log.Warn("Posture has finished running")
×
98
        return nil
×
99
}
100

101
// Stop stops posture.
102
func (bt *posture) Stop() {
×
103
        bt.benchmark.Stop()
×
104

×
105
        if err := bt.client.Close(); err != nil {
×
106
                bt.log.Fatal("Cannot close client", err)
×
107
        }
×
108

109
        bt.cancel()
×
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc