/
provider.go
99 lines (80 loc) · 2.69 KB
/
provider.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
//go:build !windows
package dimension
import (
"log"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/aws/amazon-cloudwatch-agent-test/environment"
)
type ExpectedDimensionValue struct {
Value *string
}
func (d *ExpectedDimensionValue) IsKnown() bool {
return d.Value != nil
}
func UnknownDimensionValue() ExpectedDimensionValue {
return ExpectedDimensionValue{Value: nil}
}
func GetDimensionFactory(env environment.MetaData) Factory {
allDimensionProviders := []IProvider{
&EMFECSDimensionProvider{Provider: Provider{env: env}},
&EKSClusterNameProvider{Provider: Provider{env: env}},
&ContainerInsightsDimensionProvider{Provider: Provider{env: env}},
&HostDimensionProvider{Provider: Provider{env: env}},
&LocalInstanceIdDimensionProvider{Provider: Provider{env: env}},
&LocalImageIdDimensionProvider{Provider: Provider{env: env}},
&LocalInstanceTypeDimensionProvider{Provider: Provider{env: env}},
&ECSInstanceIdDimensionProvider{Provider: Provider{env: env}},
&CustomDimensionProvider{Provider: Provider{env: env}},
}
applicableDimensionProviders := []IProvider{}
for _, provider := range allDimensionProviders {
if provider.IsApplicable() {
applicableDimensionProviders = append(applicableDimensionProviders, provider)
}
}
return Factory{applicableDimensionProviders}
}
type Instruction struct {
Key string
Value ExpectedDimensionValue
}
type Factory struct {
Providers []IProvider
}
func (f *Factory) GetDimensions(instructions []Instruction) ([]types.Dimension, []Instruction) {
resultDimensions := []types.Dimension{}
unfulfilledInstructions := []Instruction{}
for _, instruction := range instructions {
dim := f.executeInstruction(instruction)
if (dim != types.Dimension{}) {
resultDimensions = append(resultDimensions, dim)
log.Printf("Result dim is : %s, %s", *dim.Name, *dim.Value)
} else {
unfulfilledInstructions = append(unfulfilledInstructions, instruction)
if dim.Name != nil && dim.Value != nil {
log.Printf("unfulfilled dim is : %s, %s", *dim.Name, *dim.Value)
}
}
}
return resultDimensions, unfulfilledInstructions
}
func (f *Factory) executeInstruction(instruction Instruction) types.Dimension {
for _, provider := range f.Providers {
dim := provider.GetDimension(instruction)
log.Printf("instruction %v provider %s returned dimension %v", instruction, provider.Name(), dim)
if (dim != types.Dimension{}) {
return dim
}
}
return types.Dimension{}
}
type IProvider interface {
IsApplicable() bool
GetDimension(Instruction) types.Dimension
Name() string
}
type Provider struct {
env environment.MetaData
}