add_parent_span_to_mv.sql 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. alter table otel.otel_traces_url_local on cluster default
  2. add column ParentSpanId String default '';
  3. alter table otel.otel_traces_url on cluster default
  4. add column ParentSpanId String default '';
  5. drop table if exists otel_traces_url_local_mv on cluster default sync;
  6. CREATE MATERIALIZED VIEW if not exists otel.otel_traces_url_local_mv
  7. on cluster default
  8. TO otel.otel_traces_url_local
  9. (
  10. `Timestamp` DateTime64(9),
  11. `TraceId` String,
  12. `SpanId` String,
  13. `ParentSpanId` String,
  14. `Route` String,
  15. `RouteRaw` String,
  16. `Path` String,
  17. `Query` String,
  18. `Target` String,
  19. `ProtocolName` String,
  20. `ProtocolVersion` String,
  21. `Method` String,
  22. `StatusCode` Int64,
  23. `Message` String,
  24. `UserAgent` String,
  25. `Duration` Int64,
  26. `ServiceName` LowCardinality(String),
  27. `AppAlias` String
  28. )
  29. AS
  30. WITH
  31. '\\b[a-fA-F0-9]{32,}\\b|\\b[a-fA-F0-9\\-]{36}\\b|\\b\\[1-9]\\d*\\b' AS varPattern, SpanAttributes['http.route'] AS route, lower(splitByString('.', splitByString('/', Path)[-1])[-1]) AS fileExt, multiIf(
  32. (SpanAttributes['url.path']) != '', SpanAttributes['url.path'], (SpanAttributes['http.target']) != '',
  33. path(SpanAttributes['http.target']), (SpanAttributes['http.url']) != '', path(SpanAttributes['http.url']),
  34. (SpanAttributes['u\nrl.full']) != '', path(SpanAttributes['url.full']), SpanAttributes['http.route']) AS path
  35. SELECT Timestamp,
  36. TraceId,
  37. SpanId,
  38. ParentSpanId,
  39. if(route != '', route, replaceRegexpOne(Path, varPattern, '{:var}')) AS Route,
  40. route AS RouteRaw,
  41. path AS Path,
  42. if((SpanAttributes['url.query']) != '', SpanAttributes['url.query'],
  43. queryString(SpanAttributes['http.target'])) AS Query,
  44. if((SpanAttributes['http.target']) != '', SpanAttributes['http.target'],
  45. if((SpanAttributes['url.path']) != '', SpanAttributes['url.path'], SpanAttributes['http.route'])) AS Target,
  46. multiIf((SpanAttributes['http.scheme']) != '', SpanAttributes['http.scheme'],
  47. (SpanAttributes['url.scheme']) != '', SpanAttributes['url.scheme'],
  48. SpanAttributes['network.protocol.name']) AS ProtocolName,
  49. if((SpanAttributes['http.flavor']) != '', SpanAttributes['http.flavor'],
  50. SpanAttributes['network.protocol.version']) AS ProtocolVersion,
  51. if((SpanAttributes['http.method']) != '', SpanAttributes['http.method'],
  52. SpanAttributes['http.request.method']) AS Method,
  53. toInt64OrZero(if((SpanAttributes['http.status_code']) != '', SpanAttributes['http.status_code'],
  54. SpanAttributes['http.response.status_code'])) AS StatusCode,
  55. if(StatusMessage != '', StatusMessage, SpanAttributes['error.type']) AS Message,
  56. if((SpanAttributes['http.user_agent']) != '', SpanAttributes['http.user_agent'],
  57. SpanAttributes['user_agent.original']) AS UserAgent,
  58. Duration,
  59. ServiceName,
  60. AppAlias
  61. FROM otel.otel_traces_local
  62. WHERE (SpanKindNumber = 2)
  63. AND (Path != '')
  64. AND (((SpanAttributes['http.method']) != '') OR ((SpanAttributes['http.request.method']) != ''))
  65. AND (fileExt != Path)
  66. AND (fileExt NOT IN
  67. ('css', 'gif', 'ico', 'jpg', 'jpeg', 'js', 'json', 'mp3', 'mp4', 'png', 'svg', 'ttf', 'txt', 'wasm', 'woff'));
  68. truncate table otel.otel_traces_url_local on cluster default;