This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solarwinds.go
149 lines (129 loc) · 4.17 KB
/
solarwinds.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2018 Istio Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:generate $GOPATH/src/istio.io/istio/bin/mixer_codegen.sh -f mixer/adapter/solarwinds/config/config.proto
// Package solarwinds publishes metric and log values collected by Mixer
// to appoptics and papertrail respectively.
package solarwinds
import (
"context"
"fmt"
"regexp"
"istio.io/istio/mixer/adapter/solarwinds/config"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/template/logentry"
"istio.io/istio/mixer/template/metric"
)
type (
builder struct {
cfg *config.Params
metricTypes map[string]*metric.Type
logentryTypes map[string]*logentry.Type
}
handler struct {
logger adapter.Logger
metricsHandler metricsHandlerInterface
logHandler logHandlerInterface
}
)
const paperTrailURLPattern = `logs\d+.papertrailapp.com\:\d{3,5}`
var (
_ metric.HandlerBuilder = &builder{}
_ metric.Handler = &handler{}
)
// GetInfo returns the Info associated with this adapter.
func GetInfo() adapter.Info {
return adapter.Info{
Name: "solarwinds",
Impl: "istio.io/istio/mixer/adapter/solarwinds",
Description: "Publishes metrics to appoptics and logs to papertrail",
SupportedTemplates: []string{
metric.TemplateName,
logentry.TemplateName,
},
NewBuilder: func() adapter.HandlerBuilder { return &builder{} },
DefaultConfig: &config.Params{},
}
}
func (b *builder) SetAdapterConfig(cfg adapter.Config) {
// this is a common adapter config for both log and metric
b.cfg = cfg.(*config.Params)
}
func (b *builder) SetMetricTypes(mts map[string]*metric.Type) {
b.metricTypes = mts
}
func (b *builder) SetLogEntryTypes(entries map[string]*logentry.Type) {
b.logentryTypes = entries
}
func (b *builder) Validate() (ce *adapter.ConfigErrors) {
if b.cfg.AppopticsBatchSize < 0 || b.cfg.AppopticsBatchSize > 1000 {
ce = ce.Append("appoptics_batch_size", fmt.Errorf("appoptics batch size provided is not in the range from 1 to 1000"))
}
if b.cfg.PapertrailUrl != "" {
if re := regexp.MustCompile(paperTrailURLPattern); !re.MatchString(b.cfg.PapertrailUrl) {
ce = ce.Append("paper_trail_url",
fmt.Errorf("papertrail url provided is invalid: %v. It did not match the pattern: %s",
b.cfg.PapertrailUrl, paperTrailURLPattern))
}
}
for instName, inst := range b.cfg.Metrics {
mInst, ok := b.metricTypes[instName]
if !ok {
ce = ce.Append("metrics", fmt.Errorf("%s is an invalid metric instance name", instName))
} else {
for _, label := range inst.LabelNames {
if _, ok := mInst.Dimensions[label]; !ok {
ce = ce.Append("metric label", fmt.Errorf("%s is an invalid metric label name", label))
}
}
}
}
for inst := range b.cfg.Logs {
if _, ok := b.logentryTypes[inst]; !ok {
ce = ce.Append("metrics", fmt.Errorf("%s is an invalid logentry instance name", inst))
}
}
return
}
func (b *builder) Build(ctx context.Context, env adapter.Env) (adapter.Handler, error) {
logger := env.Logger()
m, err := newMetricsHandler(ctx, env, b.cfg)
if err != nil {
return nil, err
}
l, err := newLogHandler(ctx, env, b.cfg)
if err != nil {
_ = m.close()
return nil, err
}
return &handler{
metricsHandler: m,
logHandler: l,
logger: logger,
}, nil
}
func (h *handler) HandleMetric(ctx context.Context, vals []*metric.Instance) error {
return h.metricsHandler.handleMetric(ctx, vals)
}
func (h *handler) HandleLogEntry(ctx context.Context, values []*logentry.Instance) error {
return h.logHandler.handleLogEntry(ctx, values)
}
func (h *handler) Close() error {
if h.metricsHandler != nil {
_ = h.metricsHandler.close()
}
if h.logHandler != nil {
_ = h.logHandler.close()
}
return nil
}