#fastly #api-client #api #client #api-bindings

fastly_rt

Rust库用于Fastly实时统计API

1个不稳定版本

0.1.0 2022年2月1日

#482 in HTTP客户端

BSD-3-Clause许可

25KB
589

fastly-rt

Fastly实时分析API和实时源指标的Rust库。


lib.rs:

Fastly实时API客户端

该库包含Fastly的实时分析和实时源指标包装器。

实时分析

实时分析提供服务的统计数据。相关结构有service::ServiceResponseservice::ServiceDataInSecondservice::ServiceStats

获取连续的统计信息

use fastly_rt::service::ServiceClient;
use std::env;
use std::{thread, time};

#[tokio::main]
async fn main() {
    let api_key = env::var("KEY").expect("env KEY not set");
    let sid = env::var("SID").expect("env SID not set");

    let mut rt = ServiceClient::new(&api_key, &sid).unwrap();

    for _ in 0..5 {
        let rt_data = rt.get_stats_consecutive().await.unwrap();
       
        for data in rt_data.data {
            println!("time {}, number of requests {}", data.recorded, data.aggregated.requests);
        }

        thread::sleep(time::Duration::from_secs(1));
    }
}

获取最后120秒的统计信息

use fastly_rt::service::ServiceClient;
use std::env;

#[tokio::main]
async fn main() {
    let api_key = env::var("KEY").expect("env KEY not set");
    let sid = env::var("SID").expect("env SID not set");
    
    let mut rt = ServiceClient::new(&api_key, &sid).unwrap();

    let rt_data = rt.get_stats_120s().await.unwrap();
       
    for data in rt_data.data {
        println!("time {}, number of requests {}", data.recorded, data.aggregated.requests);
    }
}

获取最后10秒的统计信息

use fastly_rt::service::ServiceClient;
use std::env;

#[tokio::main]
async fn main() {
    let api_key = env::var("KEY").expect("env KEY not set");
    let sid = env::var("SID").expect("env SID not set");

    let mut rt = ServiceClient::new(&api_key, &sid).unwrap();

    let rt_data = rt.get_stats_max(10).await.unwrap();
       
    for data in rt_data.data {
        println!("time {}, number of requests {}", data.recorded, data.aggregated.requests);
    }
}

实时源指标

实时源指标提供服务的源统计信息。相关结构有origin::OriginResponseorigin::OriginDataInSecondorigin::OriginStats

示例与实时源指标相似

依赖项

~4–16MB
~236K SLoC