Skip to content

Commit

Permalink
chore: update vendored proto file
Browse files Browse the repository at this point in the history
This commit updates the vendored proto file to that found in
version 0.249.0 of the Go SDK, and adds a couple of extra
fields:

- `api_version`, on app and datasource instance settings
- `error_source`, on a data response. This can be customimsed by
  overriding the new `DataQueryError::source` method.
  • Loading branch information
sd2k committed Sep 19, 2024
1 parent 02aa423 commit fba067b
Show file tree
Hide file tree
Showing 6 changed files with 1,238 additions and 88 deletions.
2 changes: 1 addition & 1 deletion crates/grafana-plugin-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.5.0"
authors = ["Ben Sully <ben.sully@grafana.com>"]
license = "MIT/Apache-2.0"
edition = "2021"
rust-version = "1.63"
rust-version = "1.77"
repository = "https://github.com/grafana/grafana-plugin-sdk-rust"
description = "SDK for building Grafana backend plugins."

Expand Down
18 changes: 15 additions & 3 deletions crates/grafana-plugin-sdk/src/backend/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use futures_util::StreamExt;
use serde::de::DeserializeOwned;

use crate::{
backend::{self, ConvertFromError, InstanceSettings, TimeRange},
backend::{
self, error_source::ErrorSource, ConvertFromError, GrafanaPlugin, InstanceSettings,
PluginType, TimeRange,
},
data, pluginv2,
};

use super::{GrafanaPlugin, PluginType};

/// A request for data made by Grafana.
///
/// Details of the request source can be found in `plugin_context`,
Expand Down Expand Up @@ -184,6 +185,13 @@ pub trait DataQueryError: std::error::Error {
fn status(&self) -> DataQueryStatus {
DataQueryStatus::Unknown
}

/// The source of the error.
///
/// Defaults to [`ErrorSource::Plugin`] if not overridden.
fn source(&self) -> ErrorSource {
ErrorSource::default()
}
}

/// Status codes for [`DataQueryError`].
Expand Down Expand Up @@ -454,6 +462,7 @@ where
status: DataQueryStatus::Internal.as_i32(),
error: e.to_string(),
json_meta: vec![],
error_source: ErrorSource::Plugin.to_string(),
},
)
},
Expand All @@ -465,13 +474,15 @@ where
status: DataQueryStatus::OK.as_i32(),
error: "".to_string(),
json_meta: vec![],
error_source: "".to_string(),
},
)
},
)
}
Err(e) => {
let status = e.status().as_i32();
let source = e.source().to_string();
let err_string = e.to_string();
(
e.ref_id(),
Expand All @@ -480,6 +491,7 @@ where
status,
error: err_string,
json_meta: vec![],
error_source: source,
},
)
}
Expand Down
23 changes: 23 additions & 0 deletions crates/grafana-plugin-sdk/src/backend/error_source.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::fmt;

/// The source of an error.
///
/// This is used to indicate whether the error occurred in the plugin or downstream.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorSource {
/// The error occurred in the plugin.
#[default]
Plugin,
/// The error occurred downstream of the plugin.
Downstream,
}

impl fmt::Display for ErrorSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Plugin => f.write_str("plugin"),
Self::Downstream => f.write_str("downstream"),
}
}
}
14 changes: 14 additions & 0 deletions crates/grafana-plugin-sdk/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub use tonic::async_trait;

mod data;
mod diagnostics;
mod error_source;
mod noop;
mod resource;
mod stream;
Expand All @@ -170,6 +171,7 @@ pub use diagnostics::{
CheckHealthRequest, CheckHealthResponse, CollectMetricsRequest, CollectMetricsResponse,
DiagnosticsService, HealthStatus, Payload as MetricsPayload,
};
pub use error_source::ErrorSource;
pub use resource::{
BoxResourceFuture, BoxResourceStream, CallResourceRequest, ErrIntoHttpResponse,
IntoHttpResponse, ResourceService,
Expand Down Expand Up @@ -879,6 +881,10 @@ pub struct AppInstanceSettings<JsonData, SecureJsonData> {
pub decrypted_secure_json_data: SecureJsonData,
/// The last time the configuration for the app plugin instance was updated.
pub updated: DateTime<Utc>,

/// The API version when the settings were saved.
/// NOTE: this may be an older version than the current apiVersion.
pub api_version: String,
}

impl<JsonData, SecureJsonData> Debug for AppInstanceSettings<JsonData, SecureJsonData>
Expand All @@ -890,6 +896,7 @@ where
.field("json_data", &self.json_data)
.field("decrypted_secure_json_data", &"<redacted>")
.field("updated", &self.updated)
.field("api_version", &self.api_version)
.finish()
}
}
Expand Down Expand Up @@ -918,6 +925,7 @@ where
.ok_or(ConvertFromError::InvalidTimestamp {
timestamp: proto.last_updated_ms,
})?,
api_version: proto.api_version,
})
})
.transpose()
Expand Down Expand Up @@ -988,6 +996,10 @@ pub struct DataSourceInstanceSettings<JsonData, SecureJsonData> {

/// The last time the configuration for the datasource instance was updated.
pub updated: DateTime<Utc>,

/// The API version when the settings were saved.
/// NOTE: this may be an older version than the current apiVersion.
pub api_version: String,
}

impl<JsonData, SecureJsonData> Debug for DataSourceInstanceSettings<JsonData, SecureJsonData>
Expand All @@ -1008,6 +1020,7 @@ where
.field("json_data", &self.json_data)
.field("decrypted_secure_json_data", &"<redacted>")
.field("updated", &self.updated)
.field("api_version", &self.api_version)
.finish()
}
}
Expand Down Expand Up @@ -1045,6 +1058,7 @@ where
.ok_or(ConvertFromError::InvalidTimestamp {
timestamp: proto.last_updated_ms,
})?,
api_version: proto.api_version,
})
})
.transpose()
Expand Down
Loading

0 comments on commit fba067b

Please sign in to comment.