Skip to content

Commit

Permalink
feat(aws): init aws cloudwatch
Browse files Browse the repository at this point in the history
Signed-off-by: mlycore <maxwell92@126.com>
  • Loading branch information
mlycore committed Aug 12, 2022
1 parent b58b62d commit b69c0d7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pisa-proxy/audit/aws/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "aws"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aws-config = "0.47.0"
aws-sdk-cloudwatchlogs = { version = "0.17.0", default-features = false, features = ["rustls"]}
chrono = "0.4.0"
tokio = { version = "1", features = ["full"] }
82 changes: 82 additions & 0 deletions pisa-proxy/audit/aws/src/aws.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2022 SphereEx Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use aws_config::meta::region::RegionProviderChain;
use aws_sdk_cloudwatchlogs::{model::InputLogEvent, Client};
use chrono::Utc;

pub struct CloudWatchLog {
log_group_name: String,
log_stream_name: String,
timestamp: i64,
message: String,
}

impl CloudWatchLog {
pub fn new(group: String, stream: String, message: String) -> Self {
let now = Utc::now();
CloudWatchLog {
log_group_name: group,
log_stream_name: stream,
timestamp: now.timestamp_millis(),
message,
}
}
}

pub struct CloudWatchSinker {
pub client: Client,
// pub region: String,
}

impl CloudWatchSinker {
pub async fn new() -> Self {
let region_provider = RegionProviderChain::first_try("us-east-1");
let shared_config = aws_config::from_env().region(region_provider).load().await;
let client = Client::new(&shared_config);

CloudWatchSinker { client }
}
pub async fn send(&self, input: CloudWatchLog) -> Result<(), aws_sdk_cloudwatchlogs::Error> {
let log_group_name = input.log_group_name.clone();
let log_stream_name = input.log_stream_name.clone();
let message = input.message.clone();

let streams =
self.client.describe_log_streams().log_group_name(input.log_group_name).send().await?;
for s in streams.log_streams().unwrap() {
if s.log_stream_name().unwrap() == log_stream_name.clone() {
let next = s.upload_sequence_token().unwrap();
let builder = InputLogEvent::builder();
let e = builder
.set_message(Some(message.clone()))
.set_timestamp(Some(input.timestamp))
.build();

let resp = self
.client
.put_log_events()
.set_sequence_token(Some(next.to_string()))
.log_group_name(log_group_name.clone())
.log_stream_name(log_stream_name)
.log_events(e)
.send()
.await?;
println!("aws resp {:?}", resp);
break;
}
}
Ok(())
}
}
15 changes: 15 additions & 0 deletions pisa-proxy/audit/aws/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 SphereEx Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod aws;

0 comments on commit b69c0d7

Please sign in to comment.