-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathzap_appinsights.go
More file actions
149 lines (126 loc) · 4.02 KB
/
Copy pathzap_appinsights.go
File metadata and controls
149 lines (126 loc) · 4.02 KB
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
package zapappinsigths
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/Microsoft/ApplicationInsights-Go/appinsights"
"github.com/Microsoft/ApplicationInsights-Go/appinsights/contracts"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type AppInsightsConfig struct {
client appinsights.TelemetryClient
async bool
//levels []zapcore.Level
filters map[string]func(interface{}) interface{}
}
// Config for application insights
type Config struct {
InstrumentationKey string
EndpointURL string
MaxBatchSize int
MaxBatchInterval time.Duration
}
func NewAppInsightsCore(conf Config, fs ...zapcore.Field) (zapcore.Core, zap.Option, error) {
allLevels := zap.LevelEnablerFunc(func(l zapcore.Level) bool { return true })
config := zap.NewProductionEncoderConfig()
config.EncodeLevel = appInsightsLevelEncoder
jsonEncode := zapcore.NewJSONEncoder(config)
option := zap.Fields(append(fs)...)
if conf.InstrumentationKey == "" {
return nil, nil, fmt.Errorf("InstrumentationKey is required and missing from configuration")
}
telemetryConf := appinsights.NewTelemetryConfiguration(conf.InstrumentationKey)
if conf.MaxBatchSize != 0 {
telemetryConf.MaxBatchSize = conf.MaxBatchSize
}
if conf.MaxBatchInterval != 0 {
telemetryConf.MaxBatchInterval = conf.MaxBatchInterval
}
if conf.EndpointURL != "" {
telemetryConf.EndpointUrl = conf.EndpointURL
}
telemetryClient := appinsights.NewTelemetryClientFromConfig(telemetryConf)
appInsightsConfig := AppInsightsConfig{
client: telemetryClient,
//levels: defaultLevels,
filters: make(map[string]func(interface{}) interface{}),
}
syncer := New(&appInsightsConfig)
return zapcore.NewCore(jsonEncode, syncer, allLevels), option, nil
}
var defaultLevels = []zapcore.Level{
zapcore.FatalLevel,
zapcore.PanicLevel,
zapcore.DPanicLevel,
zapcore.ErrorLevel,
zapcore.WarnLevel,
zapcore.InfoLevel,
zapcore.DebugLevel,
}
var levelMap = map[string]contracts.SeverityLevel{
"Critical": appinsights.Critical,
"Error": appinsights.Error,
"Warning": appinsights.Warning,
"Information": appinsights.Information,
"Verbose": appinsights.Verbose,
}
// appInsightsLevelEncoder maps the zap log levels to Application Insights levels.
func appInsightsLevelEncoder(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
switch l {
case zapcore.DebugLevel:
enc.AppendString(contracts.Verbose.String())
case zapcore.InfoLevel:
enc.AppendString(contracts.Information.String())
case zapcore.WarnLevel:
enc.AppendString(contracts.Warning.String())
case zapcore.ErrorLevel:
enc.AppendString(contracts.Error.String())
case zapcore.DPanicLevel:
enc.AppendString(contracts.Critical.String())
case zapcore.PanicLevel:
enc.AppendString(contracts.Critical.String())
case zapcore.FatalLevel:
enc.AppendString(contracts.Critical.String())
}
}
// New returns an implementation of ZapWriteSyncer which should be compatible with zap.WriteSyncer
func New(appInsightsConfig *AppInsightsConfig) zapcore.WriteSyncer {
return appInsightsConfig
}
func (appInsightsConfig *AppInsightsConfig) Sync() error {
// currently a noop.
return nil
}
func BuildTrace(data map[string]interface{}) *appinsights.TraceTelemetry {
message := data["msg"].(string)
level := levelMap[data["level"].(string)]
trace := appinsights.NewTraceTelemetry(message, level)
for k, v := range data {
switch k {
case "msg", "level":
break
default:
// Currently AppInsights Go SDK only support custom dimension (filter with string values)
switch v.(type) {
case int:
trace.BaseTelemetry.Properties[k] = string(v.(int))
case string:
trace.BaseTelemetry.Properties[k] = v.(string)
case float64:
trace.BaseTelemetry.Properties[k] = strconv.FormatFloat(v.(float64), 'f', 6, 64)
}
}
}
return trace
}
func (appInsightsConfig *AppInsightsConfig) Write(p []byte) (int, error) {
var data map[string]interface{}
if err := json.Unmarshal(p, &data); err != nil {
panic(err)
}
trace := BuildTrace(data)
go appInsightsConfig.client.Track(trace)
return len(trace.Message), nil
}