github.com/kaydxh/golang@v0.0.131/pkg/middleware/resource/monitor.resource.go (about) 1 package resource 2 3 import ( 4 "context" 5 "fmt" 6 7 context_ "github.com/kaydxh/golang/go/context" 8 errors_ "github.com/kaydxh/golang/go/errors" 9 net_ "github.com/kaydxh/golang/go/net" 10 app_ "github.com/kaydxh/golang/pkg/webserver/app" 11 "go.opentelemetry.io/otel" 12 "go.opentelemetry.io/otel/attribute" 13 ) 14 15 //dims 16 var ( 17 CallerMethodKey = attribute.Key("caller_method") // caller method 18 CalleeMethodKey = attribute.Key("callee_method") // callee method 19 PodIpKey = attribute.Key("pod_ip") // pod ip 20 ServerNameKey = attribute.Key("server_name") // server name 21 ErrorCodeKey = attribute.Key("error_code") // error code 22 ) 23 24 var OpentelemetryDimsKey = "opentelemetry_dims_key" 25 var OpentelemetryMetricsKey = "opentelemetry_metrics_key" 26 27 type Dimension struct { 28 CalleeMethod string 29 Error error 30 } 31 32 type AddKeyContexFunc func(ctx context.Context) context.Context 33 34 func AddKeysContext(ctx context.Context, ops ...AddKeyContexFunc) context.Context { 35 for _, op := range ops { 36 ctx = op(ctx) 37 } 38 return ctx 39 } 40 41 func AddAttrKeysContext(ctx context.Context) context.Context { 42 return context.WithValue(ctx, OpentelemetryDimsKey, map[string]interface{}{}) 43 } 44 45 func UpdateAttrsContext(ctx context.Context, values map[string]interface{}) error { 46 return context_.UpdateContext(ctx, OpentelemetryDimsKey, values) 47 } 48 49 func AddMetricKeysContext(ctx context.Context) context.Context { 50 return context.WithValue(ctx, OpentelemetryMetricsKey, map[string]interface{}{}) 51 } 52 53 func UpdateMetricContext(ctx context.Context, values map[string]interface{}) error { 54 return context_.UpdateContext(ctx, OpentelemetryMetricsKey, values) 55 } 56 57 func AppendAttrsContext(ctx context.Context, values ...string) context.Context { 58 return context_.AppendContext(ctx, OpentelemetryDimsKey, values...) 59 } 60 61 func AppendMetricCountContext(ctx context.Context, values ...string) context.Context { 62 return context_.AppendContext(ctx, OpentelemetryMetricsKey, values...) 63 } 64 65 func ExtractAttrsWithContext(ctx context.Context) []attribute.KeyValue { 66 var attrs []attribute.KeyValue 67 68 values, ok := ctx.Value(OpentelemetryDimsKey).(map[string]interface{}) 69 if !ok { 70 return nil 71 } 72 for key, value := range values { 73 var dimKey = attribute.Key(key) 74 switch value := value.(type) { 75 case string: 76 attrs = append(attrs, dimKey.String(value)) 77 78 case int: 79 attrs = append(attrs, dimKey.Int(value)) 80 case int32: 81 attrs = append(attrs, dimKey.Int(int(value))) 82 case int64: 83 attrs = append(attrs, dimKey.Int64(int64(value))) 84 85 case uint: 86 attrs = append(attrs, dimKey.Int(int(value))) 87 case uint32: 88 attrs = append(attrs, dimKey.Int(int(value))) 89 case uint64: 90 attrs = append(attrs, dimKey.Int64(int64(value))) 91 92 case float32: 93 attrs = append(attrs, dimKey.Float64(float64(value))) 94 case float64: 95 attrs = append(attrs, dimKey.Float64(value)) 96 97 case bool: 98 attrs = append(attrs, dimKey.Bool(value)) 99 } 100 } 101 102 return attrs 103 } 104 105 func ReportBusinessMetric(ctx context.Context, attrs []attribute.KeyValue) { 106 values, ok := ctx.Value(OpentelemetryMetricsKey).(map[string]interface{}) 107 if !ok { 108 return 109 } 110 111 for key, value := range values { 112 113 var ( 114 n int64 115 f float64 116 counterType bool 117 ) 118 switch value := value.(type) { 119 case int: 120 n = int64(value) 121 counterType = true 122 123 case int32: 124 n = int64(value) 125 counterType = true 126 case int64: 127 n = int64(value) 128 counterType = true 129 130 case uint: 131 n = int64(value) 132 counterType = true 133 case uint32: 134 n = int64(value) 135 counterType = true 136 case uint64: 137 n = int64(value) 138 counterType = true 139 140 case float32: 141 f = float64(value) 142 case float64: 143 f = float64(value) 144 } 145 146 if counterType { 147 counter, err := DefaultMetricMonitor.GetOrNewBusinessCounter(key) 148 if err != nil { 149 otel.Handle(err) 150 continue 151 } 152 counter.Add(ctx, n, attrs...) 153 154 } else { 155 156 histogram, err := DefaultMetricMonitor.GetOrNewBusinessHistogram(key) 157 if err != nil { 158 otel.Handle(err) 159 continue 160 } 161 histogram.Record(ctx, f, attrs...) 162 } 163 } 164 165 return 166 } 167 168 func Attrs(dim Dimension) []attribute.KeyValue { 169 var attrs []attribute.KeyValue 170 hostIP, err := net_.GetHostIP() 171 if err == nil && hostIP.String() != "" { 172 attrs = append(attrs, PodIpKey.String(hostIP.String())) 173 } 174 if dim.CalleeMethod != "" { 175 attrs = append(attrs, CalleeMethodKey.String(dim.CalleeMethod)) 176 } 177 178 if dim.Error != nil { 179 errorCode := int64(errors_.ErrorToCode(dim.Error)) 180 message := errors_.ErrorToString(dim.Error) 181 attrs = append(attrs, ErrorCodeKey.String(fmt.Sprintf("%d:%s", errorCode, message))) 182 } else { 183 attrs = append(attrs, ErrorCodeKey.String("0:OK")) 184 } 185 186 appName := app_.GetVersion().AppName 187 if appName != "" { 188 attrs = append(attrs, ServerNameKey.String(appName)) 189 } 190 191 return attrs 192 }