use std::collections::BTreeMap;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use serde_json::{json, Map, Value};
use crate::api::{Api, GraphQLResponse, SkyWalkingCapabilities, TimeInfo};
use crate::core::{Error, Result};
use crate::duration::{endpoint_id, instance_id, service_id, TimeRange};
use crate::native::{
decode_topology, i64_of, key_values, native_trace_from_spans, objects, str_of, strings,
KeyValue, NativeTrace, Topology, NATIVE_SPAN_FIELDS,
};
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SkyWalkingService {
pub id: String,
pub name: String,
pub group: String,
pub short_name: String,
pub layers: Vec<String>,
pub normal: bool,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct SkyWalkingEndpoint {
pub id: String,
pub name: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct InstanceAttribute {
pub name: String,
pub value: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SkyWalkingInstance {
pub id: String,
pub name: String,
pub language: String,
#[serde(rename = "instanceUUID")]
pub instance_uuid: String,
pub attributes: Vec<InstanceAttribute>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Paging {
pub page_num: usize,
pub page_size: usize,
}
impl Default for Paging {
fn default() -> Self {
Paging {
page_num: 1,
page_size: 20,
}
}
}
impl Paging {
pub fn new(page_num: usize, page_size: usize) -> Self {
Paging {
page_num,
page_size,
}
}
fn to_json(self, operation: &str) -> Result<Value> {
if self.page_num < 1 {
return Err(Error::validation(format!(
"{operation}: pageNum must be a positive integer"
)));
}
if self.page_size < 1 || self.page_size > 10_000 {
return Err(Error::validation(format!(
"{operation}: pageSize must be 1..10000"
)));
}
Ok(json!({ "pageNum": self.page_num, "pageSize": self.page_size }))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum TraceState {
#[default]
All,
Success,
Error,
}
impl TraceState {
pub fn as_str(self) -> &'static str {
match self {
TraceState::All => "ALL",
TraceState::Success => "SUCCESS",
TraceState::Error => "ERROR",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum TraceOrder {
#[default]
ByStartTime,
ByDuration,
}
impl TraceOrder {
pub fn as_str(self) -> &'static str {
match self {
TraceOrder::ByStartTime => "BY_START_TIME",
TraceOrder::ByDuration => "BY_DURATION",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum LogOrder {
#[default]
Des,
Asc,
}
impl LogOrder {
pub fn as_str(self) -> &'static str {
match self {
LogOrder::Des => "DES",
LogOrder::Asc => "ASC",
}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct QueryTracesOptions {
pub service: Option<String>,
pub instance: Option<String>,
pub endpoint: Option<String>,
pub trace_id: Option<String>,
pub trace_state: TraceState,
pub min_duration_ms: Option<u64>,
pub max_duration_ms: Option<u64>,
pub tags: BTreeMap<String, String>,
pub range: Option<TimeRange>,
pub paging: Paging,
pub order: TraceOrder,
}
impl QueryTracesOptions {
pub fn service(service: impl Into<String>) -> Self {
QueryTracesOptions {
service: Some(service.into()),
..Default::default()
}
}
pub fn tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.tags.insert(key.into(), value.into());
self
}
pub fn range(mut self, range: TimeRange) -> Self {
self.range = Some(range);
self
}
pub fn trace_state(mut self, state: TraceState) -> Self {
self.trace_state = state;
self
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryTracesResult {
pub traces: Vec<NativeTrace>,
pub retrieved_time_range: Option<(i64, i64)>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NativeLog {
pub service_name: String,
pub service_id: String,
pub service_instance_name: String,
pub endpoint_name: String,
pub trace_id: String,
pub timestamp: i64,
pub content_type: String,
pub content: String,
pub tags: Vec<KeyValue>,
}
impl NativeLog {
pub fn tag(&self, key: &str) -> Option<&str> {
self.tags
.iter()
.find(|t| t.key == key)
.map(|t| t.value.as_str())
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct QueryLogsOptions {
pub service: Option<String>,
pub instance: Option<String>,
pub endpoint: Option<String>,
pub trace_id: Option<String>,
pub segment_id: Option<String>,
pub span_id: Option<i64>,
pub keywords: Vec<String>,
pub excluding_keywords: Vec<String>,
pub tags: BTreeMap<String, String>,
pub range: Option<TimeRange>,
pub paging: Paging,
pub order: LogOrder,
}
impl QueryLogsOptions {
pub fn service(service: impl Into<String>) -> Self {
QueryLogsOptions {
service: Some(service.into()),
..Default::default()
}
}
pub fn trace_id(trace_id: impl Into<String>) -> Self {
QueryLogsOptions {
trace_id: Some(trace_id.into()),
..Default::default()
}
}
pub fn tag(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.tags.insert(key.into(), value.into());
self
}
pub fn range(mut self, range: TimeRange) -> Self {
self.range = Some(range);
self
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryLogsResult {
pub logs: Vec<NativeLog>,
pub error_reason: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TopologyOptions {
pub service: Option<String>,
pub layer: Option<String>,
}
const SERVICE_FIELDS: &str = "id name group shortName layers normal";
const LOG_FIELDS: &str = "serviceName serviceId serviceInstanceName endpointName traceId timestamp contentType content tags{key value}";
const TOPOLOGY_FIELDS: &str = "nodes{id name type isReal layers} calls{id source target detectPoints sourceComponents targetComponents}";
fn decode_service(s: &Value) -> SkyWalkingService {
SkyWalkingService {
id: str_of(s, "id"),
name: str_of(s, "name"),
group: str_of(s, "group"),
short_name: str_of(s, "shortName"),
layers: strings(s.get("layers")),
normal: s.get("normal") != Some(&Value::Bool(false)),
}
}
pub fn decode_native_log(l: &Value) -> NativeLog {
NativeLog {
service_name: str_of(l, "serviceName"),
service_id: str_of(l, "serviceId"),
service_instance_name: str_of(l, "serviceInstanceName"),
endpoint_name: str_of(l, "endpointName"),
trace_id: str_of(l, "traceId"),
timestamp: i64_of(l, "timestamp", 0),
content_type: str_of(l, "contentType"),
content: str_of(l, "content"),
tags: key_values(l.get("tags")),
}
}
fn required<'a>(value: &'a str, what: &str, operation: &str) -> Result<&'a str> {
if value.trim().is_empty() {
return Err(Error::validation(format!(
"{operation}: {what} is required"
)));
}
Ok(value)
}
fn tag_list(tags: &BTreeMap<String, String>) -> Option<Value> {
(!tags.is_empty()).then(|| {
Value::Array(
tags.iter()
.map(|(k, v)| json!({ "key": k, "value": v }))
.collect(),
)
})
}
fn scoped_ids(
service: Option<&str>,
instance: Option<&str>,
endpoint: Option<&str>,
operation: &str,
out: &mut Map<String, Value>,
) -> Result<()> {
let Some(service) = service else {
if instance.is_some() || endpoint.is_some() {
return Err(Error::validation(format!(
"{operation}: instance and endpoint need service"
)));
}
return Ok(());
};
out.insert(
"serviceId".into(),
service_id(required(service, "service", operation)?).into(),
);
if let Some(i) = instance {
out.insert("serviceInstanceId".into(), instance_id(service, i).into());
}
if let Some(e) = endpoint {
out.insert("endpointId".into(), endpoint_id(service, e).into());
}
Ok(())
}
fn contains_all(text: &str, words: &[String]) -> bool {
let lower = text.to_lowercase();
words.iter().all(|w| lower.contains(&w.to_lowercase()))
}
fn contains_none(text: &str, words: &[String]) -> bool {
let lower = text.to_lowercase();
words.iter().all(|w| !lower.contains(&w.to_lowercase()))
}
#[derive(Debug, Clone)]
pub struct SkyWalkingQueryClient {
api: Arc<Api>,
}
impl SkyWalkingQueryClient {
pub(crate) fn new(api: Arc<Api>) -> Self {
SkyWalkingQueryClient { api }
}
pub async fn graphql(&self, query: &str, variables: Value) -> Result<GraphQLResponse> {
self.api.graphql(query, variables, "graphql").await
}
pub async fn time_info(&self) -> Result<TimeInfo> {
self.api.time_info("query.timeInfo").await
}
pub async fn capabilities(&self) -> Result<SkyWalkingCapabilities> {
self.api.capabilities("query.capabilities").await
}
pub async fn list_services(
&self,
layer: Option<&str>,
range: Option<TimeRange>,
) -> Result<Vec<SkyWalkingService>> {
let _ = range;
let rows = self
.api
.field(
&format!("query($l:String){{listServices(layer:$l){{{SERVICE_FIELDS}}}}}"),
json!({ "l": layer }),
"listServices",
"query.listServices",
)
.await?;
Ok(objects(Some(&rows)).map(decode_service).collect())
}
pub async fn find_service(&self, name: &str) -> Result<Option<SkyWalkingService>> {
let operation = "query.findService";
let row = self
.api
.field(
&format!("query($n:String!){{findService(serviceName:$n){{{SERVICE_FIELDS}}}}}"),
json!({ "n": required(name, "name", operation)? }),
"findService",
operation,
)
.await?;
Ok(row.is_object().then(|| decode_service(&row)))
}
pub async fn list_endpoints(
&self,
service: &str,
keyword: Option<&str>,
limit: Option<usize>,
) -> Result<Vec<SkyWalkingEndpoint>> {
let operation = "query.listEndpoints";
let limit = limit.unwrap_or(100);
if limit < 1 {
return Err(Error::validation(format!(
"{operation}: limit must be a positive integer"
)));
}
let rows = self
.api
.field(
"query($k:String,$s:ID!,$l:Int!){findEndpoint(keyword:$k,serviceId:$s,limit:$l){id name}}",
json!({
"k": keyword.filter(|k| !k.is_empty()),
"s": service_id(required(service, "service", operation)?),
"l": limit,
}),
"findEndpoint",
operation,
)
.await?;
Ok(objects(Some(&rows))
.map(|e| SkyWalkingEndpoint {
id: str_of(e, "id"),
name: str_of(e, "name"),
})
.collect())
}
pub async fn list_instances(
&self,
service: &str,
range: Option<TimeRange>,
) -> Result<Vec<SkyWalkingInstance>> {
let operation = "query.listInstances";
let id = service_id(required(service, "service", operation)?);
let duration = self
.api
.duration(&range.unwrap_or_default(), operation)
.await?;
let rows = self
.api
.field(
"query($s:ID!,$d:Duration!){listInstances(serviceId:$s,duration:$d){id name language instanceUUID attributes{name value}}}",
json!({ "s": id, "d": duration }),
"listInstances",
operation,
)
.await?;
Ok(objects(Some(&rows))
.map(|i| SkyWalkingInstance {
id: str_of(i, "id"),
name: str_of(i, "name"),
language: str_of(i, "language"),
instance_uuid: str_of(i, "instanceUUID"),
attributes: objects(i.get("attributes"))
.map(|a| InstanceAttribute {
name: str_of(a, "name"),
value: str_of(a, "value"),
})
.collect(),
})
.collect())
}
pub async fn query_traces(&self, opts: &QueryTracesOptions) -> Result<QueryTracesResult> {
let operation = "query.queryTraces";
let mut c = Map::new();
scoped_ids(
opts.service.as_deref(),
opts.instance.as_deref(),
opts.endpoint.as_deref(),
operation,
&mut c,
)?;
c.insert("traceState".into(), opts.trace_state.as_str().into());
c.insert("queryOrder".into(), opts.order.as_str().into());
c.insert("paging".into(), opts.paging.to_json(operation)?);
if let Some(v) = opts.min_duration_ms {
c.insert("minTraceDuration".into(), v.into());
}
if let Some(v) = opts.max_duration_ms {
c.insert("maxTraceDuration".into(), v.into());
}
if let Some(id) = &opts.trace_id {
c.insert("traceId".into(), required(id, "traceId", operation)?.into());
}
if let Some(tags) = tag_list(&opts.tags) {
c.insert("tags".into(), tags);
}
if opts.trace_id.is_none() || opts.range.is_some() {
let d = self
.api
.duration(&opts.range.unwrap_or_default(), operation)
.await?;
c.insert("queryDuration".into(), serde_json::to_value(d)?);
}
let res = self
.api
.field(
&format!("query($c:TraceQueryCondition){{queryTraces(condition:$c){{traces{{spans{{{NATIVE_SPAN_FIELDS}}}}} retrievedTimeRange{{startTime endTime}}}}}}"),
json!({ "c": c }),
"queryTraces",
operation,
)
.await?;
let traces = objects(res.get("traces"))
.filter_map(|t| native_trace_from_spans(t.get("spans")))
.collect();
let retrieved_time_range = res
.get("retrievedTimeRange")
.and_then(|r| Some((r.get("startTime")?.as_i64()?, r.get("endTime")?.as_i64()?)));
Ok(QueryTracesResult {
traces,
retrieved_time_range,
})
}
pub async fn get_trace(&self, trace_id: &str) -> Result<Option<NativeTrace>> {
let operation = "query.getTrace";
let res = self
.api
.field(
&format!(
"query($id:ID!){{queryTrace(traceId:$id){{spans{{{NATIVE_SPAN_FIELDS}}}}}}}"
),
json!({ "id": required(trace_id, "traceId", operation)? }),
"queryTrace",
operation,
)
.await?;
Ok(native_trace_from_spans(res.get("spans")))
}
pub async fn query_logs(&self, opts: &QueryLogsOptions) -> Result<QueryLogsResult> {
let operation = "query.queryLogs";
let mut c = Map::new();
scoped_ids(
opts.service.as_deref(),
opts.instance.as_deref(),
opts.endpoint.as_deref(),
operation,
&mut c,
)?;
c.insert("paging".into(), opts.paging.to_json(operation)?);
c.insert("queryOrder".into(), opts.order.as_str().into());
if let Some(id) = &opts.trace_id {
let mut related = Map::new();
related.insert("traceId".into(), required(id, "traceId", operation)?.into());
if let Some(s) = &opts.segment_id {
related.insert("segmentId".into(), s.clone().into());
}
if let Some(s) = opts.span_id {
related.insert("spanId".into(), s.into());
}
c.insert("relatedTrace".into(), related.into());
}
if let Some(tags) = tag_list(&opts.tags) {
c.insert("tags".into(), tags);
}
let mut server_keywords = false;
if !opts.keywords.is_empty() || !opts.excluding_keywords.is_empty() {
server_keywords = self.api.capabilities(operation).await?.logs_by_keywords;
if server_keywords {
if !opts.keywords.is_empty() {
c.insert("keywordsOfContent".into(), json!(opts.keywords));
}
if !opts.excluding_keywords.is_empty() {
c.insert(
"excludingKeywordsOfContent".into(),
json!(opts.excluding_keywords),
);
}
}
}
if opts.trace_id.is_none() || opts.range.is_some() {
let d = self
.api
.duration(&opts.range.unwrap_or_default(), operation)
.await?;
c.insert("queryDuration".into(), serde_json::to_value(d)?);
}
let res = self
.api
.field(
&format!("query($c:LogQueryCondition){{queryLogs(condition:$c){{errorReason logs{{{LOG_FIELDS}}}}}}}"),
json!({ "c": c }),
"queryLogs",
operation,
)
.await?;
let mut logs: Vec<NativeLog> = objects(res.get("logs")).map(decode_native_log).collect();
if !server_keywords {
logs.retain(|l| {
contains_all(&l.content, &opts.keywords)
&& contains_none(&l.content, &opts.excluding_keywords)
});
}
Ok(QueryLogsResult {
logs,
error_reason: res
.get("errorReason")
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.map(str::to_owned),
})
}
pub async fn list_log_tag_keys(&self, range: Option<TimeRange>) -> Result<Vec<String>> {
let operation = "query.listLogTagKeys";
let d = self
.api
.duration(&range.unwrap_or_default(), operation)
.await?;
let keys = self
.api
.field(
"query($d:Duration!){queryLogTagAutocompleteKeys(duration:$d)}",
json!({ "d": d }),
"queryLogTagAutocompleteKeys",
operation,
)
.await?;
let mut out = strings(Some(&keys));
out.sort();
Ok(out)
}
pub async fn topology(
&self,
range: Option<TimeRange>,
opts: &TopologyOptions,
) -> Result<Topology> {
let operation = "query.topology";
let d = self
.api
.duration(&range.unwrap_or_default(), operation)
.await?;
let res = match &opts.service {
Some(service) => {
self.api
.field(
&format!("query($s:ID!,$d:Duration!){{getServiceTopology(serviceId:$s,duration:$d){{{TOPOLOGY_FIELDS}}}}}"),
json!({ "s": service_id(required(service, "service", operation)?), "d": d }),
"getServiceTopology",
operation,
)
.await?
}
None => {
self.api
.field(
&format!("query($d:Duration!,$l:String){{getGlobalTopology(duration:$d,layer:$l){{{TOPOLOGY_FIELDS}}}}}"),
json!({ "d": d, "l": opts.layer }),
"getGlobalTopology",
operation,
)
.await?
}
};
Ok(decode_topology(&res))
}
}