Skip to content

Commit 53dfde5

Browse files
authored
Feature/link 2 jaeger embedded (#113)
1 parent b52ac76 commit 53dfde5

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

src/main/kotlin/org/digma/intellij/plugin/ui/list/insights/SpanPanels.kt

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.digma.intellij.plugin.ui.list.insights
22

3+
import com.google.common.io.CharStreams
34
import com.intellij.openapi.fileEditor.impl.HTMLEditorProvider
45
import com.intellij.openapi.project.Project
56
import com.intellij.ui.components.ActionLink
@@ -31,6 +32,7 @@ import org.threeten.extra.AmountFormats
3132
import java.awt.BorderLayout
3233
import java.awt.Dimension
3334
import java.awt.GridLayout
35+
import java.io.InputStreamReader
3436
import java.sql.Timestamp
3537
import java.util.Locale
3638
import java.util.concurrent.TimeUnit
@@ -41,6 +43,21 @@ import javax.swing.SwingConstants
4143
import kotlin.math.abs
4244
import kotlin.math.max
4345

46+
class SpanPanels {
47+
48+
companion object {
49+
val JAEGER_EMBEDDED_HTML_TEMPLATE: String
50+
51+
init {
52+
this::class.java.getResourceAsStream("/templates/Jaeger-embedded-template.html").use {
53+
val loadedContent = CharStreams.toString(InputStreamReader(it))
54+
JAEGER_EMBEDDED_HTML_TEMPLATE = loadedContent
55+
}
56+
}
57+
}
58+
59+
}
60+
4461
fun spanPanel(spanInsight: SpanInsight): JPanel {
4562

4663
val title = JLabel(asHtml(spanBold("Top Usage")), SwingConstants.LEFT)
@@ -176,8 +193,8 @@ fun spanDurationPanel(
176193
}
177194

178195
val buttonToGraph = buildButtonToPercentilesGraph(project, spanDurationsInsight.span)
179-
val settingsState = SettingsState.getInstance(project)
180-
val iconPanel = buildIconPanelWithLinks(settingsState, traceSamples, buttonToGraph)
196+
val buttonToJaeger = buildButtonToJaeger(project, spanDurationsInsight.span, traceSamples)
197+
val iconPanel = buildIconPanelWithLinks(buttonToGraph, buttonToJaeger)
181198

182199
val result = JBPanel<JBPanel<*>>()
183200
result.layout = BorderLayout()
@@ -189,10 +206,9 @@ fun spanDurationPanel(
189206
}
190207

191208
fun buildIconPanelWithLinks(
192-
settingsState: SettingsState, traceSamples: List<TraceSample>, buttonToPercentilesGraph: JButton
209+
buttonToPercentilesGraph: JButton, buttonToJaeger: JButton?
193210
): JBPanel<*> {
194211

195-
val jaegerUrl = buildLinkToJaeger(settingsState, traceSamples)
196212
val iconPanel = panel {
197213
row {
198214
icon(Laf.Icons.Insight.HISTOGRAM)
@@ -201,39 +217,58 @@ fun buildIconPanelWithLinks(
201217
row {
202218
cell(buttonToPercentilesGraph)
203219
}
204-
if (jaegerUrl.isNotBlank()) {
220+
if (buttonToJaeger != null) {
205221
row {
206-
val bl = browserLink("Compare", jaegerUrl)
207-
bl.component.icon = null
208-
bl.component.toolTipText = "Compare with Jaeger"
222+
cell(buttonToJaeger)
209223
}
210224
}
211225
}.andTransparent()
212226
return iconPanel
213227
}
214228

215-
fun buildLinkToJaeger(
216-
settingsState: SettingsState, traceSamples: List<TraceSample>, embedLinks: Boolean = false
217-
): String {
229+
// if cannot create the button then would return null
230+
fun buildButtonToJaeger(
231+
project: Project, spanInfo: SpanInfo, traceSamples: List<TraceSample>
232+
): JButton? {
233+
234+
val settingsState = SettingsState.getInstance(project)
218235

219236
val jaegerBaseUrl = settingsState.jaegerUrl?.trim()?.trimEnd('/')
220237
if (jaegerBaseUrl.isNullOrBlank() || traceSamples.isNullOrEmpty()) {
221-
return ""
238+
return null
222239
}
223240
val filtered = traceSamples.filter { x -> x.hasTraceId() }
224241
if (filtered.isNullOrEmpty()) {
225-
return ""
242+
return null
226243
}
227-
val embedPart = if (embedLinks) "&uiEmbed=v0" else ""
244+
245+
val caption: String
246+
val jaegerUrl: String
247+
val embedPart = "&uiEmbed=v0"
228248

229249
val trace1 = filtered[0].traceId?.lowercase()
230250
if (filtered.size == 1) {
231-
return "${jaegerBaseUrl}/trace/${trace1}?cohort=${trace1}${embedPart}"
251+
caption = "A sample ${filtered[0].traceName} trace"
252+
jaegerUrl = "${jaegerBaseUrl}/trace/${trace1}?cohort=${trace1}${embedPart}"
253+
} else {
254+
// assuming it has (at least) size of 2
255+
val trace2 = filtered[1].traceId?.lowercase()
256+
caption = "Comparing: A sample ${filtered[0].traceName} trace with a ${filtered[1].traceName} trace"
257+
jaegerUrl = "${jaegerBaseUrl}/trace/${trace1}...${trace2}?cohort=${trace1}&cohort=${trace2}${embedPart}"
232258
}
233259

234-
// assuming it has (at least) size of 2
235-
val trace2 = filtered[1].traceId?.lowercase()
236-
return "${jaegerBaseUrl}/trace/${trace1}...${trace2}?cohort=${trace1}&cohort=${trace2}${embedPart}"
260+
val htmlContent = SpanPanels.JAEGER_EMBEDDED_HTML_TEMPLATE
261+
.replace("__JAEGER_EMBEDDED_URL__", jaegerUrl)
262+
.replace("__CAPTION__", caption)
263+
264+
val editorTitle = "Jaeger sample traces of Span ${spanInfo.name}"
265+
266+
val button = ActionLink("Compare")
267+
button.addActionListener {
268+
HTMLEditorProvider.openEditor(project, editorTitle, htmlContent)
269+
}
270+
271+
return button
237272
}
238273

239274
fun buildButtonToPercentilesGraph(project: Project, span: SpanInfo): ActionLink {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html xml:lang="en">
3+
<head>
4+
<title>Trace</title>
5+
</head>
6+
<body>
7+
<h2 style="color:white;">__CAPTION__</h2>
8+
<iframe style="width:100%; height:500px;"
9+
title="Trace"
10+
src="__JAEGER_EMBEDDED_URL__">
11+
</iframe>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)