Skip to content

Commit ddf6db9

Browse files
committed
hack in 233
1 parent c34c51c commit ddf6db9

File tree

2 files changed

+76
-36
lines changed
  • plugins/core/jetbrains-community

2 files changed

+76
-36
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.intellij.platform.diagnostic.telemetry.helpers
5+
6+
import io.opentelemetry.api.common.AttributeKey
7+
import io.opentelemetry.api.common.Attributes
8+
import io.opentelemetry.api.trace.Span
9+
import io.opentelemetry.api.trace.StatusCode
10+
import kotlin.coroutines.cancellation.CancellationException
11+
12+
val EXCEPTION_ESCAPED = AttributeKey.booleanKey("exception.escaped")
13+
14+
inline fun <T> Span.useWithoutActiveScope(operation: (Span) -> T): T {
15+
try {
16+
return operation(this)
17+
}
18+
catch (e: CancellationException) {
19+
recordException(e, Attributes.of(EXCEPTION_ESCAPED, true))
20+
throw e
21+
}
22+
catch (e: Throwable) {
23+
recordException(e, Attributes.of(EXCEPTION_ESCAPED, true))
24+
setStatus(StatusCode.ERROR)
25+
throw e
26+
}
27+
finally {
28+
end()
29+
}
30+
}

plugins/core/jetbrains-community/src/software/aws/toolkits/jetbrains/services/telemetry/otel/OtelBase.kt

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package software.aws.toolkits.jetbrains.services.telemetry.otel
55

6+
import com.intellij.openapi.application.ApplicationInfo
7+
import com.intellij.platform.diagnostic.telemetry.helpers.useWithoutActiveScope
68
import io.opentelemetry.api.common.AttributeKey
79
import io.opentelemetry.api.common.Attributes
810
import io.opentelemetry.api.trace.Span
@@ -31,15 +33,24 @@ class DefaultSpanBuilder(delegate: SpanBuilder) : AbstractSpanBuilder<DefaultSpa
3133
override fun doStartSpan() = BaseSpan(parent, delegate.startSpan())
3234
}
3335

