forked from microsoftgraph/msgraph-sdk-go-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_telemetry_handler.go
50 lines (45 loc) · 1.56 KB
/
graph_telemetry_handler.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
package msgraphgocore
import (
nethttp "net/http"
runtime "runtime"
uuid "github.com/google/uuid"
khttp "github.com/microsoft/kiota-http-go"
)
// GraphTelemetryHandler is a middleware handler that adds telemetry headers to requests.
type GraphTelemetryHandler struct {
sdkVersion string
}
// NewGraphTelemetryHandler creates a new GraphTelemetryHandler.
func NewGraphTelemetryHandler(options *GraphClientOptions) *GraphTelemetryHandler {
serviceVersionPrefix := ""
if options != nil && options.GraphServiceLibraryVersion != "" {
serviceVersionPrefix += "graph-go"
if options.GraphServiceVersion != "" {
serviceVersionPrefix += "-" + options.GraphServiceVersion
}
serviceVersionPrefix += "/" + options.GraphServiceLibraryVersion
serviceVersionPrefix += ", "
}
featuresSuffix := ""
if runtime.GOOS != "" {
featuresSuffix += " hostOS=" + runtime.GOOS + ";"
}
if runtime.GOARCH != "" {
featuresSuffix += " hostArch=" + runtime.GOARCH + ";"
}
goVersion := runtime.Version()
if goVersion != "" {
featuresSuffix += " runtimeEnvironment=" + goVersion + ";"
}
if featuresSuffix != "" {
featuresSuffix = " (" + featuresSuffix[1:] + ")"
}
return &GraphTelemetryHandler{
sdkVersion: serviceVersionPrefix + "graph-go-core/" + CoreVersion + featuresSuffix,
}
}
func (middleware GraphTelemetryHandler) Intercept(pipeline khttp.Pipeline, middlewareIndex int, req *nethttp.Request) (*nethttp.Response, error) {
req.Header.Add("SdkVersion", middleware.sdkVersion)
req.Header.Add("client-request-id", uuid.NewString())
return pipeline.Next(req, middlewareIndex)
}