Update versions, some cleanup.

This commit is contained in:
Syfaro 2023-02-10 22:00:42 -05:00
parent 510da555e6
commit e1f5b02464
9 changed files with 997 additions and 419 deletions

View File

@ -2,61 +2,37 @@ name: Release
on:
push:
pull_request:
tags:
- "*"
env:
CARGO_TERM_COLOR: always
jobs:
release:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Cache target
uses: actions/cache@v2
with:
path: |
target/
key: ${{ runner.os }}-debug-${{ hashFiles('Cargo.lock') }}
- name: Get stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Build binaries
uses: actions-rs/cargo@v1
with:
command: build
args: --release
- name: Move binaries
run: |
cargo build --release --bin bkapi
mv target/release/bkapi bkapi/bkapi
- name: Upload binaries
uses: actions/upload-artifact@v2
with:
name: bkapi
path: bkapi/bkapi
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v3
uses: docker/metadata-action@v4
with:
images: ghcr.io/syfaro/bkapi
- name: Build and push Docker image
uses: docker/build-push-action@v2
uses: docker/build-push-action@v4
with:
context: .
push: true

1265
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,3 +3,8 @@ members = [
"bkapi",
"bkapi-client",
]
[workspace.metadata.release]
sign-tag = true
shared-version = true
tag-name = "v{{version}}"

View File

@ -3,6 +3,7 @@ name = "bkapi-client"
version = "0.1.0"
authors = ["Syfaro <syfaro@huefox.com>"]
edition = "2018"
publish = false
[dependencies]
tracing = "0.1"
@ -14,7 +15,7 @@ opentelemetry-http = "0.7"
futures = "0.3"
reqwest = { version = "0.11", features = ["json"] }
async-nats = "0.21"
async-nats = "0.27"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

View File

