|
| 1 | +package telemetryconvcheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "go/ast" |
| 6 | + "go/constant" |
| 7 | + "go/token" |
| 8 | + "go/types" |
| 9 | + "slices" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/samber/lo" |
| 13 | + "golang.org/x/tools/go/analysis" |
| 14 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 15 | + "golang.org/x/tools/go/ast/inspector" |
| 16 | +) |
| 17 | + |
| 18 | +const defaultConvPackage = "github.com/authzed/spicedb/internal/telemetry/otelconv" |
| 19 | + |
| 20 | +var ( |
| 21 | + validPrefixes = []string{"spicedb.internal.", "spicedb.external."} |
| 22 | + convPackage *string |
| 23 | +) |
| 24 | + |
| 25 | +func Analyzer() *analysis.Analyzer { |
| 26 | + flagSet := flag.NewFlagSet("telemetryconvcheck", flag.ExitOnError) |
| 27 | + skip := flagSet.String("skip-pkg", "", "package(s) to skip for linting") |
| 28 | + convPackage = flagSet.String("conv-pkg", defaultConvPackage, "package that should contain the constants to use instead of the hardcoded strings") |
| 29 | + |
| 30 | + return &analysis.Analyzer{ |
| 31 | + Name: "telemetryconvcheck", |
| 32 | + Doc: "disallow OpenTelemetry event and attribute names that use hardcoded strings instead of the constants from otelconv", |
| 33 | + Run: func(pass *analysis.Pass) (any, error) { |
| 34 | + // Check for a skipped package |
| 35 | + if len(*skip) > 0 { |
| 36 | + skipped := lo.Map(strings.Split(*skip, ","), func(skipped string, _ int) string { return strings.TrimSpace(skipped) }) |
| 37 | + for _, s := range skipped { |
| 38 | + if strings.Contains(pass.Pkg.Path(), s) { |
| 39 | + return nil, nil |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + inspectorInst := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 45 | + inspectorInst.Preorder([]ast.Node{(*ast.CallExpr)(nil)}, func(node ast.Node) { |
| 46 | + callExpr := node.(*ast.CallExpr) |
| 47 | + functionName := getFunctionName(callExpr) |
| 48 | + if functionName == "" { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + if !(isOtelTracingCall(pass, callExpr) || containsPattern(functionName, otelTracingFunctionPatterns)) { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + checkArguments(pass, callExpr, functionName) |
| 57 | + }) |
| 58 | + |
| 59 | + return nil, nil |
| 60 | + }, |
| 61 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 62 | + Flags: *flagSet, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +var otelTracingFunctionPatterns = []string{ |
| 67 | + // Event methods |
| 68 | + "AddEvent", |
| 69 | + |
| 70 | + // Attribute creation functions |
| 71 | + "attribute.String", |
| 72 | + "attribute.Int", |
| 73 | + "attribute.Int64", |
| 74 | + "attribute.Float64", |
| 75 | + "attribute.Bool", |
| 76 | + "attribute.StringSlice", |
| 77 | + "attribute.IntSlice", |
| 78 | + "attribute.Int64Slice", |
| 79 | + "attribute.Float64Slice", |
| 80 | + "attribute.BoolSlice", |
| 81 | + |
| 82 | + // Attribute setting methods |
| 83 | + "SetAttributes", |
| 84 | + "WithAttributes", |
| 85 | +} |
| 86 | + |
| 87 | +func containsPattern(functionName string, patterns []string) bool { |
| 88 | + for _, pattern := range patterns { |
| 89 | + if strings.Contains(functionName, pattern) { |
| 90 | + return true |
| 91 | + } |
| 92 | + } |
| 93 | + return false |
| 94 | +} |
| 95 | + |
| 96 | +func isOtelTracingCall(pass *analysis.Pass, callExpr *ast.CallExpr) bool { |
| 97 | + selExpr, ok := callExpr.Fun.(*ast.SelectorExpr) |
| 98 | + if !ok { |
| 99 | + return false |
| 100 | + } |
| 101 | + |
| 102 | + typ := pass.TypesInfo.TypeOf(selExpr.X) |
| 103 | + if typ == nil { |
| 104 | + return false |
| 105 | + } |
| 106 | + |
| 107 | + typeName := typ.String() |
| 108 | + return strings.Contains(typeName, "go.opentelemetry.io/otel/trace") || |
| 109 | + strings.Contains(typeName, "trace.Span") || |
| 110 | + strings.Contains(typeName, "attribute.Key") |
| 111 | +} |
| 112 | + |
| 113 | +func getFunctionName(callExpr *ast.CallExpr) string { |
| 114 | + switch fun := callExpr.Fun.(type) { |
| 115 | + case *ast.Ident: |
| 116 | + return fun.Name |
| 117 | + case *ast.SelectorExpr: |
| 118 | + if ident, ok := fun.X.(*ast.Ident); ok { |
| 119 | + return ident.Name + "." + fun.Sel.Name |
| 120 | + } |
| 121 | + return fun.Sel.Name |
| 122 | + default: |
| 123 | + return "" |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func checkArguments(pass *analysis.Pass, callExpr *ast.CallExpr, functionName string) { |
| 128 | + switch { |
| 129 | + case strings.Contains(functionName, "AddEvent"): |
| 130 | + if len(callExpr.Args) > 0 { |
| 131 | + checkStringArgument(pass, callExpr.Args[0], "event name") |
| 132 | + } |
| 133 | + if len(callExpr.Args) > 1 { |
| 134 | + for _, arg := range callExpr.Args[1:] { |
| 135 | + if optCall, ok := arg.(*ast.CallExpr); ok { |
| 136 | + if name := getFunctionName(optCall); name == "WithAttributes" || name == "trace.WithAttributes" { |
| 137 | + checkAttributes(pass, optCall.Args) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + case strings.Contains(functionName, "SetAttributes") || strings.Contains(functionName, "WithAttributes"): |
| 143 | + checkAttributes(pass, callExpr.Args) |
| 144 | + case strings.Contains(functionName, "attribute."): |
| 145 | + if len(callExpr.Args) > 0 { |
| 146 | + checkStringArgument(pass, callExpr.Args[0], "attribute key") |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func checkAttributes(pass *analysis.Pass, args []ast.Expr) { |
| 152 | + for _, arg := range args { |
| 153 | + ast.Inspect(arg, func(node ast.Node) bool { |
| 154 | + callExpr, ok := node.(*ast.CallExpr) |
| 155 | + if !ok { |
| 156 | + return true |
| 157 | + } |
| 158 | + |
| 159 | + functionName := getFunctionName(callExpr) |
| 160 | + if strings.Contains(functionName, "attribute.") && len(callExpr.Args) > 0 { |
| 161 | + checkStringArgument(pass, callExpr.Args[0], "attribute key") |
| 162 | + } |
| 163 | + return true |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func checkStringArgument(pass *analysis.Pass, arg ast.Expr, description string) { |
| 169 | + if lit, ok := arg.(*ast.BasicLit); ok && lit.Kind == token.STRING { |
| 170 | + pass.Reportf(arg.Pos(), "use a constant from github.com/authzed/spicedb/internal/telemetry/otelconv for %s instead of the hardcoded string %s", description, lit.Value) |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + var ident *ast.Ident |
| 175 | + switch n := arg.(type) { |
| 176 | + case *ast.Ident: |
| 177 | + ident = n |
| 178 | + case *ast.SelectorExpr: |
| 179 | + ident = n.Sel |
| 180 | + default: |
| 181 | + return |
| 182 | + } |
| 183 | + |
| 184 | + obj := pass.TypesInfo.ObjectOf(ident) |
| 185 | + if obj == nil { |
| 186 | + return |
| 187 | + } |
| 188 | + |
| 189 | + if _, ok := obj.(*types.Const); !ok { |
| 190 | + return |
| 191 | + } |
| 192 | + |
| 193 | + pkg := obj.Pkg() |
| 194 | + if pkg == nil || pkg.Path() != *convPackage { |
| 195 | + var pkgPath string |
| 196 | + if pkg != nil { |
| 197 | + pkgPath = pkg.Path() |
| 198 | + } else { |
| 199 | + pkgPath = "the current package" |
| 200 | + } |
| 201 | + pass.Reportf( |
| 202 | + arg.Pos(), |
| 203 | + "use a constant from `%s` for %s, not from `%s`", |
| 204 | + *convPackage, |
| 205 | + description, |
| 206 | + pkgPath, |
| 207 | + ) |
| 208 | + } |
| 209 | + |
| 210 | + if constObj, ok := obj.(*types.Const); ok { |
| 211 | + if constObj.Val().Kind() != constant.String { |
| 212 | + pass.Reportf(arg.Pos(), "constant %s from %s should be a string, but has type %s", ident.Name, *convPackage, constObj.Type().String()) |
| 213 | + } |
| 214 | + constValue := constant.StringVal(constObj.Val()) |
| 215 | + if !slices.ContainsFunc(validPrefixes, func(prefix string) bool { |
| 216 | + return strings.HasPrefix(constValue, prefix) |
| 217 | + }) { |
| 218 | + pass.Reportf( |
| 219 | + arg.Pos(), |
| 220 | + "constant %s from %s should start with one of %q, but has value %q", |
| 221 | + ident.Name, |
| 222 | + *convPackage, |
| 223 | + validPrefixes, |
| 224 | + constValue, |
| 225 | + ) |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments