-
Notifications
You must be signed in to change notification settings - Fork 48
Adding logs for cloud #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cede422
adding logs.rs
joshuajerin aa10517
fetching org_id and instance_id
joshuajerin b87710c
adding fmt changes
joshuajerin 17eb5d4
Merge branch 'main' into logs_cloud
joshuajerin 85c4b45
minor changes
joshuajerin 6d12b8d
Merge branch 'main' of https://github.com/tembo-io/tembo into logs_cloud
joshuajerin 83e8b6e
removing unnecessary changes
joshuajerin 71764d5
adding simple parse to logs
joshuajerin 612f808
Merge branch 'logs_cloud' of https://github.com/tembo-io/tembo into l…
joshuajerin d915a24
minor preetify
joshuajerin 18b9061
minor fmt change
joshuajerin 77f8e1c
Merge branch 'main' into logs_cloud
joshuajerin 72b9cc8
format logs and removing verbose
joshuajerin 7f3bf37
read from credentials
joshuajerin a0596f7
Merge branch 'main' into logs_cloud
joshuajerin 328f258
adding unit tests
joshuajerin 2718829
fmt changes
joshuajerin 4c30dee
Merge branch 'main' into logs_cloud
joshuajerin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use crate::apply::{get_instance_id, get_instance_settings}; | ||
use crate::cli::context::{get_current_context, Environment, Profile}; | ||
use anyhow::Result; | ||
use clap::Args; | ||
use reqwest::blocking::Client; | ||
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION}; | ||
use serde::{Deserialize, Serialize}; | ||
use temboclient::apis::configuration::Configuration; | ||
|
||
#[derive(Args)] | ||
pub struct LogsCommand { | ||
#[clap(short, long)] | ||
pub verbose: bool, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct LogStream { | ||
app: String, | ||
container: String, | ||
pod: String, | ||
stream: String, | ||
tembo_instance_id: String, | ||
tembo_organization_id: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct LogEntry { | ||
stream: LogStream, | ||
values: Vec<Vec<String>>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct LogResult { | ||
resultType: String, | ||
result: Vec<LogEntry>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
struct LogData { | ||
status: String, | ||
data: LogResult, | ||
} | ||
|
||
fn beautify_logs(json_data: &str) -> Result<()> { | ||
let log_data: LogData = serde_json::from_str(json_data)?; | ||
|
||
for entry in log_data.data.result { | ||
//println!("\nApp: {}, Container: {}, Pod: {}", entry.stream.app, entry.stream.container, entry.stream.pod); | ||
for value in entry.values { | ||
let log_message = &value[1]; | ||
println!("\n{}", log_message); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn execute(verbose: bool) -> Result<()> { | ||
let env = get_current_context()?; | ||
let org_id = env.org_id.clone().unwrap_or_default(); | ||
let profile = env.selected_profile.clone().unwrap(); | ||
|
||
let config = Configuration { | ||
base_path: profile.tembo_host, | ||
bearer_access_token: Some(profile.tembo_access_token.clone()), | ||
..Default::default() | ||
}; | ||
|
||
let instance_settings = get_instance_settings(None, None)?; | ||
|
||
let client = Client::new(); | ||
let mut headers = HeaderMap::new(); | ||
headers.insert("X-Scope-OrgID", HeaderValue::from_str(&org_id)?); | ||
headers.insert( | ||
AUTHORIZATION, | ||
HeaderValue::from_str(&format!("Bearer {}", profile.tembo_access_token))?, | ||
); | ||
|
||
for (_key, value) in instance_settings.iter() { | ||
let instance_id_option = | ||
get_instance_id(value.instance_name.clone(), &config, env.clone())?; | ||
|
||
let instance_id = if let Some(id) = instance_id_option { | ||
id | ||
} else { | ||
eprintln!("Instance ID not found for {}", value.instance_name); | ||
continue; | ||
}; | ||
|
||
let query = format!("{{tembo_instance_id=\"{}\"}}", instance_id); | ||
let url = "https://api.data-1.use1.tembo.io/loki/api/v1/query_range"; | ||
joshuajerin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let response = client | ||
.get(url) | ||
.headers(headers.clone()) | ||
.query(&[("query", &query)]) | ||
.send()?; | ||
|
||
if response.status().is_success() { | ||
let response_body = response.text()?; | ||
beautify_logs(&response_body)?; | ||
} else { | ||
eprintln!("Error: {:?}", response.status()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ pub mod apply; | |
pub mod context; | ||
pub mod delete; | ||
pub mod init; | ||
pub mod logs; | ||
pub mod validate; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.