Skip to content

Commit 61f018c

Browse files
authored
Cleaned up Tags (#145)
* Each Tag is now its own individual class, which allows 'overridable' to be property of the Tag and not specified at tag instantiate. * Should be more performant. * Key changes `url` -> `http.url` and `status.code` -> `http.status.code` to match Node agent.
1 parent 3323770 commit 61f018c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+296
-269
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ with context.new_entry_span(op='https://github.com/apache') as span:
8484
span.component = Component.Flask
8585
# the span automatically stops when exiting the `with` context
8686

87+
class TagSinger(Tag):
88+
key = 'Singer'
89+
8790
with context.new_exit_span(op='https://github.com/apache', peer='localhost:8080', component=Component.Flask) as span:
88-
span.tag(Tag(key='Singer', val='Nakajima'))
91+
span.tag(TagSinger('Nakajima'))
8992

9093
with context.new_local_span(op='https://github.com/apache') as span:
91-
span.tag(Tag(key='Singer', val='Nakajima'))
94+
span.tag(TagSinger('Nakajima'))
9295
```
9396

9497
### Decorators

skywalking/agent/protocol/grpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ def generator():
120120
data=[KeyStringValuePair(key=item.key, value=item.val) for item in log.items],
121121
) for log in span.logs],
122122
tags=[KeyStringValuePair(
123-
key=str(tag.key),
123+
key=tag.key,
124124
value=str(tag.val),
125-
) for tag in span.tags],
125+
) for tag in span.iter_tags()],
126126
refs=[SegmentReference(
127127
refType=0 if ref.ref_type == "CrossProcess" else 1,
128128
traceId=ref.trace_id,

skywalking/agent/protocol/kafka.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def generator():
7878
data=[KeyStringValuePair(key=item.key, value=item.val) for item in log.items],
7979
) for log in span.logs],
8080
tags=[KeyStringValuePair(
81-
key=str(tag.key),
81+
key=tag.key,
8282
value=str(tag.val),
83-
) for tag in span.tags],
83+
) for tag in span.iter_tags()],
8484
refs=[SegmentReference(
8585
refType=0 if ref.ref_type == "CrossProcess" else 1,
8686
traceId=ref.trace_id,

skywalking/client/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def report(self, generator):
9595
'tags': [{
9696
'key': tag.key,
9797
'value': tag.val,
98-
} for tag in span.tags],
98+
} for tag in span.iter_tags()],
9999
'refs': [{
100100
'refType': 0,
101101
'traceId': ref.trace_id,

skywalking/decorators.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ async def wrapper(*args, **kwargs):
4040
span = context.new_local_span(op=_op)
4141
span.layer = layer
4242
span.component = component
43-
[span.tag(tag) for tag in tags or []]
43+
if tags:
44+
for tag in tags:
45+
span.tag(tag)
4446
with span:
4547
return await func(*args, **kwargs)
4648
return wrapper
@@ -52,7 +54,9 @@ def wrapper(*args, **kwargs):
5254
span = context.new_local_span(op=_op)
5355
span.layer = layer
5456
span.component = component
55-
[span.tag(tag) for tag in tags or []]
57+
if tags:
58+
for tag in tags:
59+
span.tag(tag)
5660
with span:
5761
return func(*args, **kwargs)
5862
return wrapper
@@ -77,7 +81,9 @@ def wrapper(*args, **kwargs):
7781
context.continued(snapshot)
7882
span.layer = layer
7983
span.component = component
80-
[span.tag(tag) for tag in tags or []]
84+
if tags:
85+
for tag in tags:
86+
span.tag(tag)
8187
func(*args, **kwargs)
8288

8389
return wrapper

skywalking/plugins/sw_aiohttp.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
#
1717

1818
from skywalking import Layer, Component, config
19-
from skywalking.trace import tags
2019
from skywalking.trace.carrier import Carrier
2120
from skywalking.trace.context import get_context, NoopContext
2221
from skywalking.trace.span import NoopSpan
23-
from skywalking.trace.tags import Tag
22+
from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpStatusCode
2423

2524

2625
def install():
@@ -38,8 +37,8 @@ async def _sw_request(self: ClientSession, method: str, str_or_url, **kwargs):
3837

3938
with span:
4039
span.layer = Layer.Http
41-
span.tag(Tag(key=tags.HttpMethod, val=method.upper())) # pyre-ignore
42-
span.tag(Tag(key=tags.HttpUrl, val=url)) # pyre-ignore
40+
span.tag(TagHttpMethod(method.upper())) # pyre-ignore
41+
span.tag(TagHttpURL(url)) # pyre-ignore
4342

4443
carrier = span.inject()
4544
headers = kwargs.get('headers')
@@ -54,7 +53,7 @@ async def _sw_request(self: ClientSession, method: str, str_or_url, **kwargs):
5453

5554
res = await _request(self, method, str_or_url, **kwargs)
5655

57-
span.tag(Tag(key=tags.HttpStatus, val=res.status, overridable=True))
56+
span.tag(TagHttpStatusCode(res.status))
5857

5958
if res.status >= 400:
6059
span.error_occurred = True
@@ -83,12 +82,12 @@ async def _sw_handle_request(self, request, start_time: float):
8382
span.peer = '%s:%d' % request._transport_peername if isinstance(request._transport_peername, (list, tuple))\
8483
else request._transport_peername
8584

86-
span.tag(Tag(key=tags.HttpMethod, val=method)) # pyre-ignore
87-
span.tag(Tag(key=tags.HttpUrl, val=str(request.url))) # pyre-ignore
85+
span.tag(TagHttpMethod(method)) # pyre-ignore
86+
span.tag(TagHttpURL(str(request.url))) # pyre-ignore
8887

8988
resp, reset = await _handle_request(self, request, start_time)
9089

91-
span.tag(Tag(key=tags.HttpStatus, val=resp.status, overridable=True))
90+
span.tag(TagHttpStatusCode(resp.status))
9291

9392
if resp.status >= 400:
9493
span.error_occurred = True

skywalking/plugins/sw_celery.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
#
1717

1818
from skywalking import Layer, Component, config
19-
from skywalking.trace import tags
2019
from skywalking.trace.carrier import Carrier
2120
from skywalking.trace.context import get_context
22-
from skywalking.trace.tags import Tag
21+
from skywalking.trace.tags import TagMqBroker, TagCeleryParameters
2322

2423

2524
def install():
@@ -45,13 +44,13 @@ def send_task(self, name, args=None, kwargs=None, **options):
4544
with get_context().new_exit_span(op=op, peer=peer, component=Component.Celery) as span:
4645
span.layer = Layer.MQ
4746

48-
span.tag(Tag(key=tags.MqBroker, val=broker_url))
49-
# span.tag(Tag(key=tags.MqTopic, val=exchange))
50-
# span.tag(Tag(key=tags.MqQueue, val=queue))
47+
span.tag(TagMqBroker(broker_url))
48+
# span.tag(TagMqTopic(exchange))
49+
# span.tag(TagMqQueue(queue))
5150

5251
if config.celery_parameters_length:
5352
params = '*{}, **{}'.format(args, kwargs)[:config.celery_parameters_length]
54-
span.tag(Tag(key=tags.CeleryParameters, val=params))
53+
span.tag(TagCeleryParameters(params))
5554

5655
options = {**options}
5756
headers = options.get('headers')
@@ -95,13 +94,13 @@ def fun(*args, **kwargs):
9594
span.layer = Layer.MQ
9695
span.component = Component.Celery
9796

98-
span.tag(Tag(key=tags.MqBroker, val=task.app.conf['broker_url']))
99-
# span.tag(Tag(key=tags.MqTopic, val=exchange))
100-
# span.tag(Tag(key=tags.MqQueue, val=queue))
97+
span.tag(TagMqBroker(task.app.conf['broker_url']))
98+
# span.tag(TagMqTopic(exchange))
99+
# span.tag(TagMqQueue(queue))
101100

102101
if config.celery_parameters_length:
103102
params = '*{}, **{}'.format(args, kwargs)[:config.celery_parameters_length]
104-
span.tag(Tag(key=tags.CeleryParameters, val=params))
103+
span.tag(TagCeleryParameters(params))
105104

106105
return _fun(*args, **kwargs)
107106

skywalking/plugins/sw_django.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
#
1717

1818
from skywalking import Layer, Component, config
19-
from skywalking.trace import tags
2019
from skywalking.trace.carrier import Carrier
2120
from skywalking.trace.context import get_context, NoopContext
2221
from skywalking.trace.span import NoopSpan
23-
from skywalking.trace.tags import Tag
22+
from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpStatusCode, TagHttpParams
2423

2524
version_rule = {
2625
"name": "django",
@@ -59,16 +58,15 @@ def _sw_get_response(this, request):
5958
span.component = Component.Django
6059
span.peer = '%s:%s' % (request.META.get('REMOTE_ADDR'), request.META.get('REMOTE_PORT') or "80")
6160

62-
span.tag(Tag(key=tags.HttpMethod, val=method))
63-
span.tag(Tag(key=tags.HttpUrl, val=request.build_absolute_uri().split("?")[0]))
61+
span.tag(TagHttpMethod(method))
62+
span.tag(TagHttpURL(request.build_absolute_uri().split("?")[0]))
6463

6564
# you can get request parameters by `request.GET` even though client are using POST or other methods
6665
if config.django_collect_http_params and request.GET:
67-
span.tag(Tag(key=tags.HttpParams,
68-
val=params_tostring(request.GET)[0:config.http_params_length_threshold]))
66+
span.tag(TagHttpParams(params_tostring(request.GET)[0:config.http_params_length_threshold]))
6967

7068
resp = _get_response(this, request)
71-
span.tag(Tag(key=tags.HttpStatus, val=resp.status_code, overridable=True))
69+
span.tag(TagHttpStatusCode(resp.status_code))
7270
if resp.status_code >= 400:
7371
span.error_occurred = True
7472
return resp

skywalking/plugins/sw_elasticsearch.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
#
1717

1818
from skywalking import Layer, Component, config
19-
from skywalking.trace import tags
2019
from skywalking.trace.context import get_context
21-
from skywalking.trace.tags import Tag
20+
from skywalking.trace.tags import TagDbType, TagDbStatement
2221

2322

2423
def install():
@@ -33,9 +32,9 @@ def _sw_perform_request(this: Transport, method, url, headers=None, params=None,
3332
span.layer = Layer.Database
3433
res = _perform_request(this, method, url, headers=headers, params=params, body=body)
3534

36-
span.tag(Tag(key=tags.DbType, val="Elasticsearch"))
35+
span.tag(TagDbType("Elasticsearch"))
3736
if config.elasticsearch_trace_dsl:
38-
span.tag(Tag(key=tags.DbStatement, val="" if body is None else body))
37+
span.tag(TagDbStatement("" if body is None else body))
3938

4039
return res
4140

skywalking/plugins/sw_flask.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
#
1717

1818
from skywalking import Layer, Component, config
19-
from skywalking.trace import tags
2019
from skywalking.trace.carrier import Carrier
2120
from skywalking.trace.context import get_context, NoopContext
2221
from skywalking.trace.span import NoopSpan
23-
from skywalking.trace.tags import Tag
22+
from skywalking.trace.tags import TagHttpMethod, TagHttpURL, TagHttpStatusCode, TagHttpParams
2423

2524

2625
def install():
@@ -50,17 +49,16 @@ def _sw_full_dispatch_request(this: Flask):
5049
span.layer = Layer.Http
5150
span.component = Component.Flask
5251
span.peer = '%s:%s' % (req.environ["REMOTE_ADDR"], req.environ["REMOTE_PORT"])
53-
span.tag(Tag(key=tags.HttpMethod, val=method))
54-
span.tag(Tag(key=tags.HttpUrl, val=req.url.split("?")[0]))
52+
span.tag(TagHttpMethod(method))
53+
span.tag(TagHttpURL(req.url.split("?")[0]))
5554
if config.flask_collect_http_params and req.values:
56-
span.tag(Tag(key=tags.HttpParams,
57-
val=params_tostring(req.values)[0:config.http_params_length_threshold]))
55+
span.tag(TagHttpParams(params_tostring(req.values)[0:config.http_params_length_threshold]))
5856
resp = _full_dispatch_request(this)
5957

6058
if resp.status_code >= 400:
6159
span.error_occurred = True
6260

63-
span.tag(Tag(key=tags.HttpStatus, val=resp.status_code, overridable=True))
61+
span.tag(TagHttpStatusCode(resp.status_code))
6462
return resp
6563

6664
def _sw_handle_user_exception(this: Flask, e):

0 commit comments

Comments
 (0)