-
Notifications
You must be signed in to change notification settings - Fork 0
/
appcontext.go
64 lines (53 loc) · 1.95 KB
/
appcontext.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
package telemetry
import (
"context"
"log"
)
// OperationIDKey represents the key type for the operation ID in context
type OperationIDKey string
type TelemetryObj string
type ServiceNameKey string
const (
// OperationIDKeyContextKey is the key used to store the operation ID in context
OperationIDKeyContextKey OperationIDKey = "operationID"
// TelemetryContextKey represents the key type for the telemetry object in context
TelemetryContextKey TelemetryObj = "telemetry"
// Service name key
ServiceNameContextKey ServiceNameKey = "ServiceName"
)
// Helper function to retrieve the telemetry client from the context
func GetXTelemetryClient(ctx context.Context) *XTelemetryObjectImpl {
telemetryClient, ok := ctx.Value(TelemetryContextKey).(*XTelemetryObjectImpl)
if !ok {
log.Panic("Telemetry client not found in context")
}
return telemetryClient
}
// Helper function to retrieve the operation ID from the context
func GetOperationID(ctx context.Context) string {
operationID, ok := ctx.Value(OperationIDKeyContextKey).(string)
if !ok {
log.Panic("Operation ID not found in context")
}
return operationID
}
// Helper function to retrieve the service name from the context
func GetServiceName(ctx context.Context) string {
serviceName, ok := ctx.Value(ServiceNameContextKey).(string)
if !ok {
log.Panic("Service name not found in context")
}
return serviceName
}
// Set the telemetry client in the context
func SetXTelemetryClient(ctx context.Context, telemetryClient *XTelemetryObjectImpl) context.Context {
return context.WithValue(ctx, TelemetryContextKey, telemetryClient)
}
// Set the operation ID in the context
func SetOperationID(ctx context.Context, operationID string) context.Context {
return context.WithValue(ctx, OperationIDKeyContextKey, operationID)
}
// Set the service name in the context
func SetServiceName(ctx context.Context, serviceName string) context.Context {
return context.WithValue(ctx, ServiceNameContextKey, serviceName)
}