| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"go.opentelemetry.io/otel/attribute" |
| 11 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 12 |
) |
| 13 |
|
| 14 |
func TestParseFullMethod(t *testing.T) { |
| 15 |
cases := []struct { |
| 16 |
input string |
| 17 |
expectedName string |
| 18 |
expectedAttrs []attribute.KeyValue |
| 19 |
}{ |
| 20 |
{ |
| 21 |
input: "no_slash/method", |
| 22 |
expectedName: "no_slash/method", |
| 23 |
}, |
| 24 |
{ |
| 25 |
input: "/slash_but_no_second_slash", |
| 26 |
expectedName: "slash_but_no_second_slash", |
| 27 |
expectedAttrs: []attribute.KeyValue{ |
| 28 |
semconv.RPCMethod("slash_but_no_second_slash"), |
| 29 |
}, |
| 30 |
}, |
| 31 |
{ |
| 32 |
input: "/service_only/", |
| 33 |
expectedName: "service_only/", |
| 34 |
expectedAttrs: []attribute.KeyValue{ |
| 35 |
semconv.RPCMethod("service_only/"), |
| 36 |
}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
input: "//method_only", |
| 40 |
expectedName: "/method_only", |
| 41 |
expectedAttrs: []attribute.KeyValue{ |
| 42 |
semconv.RPCMethod("/method_only"), |
| 43 |
}, |
| 44 |
}, |
| 45 |
{ |
| 46 |
input: "/service/method", |
| 47 |
expectedName: "service/method", |
| 48 |
expectedAttrs: []attribute.KeyValue{ |
| 49 |
semconv.RPCMethod("service/method"), |
| 50 |
}, |
| 51 |
}, |
| 52 |
} |
| 53 |
for _, tc := range cases { |
| 54 |
t.Run(tc.input, func(t *testing.T) { |
| 55 |
name, attrs := ParseFullMethod(tc.input) |
| 56 |
assert.Equal(t, tc.expectedName, name) |
| 57 |
assert.Equal(t, tc.expectedAttrs, attrs) |
| 58 |
}) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
func TestParseFullMethodOld(t *testing.T) { |
| 63 |
cases := []struct { |
| 64 |
input string |
| 65 |
expectedName string |
| 66 |
expectedAttrs []attribute.KeyValue |
| 67 |
}{ |
| 68 |
{ |
| 69 |
input: "no_slash/method", |
| 70 |
expectedName: "no_slash/method", |
| 71 |
}, |
| 72 |
{ |
| 73 |
input: "/slash_but_no_second_slash", |
| 74 |
expectedName: "slash_but_no_second_slash", |
| 75 |
expectedAttrs: []attribute.KeyValue{ |
| 76 |
attribute.String("rpc.system", "grpc"), |
| 77 |
}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
input: "/service_only/", |
| 81 |
expectedName: "service_only/", |
| 82 |
expectedAttrs: []attribute.KeyValue{ |
| 83 |
attribute.String("rpc.system", "grpc"), |
| 84 |
attribute.String("rpc.service", "service_only"), |
| 85 |
attribute.String("rpc.method", ""), |
| 86 |
}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
input: "//method_only", |
| 90 |
expectedName: "/method_only", |
| 91 |
expectedAttrs: []attribute.KeyValue{ |
| 92 |
attribute.String("rpc.system", "grpc"), |
| 93 |
attribute.String("rpc.service", ""), |
| 94 |
attribute.String("rpc.method", "method_only"), |
| 95 |
}, |
| 96 |
}, |
| 97 |
{ |
| 98 |
input: "/service/method", |
| 99 |
expectedName: "service/method", |
| 100 |
expectedAttrs: []attribute.KeyValue{ |
| 101 |
attribute.String("rpc.system", "grpc"), |
| 102 |
attribute.String("rpc.service", "service"), |
| 103 |
attribute.String("rpc.method", "method"), |
| 104 |
}, |
| 105 |
}, |
| 106 |
} |
| 107 |
for _, tc := range cases { |
| 108 |
t.Run(tc.input, func(t *testing.T) { |
| 109 |
name, attrs := ParseFullMethodOld(tc.input) |
| 110 |
assert.Equal(t, tc.expectedName, name) |
| 111 |
assert.Equal(t, tc.expectedAttrs, attrs) |
| 112 |
}) |
| 113 |
} |
| 114 |
} |