Skip to content

Commit 85b9512

Browse files
author
Jordi Bertran de Balanda
committed
move http2 client to clientPlugin
1 parent a0b72b4 commit 85b9512

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

packages/datadog-plugin-http2/src/client.js

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { storage } = require('../../datadog-core')
4-
const Plugin = require('../../dd-trace/src/plugins/plugin')
4+
const ClientPlugin = require('../../dd-trace/src/plugins/client')
55

66
const URL = require('url').URL
77
const log = require('../../dd-trace/src/log')
@@ -24,55 +24,14 @@ const HTTP2_HEADER_PATH = ':path'
2424
const HTTP2_HEADER_STATUS = ':status'
2525
const HTTP2_METHOD_GET = 'GET'
2626

27-
class Http2ClientPlugin extends Plugin {
27+
class Http2ClientPlugin extends ClientPlugin {
2828
static get id () {
2929
return 'http2'
3030
}
3131

3232
constructor (...args) {
3333
super(...args)
3434

35-
this.addSub('apm:http2:client:request:start', ({ authority, options, headers = {} }) => {
36-
const sessionDetails = extractSessionDetails(authority, options)
37-
const path = headers[HTTP2_HEADER_PATH] || '/'
38-
const pathname = path.split(/[?#]/)[0]
39-
const method = headers[HTTP2_HEADER_METHOD] || HTTP2_METHOD_GET
40-
const uri = `${sessionDetails.protocol}//${sessionDetails.host}:${sessionDetails.port}${pathname}`
41-
const allowed = this.config.filter(uri)
42-
43-
const store = storage.getStore()
44-
const childOf = store && allowed ? store.span : null
45-
const span = this.tracer.startSpan('http.request', {
46-
childOf,
47-
tags: {
48-
[COMPONENT]: this.constructor.id,
49-
[CLIENT_PORT_KEY]: parseInt(sessionDetails.port),
50-
[SPAN_KIND]: CLIENT,
51-
'service.name': getServiceName(this.tracer, this.config, sessionDetails),
52-
'resource.name': method,
53-
'span.type': 'http',
54-
'http.method': method,
55-
'http.url': uri,
56-
'out.host': sessionDetails.host
57-
}
58-
})
59-
60-
// TODO: Figure out a better way to do this for any span.
61-
if (!allowed) {
62-
span._spanContext._trace.record = false
63-
}
64-
65-
addHeaderTags(span, headers, HTTP_REQUEST_HEADERS, this.config)
66-
67-
if (!hasAmazonSignature(headers, path)) {
68-
this.tracer.inject(span, HTTP_HEADERS, headers)
69-
}
70-
71-
analyticsSampler.sample(span, this.config.measured)
72-
73-
this.enter(span, store)
74-
})
75-
7635
this.addSub('apm:http2:client:response', (headers) => {
7736
const span = storage.getStore().span
7837
const status = headers && headers[HTTP2_HEADER_STATUS]
@@ -85,14 +44,58 @@ class Http2ClientPlugin extends Plugin {
8544

8645
addHeaderTags(span, headers, HTTP_RESPONSE_HEADERS, this.config)
8746
})
47+
}
8848

89-
this.addSub('apm:http2:client:request:finish', () => {
90-
const span = storage.getStore().span
49+
addTraceSub (eventName, handler) {
50+
this.addSub(`apm:${this.constructor.id}:client:${this.operation}:${eventName}`, handler)
51+
}
9152

92-
span.finish()
53+
start ({ authority, options, headers = {} }) {
54+
const sessionDetails = extractSessionDetails(authority, options)
55+
const path = headers[HTTP2_HEADER_PATH] || '/'
56+
const pathname = path.split(/[?#]/)[0]
57+
const method = headers[HTTP2_HEADER_METHOD] || HTTP2_METHOD_GET
58+
const uri = `${sessionDetails.protocol}//${sessionDetails.host}:${sessionDetails.port}${pathname}`
59+
const allowed = this.config.filter(uri)
60+
61+
const store = storage.getStore()
62+
const childOf = store && allowed ? store.span : null
63+
const span = this.startSpan('http.request', {
64+
childOf,
65+
meta: {
66+
[COMPONENT]: this.constructor.id,
67+
[SPAN_KIND]: CLIENT,
68+
'service.name': getServiceName(this.tracer, this.config, sessionDetails),
69+
'resource.name': method,
70+
'span.type': 'http',
71+
'http.method': method,
72+
'http.url': uri,
73+
'out.host': sessionDetails.host
74+
},
75+
metrics: {
76+
[CLIENT_PORT_KEY]: parseInt(sessionDetails.port)
77+
}
9378
})
9479

95-
this.addSub('apm:http2:client:request:error', this.addError)
80+
// TODO: Figure out a better way to do this for any span.
81+
if (!allowed) {
82+
span._spanContext._trace.record = false
83+
}
84+
85+
addHeaderTags(span, headers, HTTP_REQUEST_HEADERS, this.config)
86+
87+
if (!hasAmazonSignature(headers, path)) {
88+
this.tracer.inject(span, HTTP_HEADERS, headers)
89+
}
90+
91+
analyticsSampler.sample(span, this.config.measured)
92+
93+
this.enter(span, store)
94+
}
95+
96+
finish () {
97+
const span = storage.getStore().span
98+
span.finish()
9699
}
97100

98101
configure (config) {

0 commit comments

Comments
 (0)