@ -77,7 +77,6 @@ impl BKApiClient {
/// Search for multiple hashes given a single maximum distance.
///
/// Results are returned in the same order as given hashes.
#[tracing::instrument(err, skip(self))]
pub async fn search_many(
&self,
hashes: &[i64],

View File

@ -3,6 +3,7 @@ name = "bkapi"
version = "0.1.0"
authors = ["Syfaro <syfaro@huefox.com>"]
edition = "2018"
publish = false
[dependencies]
dotenvy = "0.15"
@ -11,7 +12,7 @@ thiserror = "1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-unwrap = "0.9"
tracing-unwrap = "0.10"
tracing-opentelemetry = "0.18"
opentelemetry = { version = "0.18", features = ["rt-tokio"] }
@ -32,9 +33,11 @@ serde_json = "1"
actix-web = "4"
actix-http = "3"
actix-service = "2"
tracing-actix-web = { version = "0.6", features = ["opentelemetry_0_18"] }
tracing-actix-web = { version = "0.7", features = ["opentelemetry_0_18"] }
async-nats = "0.21"
async-nats = "0.27"
foxlib = { git = "https://github.com/Syfaro/foxlib.git" }
[dependencies.sqlx]
version = "0.6"

View File

@ -1,4 +1,4 @@
FROM ubuntu:20.04
FROM ubuntu:22.04
EXPOSE 3000
COPY ./bkapi/bkapi /bin/bkapi
CMD ["/bin/bkapi"]

View File

@ -1,18 +1,15 @@
use std::sync::Arc;
use std::{net::SocketAddr, sync::Arc};
use actix_service::Service;
use actix_web::{
get,
web::{self, Data},
App, HttpResponse, HttpServer, Responder,
App, HttpResponse, HttpServer,
};
use clap::Parser;
use futures::StreamExt;
use opentelemetry::KeyValue;
use prometheus::{Encoder, TextEncoder};
use sqlx::postgres::PgPoolOptions;
use tracing::Instrument;
use tracing_subscriber::layer::SubscriberExt;
use tracing_unwrap::ResultExt;
mod tree;
@ -42,12 +39,12 @@ struct Config {
#[clap(long, env, default_value = "127.0.0.1:3000")]
http_listen: String,
/// Jaeger agent endpoint for span collection.
#[clap(long, env, default_value = "127.0.0.1:6831")]
jaeger_agent: String,
/// Service name for spans.
#[clap(long, env, default_value = "bkapi")]
service_name: String,
/// Host to listen for metrics requests.
#[clap(long, env, default_value = "127.0.0.1:3001")]
metrics_host: SocketAddr,
/// If logs should be output in JSON format and sent to otlp collector.
#[clap(long, env)]
json_logs: bool,
/// Database URL for fetching data.
#[clap(long, env)]
@ -77,10 +74,18 @@ async fn main() {
let _ = dotenvy::dotenv();
let config = Config::parse();
configure_tracing(&config);
foxlib::trace_init(foxlib::TracingConfig {
namespace: "bkapi",
name: "bkapi",
version: env!("CARGO_PKG_VERSION"),
otlp: config.json_logs,
});
tracing::info!("starting bkapi");
let metrics_server = foxlib::MetricsServer::serve(config.metrics_host, false).await;
let tree = tree::Tree::new();
tracing::trace!("connecting to postgres");
@ -136,7 +141,9 @@ async fn main() {
receiver
.await
.expect_or_log("tree loading was dropped before completing");
tracing::info!("initial tree loaded, starting server");
metrics_server.set_ready(true);
if let Some(client) = client {
let tree_clone = tree.clone();
@ -151,40 +158,6 @@ async fn main() {
start_server(config, tree).await.unwrap_or_log();
}
fn configure_tracing(config: &Config) {
opentelemetry::global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new());
let env = std::env::var("ENVIRONMENT");
let env = if let Ok(env) = env.as_ref() {
env.as_str()
} else if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
let tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_endpoint(&config.jaeger_agent)
.with_service_name(&config.service_name)
.with_trace_config(opentelemetry::sdk::trace::config().with_resource(
opentelemetry::sdk::Resource::new(vec![
KeyValue::new("environment", env.to_owned()),
KeyValue::new("version", env!("CARGO_PKG_VERSION")),
]),
))
.install_batch(opentelemetry::runtime::Tokio)
.expect("otel jaeger pipeline could not be created");
let trace = tracing_opentelemetry::layer().with_tracer(tracer);
tracing::subscriber::set_global_default(
tracing_subscriber::Registry::default()
.with(tracing_subscriber::EnvFilter::from_default_env())
.with(trace)
.with(tracing_subscriber::fmt::layer()),
)
.expect("tracing could not be configured");
}
async fn start_server(config: Config, tree: tree::Tree) -> Result<(), Error> {
let tree = Data::new(tree);
let config_data = Data::new(config.clone());
@ -217,8 +190,6 @@ async fn start_server(config: Config, tree: tree::Tree) -> Result<(), Error> {
.app_data(tree.clone())
.app_data(config_data.clone())
.service(search)
.service(health)
.service(metrics)
})
.bind(&config.http_listen)
.expect_or_log("bind failed")
@ -227,22 +198,6 @@ async fn start_server(config: Config, tree: tree::Tree) -> Result<(), Error> {
.map_err(Error::Io)
}
#[get("/health")]
async fn health() -> impl Responder {
"OK"
}
#[get("/metrics")]
async fn metrics() -> HttpResponse {
let mut buffer = Vec::new();
let encoder = TextEncoder::new();
let metric_families = prometheus::gather();
encoder.encode(&metric_families, &mut buffer).unwrap();
HttpResponse::Ok().body(buffer)
}
#[derive(Debug, serde::Deserialize)]
struct Query {
hash: i64,

View File

@ -166,7 +166,7 @@ struct Payload {
}
/// Listen for incoming payloads from Postgres.
#[tracing::instrument(skip(conn, subscription, query, tree, initial))]
#[tracing::instrument(skip_all)]
pub(crate) async fn listen_for_payloads_db(
conn: Pool<Postgres>,
subscription: String,
@ -204,7 +204,7 @@ pub(crate) async fn listen_for_payloads_db(
}
/// Listen for incoming payloads from NATS.
#[tracing::instrument(skip(config, pool, client, tree, initial))]
#[tracing::instrument(skip_all)]
pub(crate) async fn listen_for_payloads_nats(
config: Config,
pool: sqlx::PgPool,