Skip to content
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

feat(tool): Add a binary to generate the schema file #739

Merged
merged 3 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/data-schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- run: make doc-schema
- run: make init-submodules
- run: cargo run -p generate-schema > event.schema.json
- name: commit and push
if: github.ref == 'refs/heads/master'
env:
Expand All @@ -28,7 +29,7 @@ jobs:
git clone https://getsentry-bot:[email protected]/getsentry/sentry-data-schemas
cd sentry-data-schemas/
mkdir -p ./relay/
cp ../docs/event-schema/event.schema.json ./relay/event.schema.json
mv ../event.schema.json ./relay/event.schema.json
if git diff-files --quiet; then
echo "No changes"
exit 0
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Remove a temporary flag from attachment kafka messages indicating rate limited crash reports to Sentry. This is now enabled by default. ([#718](https://github.com/getsentry/relay/pull/718))
- Performance improvement of http requests to upstream, high priority messages are sent first. ([#678](https://github.com/getsentry/relay/pull/678))
- Experimental data scrubbing on minidumps([#682](https://github.com/getsentry/relay/pull/682))
- Move `generate-schema` from the Relay CLI into a standalone tool. ([#739](//github.com/getsentry/relay/pull/739))

## 20.8.0

Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,6 @@ doc-metrics: .venv/bin/python
.venv/bin/pip install -U -r requirements-doc.txt
cd scripts && ../.venv/bin/python extract_metric_docs.py

doc-schema: init-submodules
# Makes no sense to nest this in docs, but unfortunately that's the path
# Snuba uses in their setup right now. Eventually this should be gone in
# favor of the data schemas repo
mkdir -p docs/event-schema/
rm -rf docs/event-schema/event.schema.*
set -e && cd relay && cargo run --features jsonschema -- event-json-schema \
> ../docs/event-schema/event.schema.json
.PHONY: doc-schema

doc-server: doc-prose
.venv/bin/mkdocs serve
.PHONY: doc-server
Expand Down
1 change: 0 additions & 1 deletion relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ publish = false
default = ["ssl"]
ssl = ["relay-server/ssl"]
processing = ["relay-server/processing"]
jsonschema = ["relay-general/jsonschema"]

# Direct dependencies of the main application in `src/`
[dependencies]
Expand Down
15 changes: 0 additions & 15 deletions relay/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ pub fn execute() -> Result<(), Error> {
return generate_completions(&matches);
} else if let Some(matches) = matches.subcommand_matches("process-event") {
return process_event(&matches);
} else if let Some(_matches) = matches.subcommand_matches("event-json-schema") {
#[cfg(feature = "jsonschema")]
return event_json_schema(&_matches);

#[cfg(not(feature = "jsonschema"))]
failure::bail!("Relay needs to be compiled with the 'jsonschema' feature for this command to be available.");
}

// Commands that need a loaded config:
Expand Down Expand Up @@ -396,15 +390,6 @@ pub fn process_event<'a>(matches: &ArgMatches<'a>) -> Result<(), Error> {
Ok(())
}

#[cfg(feature = "jsonschema")]
pub fn event_json_schema<'a>(_matches: &ArgMatches<'a>) -> Result<(), Error> {
serde_json::to_writer_pretty(
&mut io::stdout().lock(),
&relay_general::protocol::event_json_schema(),
)?;
Ok(())
}

pub fn run<'a>(config: Config, _matches: &ArgMatches<'a>) -> Result<(), Error> {
setup::dump_spawn_infos(&config);
setup::check_config(&config)?;
Expand Down
16 changes: 16 additions & 0 deletions tools/generate-schema/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "generate-schema"
version = "0.1.0"
authors = ["Sentry <[email protected]>"]
description = "Generates an event payload schema file"
homepage = "https://getsentry.github.io/relay/"
repository = "https://github.com/getsentry/relay"
edition = "2018"
publish = false

[dependencies]
anyhow = "1.0.32"
paw = "1.0.0"
relay-general = { path = "../../relay-general", features = ["jsonschema"] }
serde_json = "1.0.55"
structopt = { version = "0.3.16", features = ["paw"] }
90 changes: 90 additions & 0 deletions tools/generate-schema/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use std::fmt;
use std::fs::File;
use std::io;
use std::path::PathBuf;

use anyhow::Result;
use structopt::clap::AppSettings;
use structopt::StructOpt;

#[derive(Clone, Copy, Debug)]
enum SchemaFormat {
JsonSchema,
}

impl fmt::Display for SchemaFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::JsonSchema => write!(f, "jsonschema"),
}
}
}

#[derive(Debug)]
struct ParseSchemaFormatError;

impl fmt::Display for ParseSchemaFormatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid schema format")
}
}

impl std::error::Error for ParseSchemaFormatError {}

impl std::str::FromStr for SchemaFormat {
type Err = ParseSchemaFormatError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"jsonschema" => Ok(Self::JsonSchema),
_ => Err(ParseSchemaFormatError),
}
}
}

/// Prints the event protocol schema.
#[derive(Debug, StructOpt)]
#[structopt(verbatim_doc_comment, setting = AppSettings::ColoredHelp)]
struct Cli {
/// The format to output the schema in.
#[structopt(short, long, default_value = "jsonschema")]
format: SchemaFormat,

/// Optional output path. By default, the schema is printed on stdout.
#[structopt(short, long, value_name = "PATH")]
output: Option<PathBuf>,
}

impl Cli {
pub fn run(self) -> Result<()> {
let schema = relay_general::protocol::event_json_schema();

match self.output {
Some(path) => serde_json::to_writer_pretty(File::create(path)?, &schema)?,
None => serde_json::to_writer_pretty(io::stdout(), &schema)?,
}

Ok(())
}
}

fn print_error(error: &anyhow::Error) {
eprintln!("Error: {}", error);

let mut cause = error.source();
while let Some(ref e) = cause {
eprintln!(" caused by: {}", e);
cause = e.source();
}
}

#[paw::main]
fn main(cli: Cli) {
match cli.run() {
Ok(()) => (),
Err(error) => {
print_error(&error);
std::process::exit(1);
}
}
}