-
Notifications
You must be signed in to change notification settings - Fork 20
/
telemetry.go
77 lines (70 loc) · 2.19 KB
/
telemetry.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/
package http
import (
"fmt"
"os"
"time"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
"github.com/valyala/fasthttp"
)
type Telemetry struct {
Properties map[string]interface{}
}
var client appinsights.TelemetryClient
var initialized bool
func initClient(properties map[string]interface{}) {
instrumentationKey := os.Getenv("APP_INSIGHT_KEY")
if instrumentationKey == "" {
initialized = false
} else {
telemetryConfig := appinsights.NewTelemetryConfiguration(instrumentationKey)
telemetryConfig.MaxBatchSize = 8192
if batchSize, ok := properties["maxBatchSize"]; ok {
if batchSizeFloat, fok := batchSize.(float64); fok {
telemetryConfig.MaxBatchSize = int(batchSizeFloat)
}
if batchSizeInt, iok := batchSize.(int); iok {
telemetryConfig.MaxBatchSize = batchSizeInt
}
}
telemetryConfig.MaxBatchInterval = 2 * time.Second
if batchInterval, ok := properties["maxBatchIntervalSeconds"]; ok {
if batchIntervalFloat, fok := batchInterval.(float64); fok {
telemetryConfig.MaxBatchInterval = time.Duration(int(batchIntervalFloat)) * time.Second
}
if batchIntervalInt, iok := batchInterval.(int); iok {
telemetryConfig.MaxBatchInterval = time.Duration(batchIntervalInt) * time.Second
}
}
client = appinsights.NewTelemetryClientFromConfig(telemetryConfig)
initialized = true
}
}
// CORS middleware to allow CORS. The middleware doesn't override existing headers in incoming requests
func (c Telemetry) Telemetry(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
if v, ok := c.Properties["enabled"]; !ok || v != true {
next(ctx)
return
}
if !initialized {
initClient(c.Properties)
}
if initialized {
path := string(ctx.Path())
method := string(ctx.Method())
eventId := fmt.Sprintf("%s-%s", path, method)
event := appinsights.NewEventTelemetry(eventId)
event.Properties["client"] = fmt.Sprintf("%v", c.Properties["client"])
next(ctx)
event.Properties["status"] = fmt.Sprintf("%v", ctx.Response.StatusCode())
client.Track(event)
} else {
next(ctx)
}
}
}