This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
reportprocessor.go
108 lines (95 loc) · 3.07 KB
/
reportprocessor.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
// Copyright 2017 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.
package svcctrl
import (
"context"
"fmt"
"time"
"github.com/pborman/uuid"
sc "google.golang.org/api/servicecontrol/v1"
"istio.io/istio/mixer/adapter/svcctrl/config"
"istio.io/istio/mixer/adapter/svcctrl/template/svcctrlreport"
"istio.io/istio/mixer/pkg/adapter"
)
type (
reportImpl struct {
env adapter.Env
serviceConfig *config.GcpServiceSetting
client serviceControlClient
resolver consumerProjectIDResolver
}
)
// ProcessReport processes svcctrlreport data point and send metrics and logs via Google ServiceControl API.
func (r *reportImpl) ProcessReport(ctx context.Context, instances []*svcctrlreport.Instance) error {
logger := r.env.Logger()
for _, inst := range instances {
builder := newReportBuilder(inst, supportedMetrics, r.resolver)
op := initializeOperation(inst)
builder.build(op)
if len(op.MetricValueSets) == 0 && len(op.LogEntries) == 0 {
logger.Warningf("no metric or log entry is generated, dimensions: %v", inst)
continue
}
r.scheduleReport(op)
}
return nil
}
func (r *reportImpl) Close() error {
return nil
}
// TODO(manlinl): Retry if report is failed to send.
func (r *reportImpl) scheduleReport(op *sc.Operation) {
r.env.ScheduleWork(func() {
logger := r.env.Logger()
request := &sc.ReportRequest{
Operations: []*sc.Operation{op},
}
response, err := r.client.Report(r.serviceConfig.GoogleServiceName, request)
if err != nil || response.ReportErrors != nil {
logger.Errorf("fail to send report: %v", err)
}
if logger.VerbosityLevel(logDebug) {
if requestDetail, err := toFormattedJSON(request); err == nil {
logger.Infof("request: %v", requestDetail)
}
if response != nil {
if responseDetail, err := toFormattedJSON(response); err == nil {
logger.Infof("response:%v", responseDetail)
}
}
}
})
}
func initializeOperation(inst *svcctrlreport.Instance) *sc.Operation {
op := &sc.Operation{}
op.ConsumerId, _ = generateConsumerID(inst)
op.OperationId = uuid.New()
op.OperationName = inst.ApiOperation
op.StartTime = inst.RequestTime.UTC().Format(time.RFC3339Nano)
op.EndTime = inst.ResponseTime.UTC().Format(time.RFC3339Nano)
return op
}
func newReportProcessor(meshServiceName string, ctx *handlerContext,
resolver consumerProjectIDResolver) (*reportImpl, error) {
serviceConfig, found := ctx.serviceConfigIndex[meshServiceName]
if !found {
return nil, fmt.Errorf("unknown mesh service:%v", meshServiceName)
}
return &reportImpl{
ctx.env,
serviceConfig,
ctx.client,
resolver,
}, nil
}