|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package core |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/url" |
| 21 | + "reflect" |
| 22 | + "runtime" |
| 23 | + "sort" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "go.opentelemetry.io/otel/propagation" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + Route string = "route" |
| 31 | + Controller string = "controller" |
| 32 | + Action string = "action" |
| 33 | + Framework string = "framework" |
| 34 | + Driver string = "db_driver" |
| 35 | + Traceparent string = "traceparent" |
| 36 | + Application string = "application" |
| 37 | +) |
| 38 | + |
| 39 | +type CommenterConfig struct { |
| 40 | + EnableDBDriver bool |
| 41 | + EnableRoute bool |
| 42 | + EnableFramework bool |
| 43 | + EnableController bool |
| 44 | + EnableAction bool |
| 45 | + EnableTraceparent bool |
| 46 | + EnableApplication bool |
| 47 | +} |
| 48 | + |
| 49 | +type StaticTags struct { |
| 50 | + Application string |
| 51 | + DriverName string |
| 52 | +} |
| 53 | + |
| 54 | +type CommenterOptions struct { |
| 55 | + Config CommenterConfig |
| 56 | + Tags StaticTags |
| 57 | +} |
| 58 | + |
| 59 | +func encodeURL(k string) string { |
| 60 | + return url.QueryEscape(string(k)) |
| 61 | +} |
| 62 | + |
| 63 | +func GetFunctionName(i interface{}) string { |
| 64 | + if i == nil { |
| 65 | + return "" |
| 66 | + } |
| 67 | + return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() |
| 68 | +} |
| 69 | + |
| 70 | +func ConvertMapToComment(tags map[string]string) string { |
| 71 | + var sb strings.Builder |
| 72 | + i, sz := 0, len(tags) |
| 73 | + |
| 74 | + //sort by keys |
| 75 | + sortedKeys := make([]string, 0, len(tags)) |
| 76 | + for k := range tags { |
| 77 | + sortedKeys = append(sortedKeys, k) |
| 78 | + } |
| 79 | + sort.Strings(sortedKeys) |
| 80 | + |
| 81 | + for _, key := range sortedKeys { |
| 82 | + if i == sz-1 { |
| 83 | + sb.WriteString(fmt.Sprintf("%s=%v", encodeURL(key), encodeURL(tags[key]))) |
| 84 | + } else { |
| 85 | + sb.WriteString(fmt.Sprintf("%s=%v,", encodeURL(key), encodeURL(tags[key]))) |
| 86 | + } |
| 87 | + i++ |
| 88 | + } |
| 89 | + return sb.String() |
| 90 | +} |
| 91 | + |
| 92 | +func ExtractTraceparent(ctx context.Context) propagation.MapCarrier { |
| 93 | + // Serialize the context into carrier |
| 94 | + textMapPropogator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}) |
| 95 | + carrier := propagation.MapCarrier{} |
| 96 | + textMapPropogator.Inject(ctx, carrier) |
| 97 | + return carrier |
| 98 | +} |
| 99 | + |
| 100 | +type RequestTagsProvider interface { |
| 101 | + Route() string |
| 102 | + Action() string |
| 103 | + Framework() string |
| 104 | +} |
| 105 | + |
| 106 | +func ContextInject(ctx context.Context, h RequestTagsProvider) context.Context { |
| 107 | + ctx = context.WithValue(ctx, Route, h.Route()) |
| 108 | + ctx = context.WithValue(ctx, Action, h.Action()) |
| 109 | + ctx = context.WithValue(ctx, Framework, h.Framework()) |
| 110 | + return ctx |
| 111 | +} |
0 commit comments