Skip to content

Commit 02fba54

Browse files
authored
Add some checks to avoid runtime errors (#4945)
* Add some checks to avoid runtime errors * check span * linter
1 parent 23720bb commit 02fba54

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

packages/datadog-plugin-avsc/src/schema_iterator.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,15 @@ class SchemaExtractor {
108108
if (!builder.shouldExtractSchema(schemaName, depth)) {
109109
return false
110110
}
111-
for (const field of schema.fields) {
112-
if (!this.extractProperty(field, schemaName, field.name, builder, depth)) {
113-
log.warn('DSM: Unable to extract field with name: %s from Avro schema with name: %s', field.name, schemaName)
111+
if (schema.fields?.[Symbol.iterator]) {
112+
for (const field of schema.fields) {
113+
if (!this.extractProperty(field, schemaName, field.name, builder, depth)) {
114+
log.warn('DSM: Unable to extract field with name: %s from Avro schema with name: %s', field.name,
115+
schemaName)
116+
}
114117
}
118+
} else {
119+
log.warn('DSM: schema.fields is not iterable from Avro schema with name: %s', schemaName)
115120
}
116121
}
117122
return true

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class GrpcClientPlugin extends ClientPlugin {
6262
return parentStore
6363
}
6464

65-
error ({ span, error }) {
65+
error ({ span = this.activeSpan, error }) {
6666
this.addCode(span, error.code)
6767
if (error.code && !this._tracerConfig.grpc.client.error.statuses.includes(error.code)) {
6868
return
@@ -108,7 +108,7 @@ class GrpcClientPlugin extends ClientPlugin {
108108
}
109109

110110
addCode (span, code) {
111-
if (code !== undefined) {
111+
if (code !== undefined && span) {
112112
span.setTag('grpc.status.code', code)
113113
}
114114
}

packages/dd-trace/src/opentracing/propagation/text_map.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ class TextMapPropagator {
499499
}
500500

501501
_extractGenericContext (carrier, traceKey, spanKey, radix) {
502-
if (carrier[traceKey] && carrier[spanKey]) {
502+
if (carrier && carrier[traceKey] && carrier[spanKey]) {
503503
if (invalidSegment.test(carrier[traceKey])) return null
504504

505505
return new DatadogSpanContext({

packages/dd-trace/src/plugins/tracing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class TracingPlugin extends Plugin {
9494
}
9595

9696
addError (error, span = this.activeSpan) {
97-
if (!span._spanContext._tags.error) {
97+
if (span && !span._spanContext._tags.error) {
9898
// Errors may be wrapped in a context.
9999
error = (error && error.error) || error
100100
span.setTag('error', error || 1)

packages/dd-trace/src/profiling/profilers/event_plugins/event.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ class EventPlugin extends TracingPlugin {
2121
}
2222

2323
error () {
24-
this.store.getStore().error = true
24+
const store = this.store.getStore()
25+
if (store) {
26+
store.error = true
27+
}
2528
}
2629

2730
finish () {
28-
const { startEvent, startTime, error } = this.store.getStore()
31+
const store = this.store.getStore()
32+
if (!store) return
33+
34+
const { startEvent, startTime, error } = store
2935
if (error) {
3036
return // don't emit perf events for failed operations
3137
}

0 commit comments

Comments
 (0)