34-
abstract class AbstractSpanBuilder<Builder : AbstractSpanBuilder<Builder, Span>, Span : AbstractBaseSpan>(protected val delegate: SpanBuilder) : SpanBuilder {
36+
abstract class AbstractSpanBuilder<BuilderType : AbstractSpanBuilder<BuilderType, SpanType>, SpanType : AbstractBaseSpan>(protected val delegate: SpanBuilder) : SpanBuilder {
3537
/**
3638
* Same as [com.intellij.platform.diagnostic.telemetry.helpers.use] except downcasts to specific subclass of [BaseSpan]
3739
*
3840
* @inheritdoc
3941
*/
4042
inline fun<T> use(operation: (Span) -> T): T =
41-
startSpan().ijUse { span ->
42-
operation(span as Span)
43+
// FIX_WHEN_MIN_IS_241: not worth fixing for 233
44+
if (ApplicationInfo.getInstance().build.baselineVersion == 233) {
45+
startSpan().useWithoutActiveScope { span ->
46+
span.makeCurrent().use {
47+
operation(span as SpanType)
48+
}
49+
}
50+
} else {
51+
startSpan().ijUse { span ->
52+
operation(span as SpanType)
53+
}
4354
}
4455

4556
/**
@@ -49,89 +60,89 @@ abstract class AbstractSpanBuilder<Builder : AbstractSpanBuilder<Builder, Span>,
4960
*/
5061
suspend inline fun<T> useWithScope(
5162
context: CoroutineContext = EmptyCoroutineContext,
52-
crossinline operation: suspend CoroutineScope.(Span) -> T,
63+
crossinline operation: suspend CoroutineScope.(SpanType) -> T,
5364
): T =
5465
ijUseWithScope(context) { span ->
55-
operation(span as Span)
66+
operation(span as SpanType)
5667
}
5768

5869
protected var parent: Context? = null
59-
override fun setParent(context: Context): Builder {
70+
override fun setParent(context: Context): BuilderType {
6071
parent = context
6172
delegate.setParent(context)
62-
return this as Builder
73+
return this as BuilderType
6374
}
6475

65-
override fun setNoParent(): Builder {
76+
override fun setNoParent(): BuilderType {
6677
parent = null
6778
delegate.setNoParent()
68-
return this as Builder
79+
return this as BuilderType
6980
}
7081

71-
override fun addLink(spanContext: SpanContext): Builder {
82+
override fun addLink(spanContext: SpanContext): BuilderType {
7283
delegate.addLink(spanContext)
73-
return this as Builder
84+
return this as BuilderType
7485
}
7586

7687
override fun addLink(
7788
spanContext: SpanContext,
7889
attributes: Attributes,
79-
): Builder {
90+
): BuilderType {
8091
delegate.addLink(spanContext, attributes)
81-
return this as Builder
92+
return this as BuilderType
8293
}
8394

84-
override fun setAttribute(key: String, value: String): Builder {
95+
override fun setAttribute(key: String, value: String): BuilderType {
8596
delegate.setAttribute(key, value)
86-
return this as Builder
97+
return this as BuilderType
8798
}
8899

89-
override fun setAttribute(key: String, value: Long): Builder {
100+
override fun setAttribute(key: String, value: Long): BuilderType {
90101
delegate.setAttribute(key, value)
91-
return this as Builder
102+
return this as BuilderType
92103
}
93104

94-
override fun setAttribute(key: String, value: Double): Builder {
105+
override fun setAttribute(key: String, value: Double): BuilderType {
95106
delegate.setAttribute(key, value)
96-
return this as Builder
107+
return this as BuilderType
97108
}
98109

99-
override fun setAttribute(key: String, value: Boolean): Builder {
110+
override fun setAttribute(key: String, value: Boolean): BuilderType {
100111
delegate.setAttribute(key, value)
101-
return this as Builder
112+
return this as BuilderType
102113
}
103114

104115
override fun <V : Any?> setAttribute(
105116
key: AttributeKey<V?>,
106117
value: V & Any,
107-
): Builder {
118+
): BuilderType {
108119
delegate.setAttribute(key, value)
109-
return this as Builder
120+
return this as BuilderType
110121
}
111122

112-
override fun setAllAttributes(attributes: Attributes): Builder {
123+
override fun setAllAttributes(attributes: Attributes): BuilderType {
113124
delegate.setAllAttributes(attributes)
114-
return this as Builder
125+
return this as BuilderType
115126
}
116127

117-
override fun setSpanKind(spanKind: SpanKind): Builder {
128+
override fun setSpanKind(spanKind: SpanKind): BuilderType {
118129
delegate.setSpanKind(spanKind)
119-
return this as Builder
130+
return this as BuilderType
120131
}
121132

122-
override fun setStartTimestamp(startTimestamp: Long, unit: TimeUnit): Builder {
133+
override fun setStartTimestamp(startTimestamp: Long, unit: TimeUnit): BuilderType {
123134
delegate.setStartTimestamp(startTimestamp, unit)
124-
return this as Builder
135+
return this as BuilderType
125136
}
126137

127-
override fun setStartTimestamp(startTimestamp: Instant): Builder {
138+
override fun setStartTimestamp(startTimestamp: Instant): BuilderType {
128139
delegate.setStartTimestamp(startTimestamp)
129-
return this as Builder
140+
return this as BuilderType
130141
}
131142

132-
protected abstract fun doStartSpan(): Span
143+
protected abstract fun doStartSpan(): SpanType
133144

134-
override fun startSpan(): Span {
145+
override fun startSpan(): SpanType {
135146
var parent = parent
136147
if (parent == null) {
137148
parent = Context.current()
@@ -140,10 +151,9 @@ abstract class AbstractSpanBuilder<Builder : AbstractSpanBuilder<Builder, Span>,
140151

141152
val contextValue = parent.get(AWS_PRODUCT_CONTEXT_KEY)
142153
if (contextValue == null) {
143-
// FIX_WHEN_MIN_IS_243: Kotlin compiler can't figure out difference between class/type parameter until 2.x
144-
val s = io.opentelemetry.api.trace.Span.fromContextOrNull(parent)
154+
val s = Span.fromContextOrNull(parent)
145155
parent = if (s is AbstractBaseSpan && s.context != null) {
146-
s.context.with(io.opentelemetry.api.trace.Span.fromContext(parent))
156+
s.context.with(Span.fromContext(parent))
147157
} else {
148158
parent.with(AWS_PRODUCT_CONTEXT_KEY, resolvePluginName())
149159
}

0 commit comments

Comments
 (0)