:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(logs): add the Loki import config contract and command surface (#105590)
Jimmy Zhu1c427ea85746
diff --git a/cli/src/commands.rs b/cli/src/commands.rsindex a1d11a371db1..a04d05b31d0c 100644--- a/cli/src/commands.rs+++ b/cli/src/commands.rs@@ -9,7 +9,10 @@ use crate::{download::SymbolSetsSubcommand,dsym::DsymSubcommand,error::CapturedError,- experimental::{endpoints::EndpointCommand, query::command::QueryCommand, tasks::TaskCommand},+ experimental::{+ endpoints::EndpointCommand, logs::command::LogsCommand, query::command::QueryCommand,+ tasks::TaskCommand,+ },invocation_context::{capture_command_run_without_context, context, init_context, set_telemetry_command_name,set_telemetry_env_id_from_environment, INVOCATION_CONTEXT,@@ -229,6 +232,12 @@ pub enum ExpCommand {cmd: EndpointCommand,},+ /// Import historical logs into PostHog from another log store+ Logs {+ #[command(subcommand)]+ cmd: LogsCommand,+ },+// TODO(sept 2026): remove these backward-compat aliases, they moved to top-level commands#[command(about = "Upload hermes sourcemaps to PostHog", hide = true)]Hermes {@@ -305,6 +314,9 @@ impl Commands {impl ExpCommand {fn telemetry_command_name(&self) -> &'static str {match self {+ ExpCommand::Logs { cmd } => match cmd {+ LogsCommand::Import { .. } => "logs_import",+ },ExpCommand::Task { cmd, .. } => match cmd {TaskCommand::List { .. } => "task_list",TaskCommand::Progress { .. } => "task_progress",@@ -527,6 +539,9 @@ impl Cli {ExpCommand::Endpoints { cmd } => {cmd.run()?;}+ ExpCommand::Logs { cmd } => {+ cmd.run()?;+ }// TODO(sept 2026): remove these backward-compat aliasesExpCommand::Hermes { cmd } => match cmd {HermesSubcommand::Inject(args) => {diff --git a/cli/src/experimental/logs/README.md b/cli/src/experimental/logs/README.mdnew file mode 100644index 000000000000..a61058f81689--- /dev/null+++ b/cli/src/experimental/logs/README.md@@ -0,0 +1,99 @@+# Importing historical logs from Loki++`posthog-cli exp logs import loki` reads a time range out of Grafana Loki and sends it to PostHog+as OTLP.++Loki guarantees only three things per entry: a nanosecond timestamp, a log line, and a set of+stream labels whose names are arbitrary. It has no schema and no reserved label names. So the+timestamp and the body map themselves, and everything else has to be told, which is what the+config file is for.++## Running it++```sh+export LOKI_USERNAME=123456 # Grafana Cloud instance id+export LOKI_PASSWORD=glc_... # Grafana Cloud access policy token+export POSTHOG_PROJECT_API_KEY=phc_...++posthog-cli exp logs import loki --config loki-import.yaml --dry-run+posthog-cli exp logs import loki --config loki-import.yaml --checkpoint /data/import.state+```++Self-hosted Loki behind a bearer token uses `LOKI_BEARER_TOKEN` instead of the username and+password. The PostHog host comes from `--host`, or from `posthog-cli login`, so an EU project needs+no extra setting.++`POSTHOG_PROJECT_API_KEY` is the project write key, not a personal API token. The intake rejects a+personal token, and the project key is already public-facing, so a long unattended run never holds+the operator's own credential.++Always run `--dry-run` first. It sizes the job and reports how many sampled records each extraction+rule actually matched. A rule that matches nothing reports `NOT FOUND`, which is the only warning+you get before a run that would otherwise take hours and produce unusable data.++## The config++```yaml+version: 1++source:+ url: https://logs-prod-012.grafana.net+ tenant: prod # X-Scope-OrgID, omit when single-tenant++range:+ from: 2025-03-01T00:00:00Z+ to: 2026-09-01T00:00:00Z+ select:+ - '{namespace="prod"}'++extract:+ service_name: { type: label, name: app }+ severity:+ - { type: json_field, path: level }+ - { type: label, name: level }+ trace_id: { type: structured_metadata, key: trace_id }+ resource_labels: [namespace, app, container]++tuning:+ shard: 1h+ max_records_per_second: 20000+ max_request_bytes: 1500000+```++Extractor types are `label`, `structured_metadata`, `json_field`, `regex` (with a `group`), and+`literal`.++Every field takes either one extractor or a list of them, and the first rule that yields a value+wins. `severity` is the field where that matters most, since a level can sit in a label on some+streams and inside the JSON body on others, but `service_name`, `trace_id`, `span_id` and each+`extra_attributes` entry accept a list on the same terms.++`resource_labels` lists the labels that identify the resource; every other label becomes a+per-record attribute. Omit the key to treat every label as a resource attribute.++Structured metadata needs Loki 3.0 with schema v13 and a writer that sent it. Older data has none,+so `trace_id` rules against it report `NOT FOUND` in a dry run.++### Where the values come from++Your Alloy or Promtail config already answers most of this, in reverse. A `labels` stage that+promoted `level` means `{ type: label, name: level }`; a level left inside a JSON body means+`{ type: json_field, path: level }`. One difference: Alloy stages are an ordered pipeline sharing+an extracted map, while this is a flat per-field mapping, so rules here do not compose.++## Resuming++Progress is recorded per selector after each shard is fully sent. A resumed run re-sends only the+shard it was inside, because the intake assigns record ids at ingest and does not deduplicate, so+an interrupted run cannot avoid that overlap.++Point `--checkpoint` at durable storage. In a Kubernetes Job that means a volume, not the+container filesystem: an evicted pod that loses its checkpoint restarts the whole range.++Widening `range.from` discards the checkpoint and re-imports everything, and says so. Extending+`range.to` forward keeps the progress already made.++## Retention++Imported records take their retention from when they were imported, not from their own timestamp,+so a backfill expires the configured number of retention days after the import finishes.diff --git a/cli/src/experimental/logs/command.rs b/cli/src/experimental/logs/command.rsnew file mode 100644index 000000000000..078f6a5d1e05--- /dev/null+++ b/cli/src/experimental/logs/command.rs@@ -0,0 +1,62 @@+use std::path::PathBuf;++use clap::Subcommand;++#[derive(Debug, Subcommand)]+pub enum LogsCommand {+ /// Import historical logs from another log store into PostHog+ Import {+ #[command(subcommand)]+ cmd: ImportSource,+ },+}++#[derive(Debug, Subcommand)]+pub enum ImportSource {+ /// Read from Grafana Loki. Credentials come from LOKI_BEARER_TOKEN, or LOKI_USERNAME and+ /// LOKI_PASSWORD for Grafana Cloud.+ Loki {+ /// Path to the import config. Its shape is documented in cli/src/experimental/logs/README.md.+ #[arg(long)]+ config: PathBuf,++ /// Where to record progress. A resumed run re-sends only the shard it was inside.+ #[arg(long, default_value = "loki-import.state")]+ checkpoint: PathBuf,++ /// Report the size, duration and mapping coverage of the run without sending anything.+ #[arg(long, default_value_t = false)]+ dry_run: bool,+ },+}++use anyhow::{bail, Result};++impl LogsCommand {+ pub fn run(&self) -> Result<()> {+ match self {+ LogsCommand::Import { cmd } => cmd.run(),+ }+ }+}++impl ImportSource {+ fn run(&self) -> Result<()> {+ match self {+ ImportSource::Loki { config, .. } => {+ // Parsing and validating the config is the whole of this layer. Reading Loki and+ // sending to the intake arrives with the engine.+ let text = std::fs::read_to_string(config).map_err(|error| {+ anyhow::anyhow!("cannot read {}: {error}", config.display())+ })?;+ super::config::LokiImportConfig::parse(&text)?;++ bail!(+ "this build validates the import config but cannot run an import yet. \+ The config at {} parsed cleanly.",+ config.display()+ )+ }+ }+ }+}diff --git a/cli/src/experimental/logs/config.rs b/cli/src/experimental/logs/config.rsnew file mode 100644index 000000000000..28df1c661238--- /dev/null+++ b/cli/src/experimental/logs/config.rs@@ -0,0 +1,493 @@+use std::collections::HashMap;+use std::num::NonZeroU32;++use anyhow::{bail, Context, Result};+use chrono::{DateTime, TimeZone, Utc};+use regex::Regex;+use serde::{Deserialize, Deserializer};+use url::Url;++/// Where one OTel field comes from in a Loki record.+#[derive(Debug, Clone, Deserialize, PartialEq)]+#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]+pub enum Extractor {+ Label { name: String },+ StructuredMetadata { key: String },+ JsonField { path: String },+ Regex { pattern: String, group: usize },+ Literal { value: String },+}++impl Extractor {+ fn validate(&self) -> Result<()> {+ if let Extractor::Regex { pattern, group } = self {+ let compiled = Regex::new(pattern)+ .with_context(|| format!("regex extractor has an invalid pattern: {pattern}"))?;+ if *group == 0 {+ bail!(+ "regex extractor group must be 1 or higher, because group 0 is the whole match"+ );+ }+ if *group >= compiled.captures_len() {+ bail!(+ "regex extractor asks for group {group} but the pattern has {} capture groups",+ compiled.captures_len().saturating_sub(1)+ );+ }+ }+ Ok(())+ }+}++/// One extractor, or an ordered list where the first that yields a value wins.+#[derive(Debug, Clone, PartialEq)]+pub struct Extractors(Vec<Extractor>);++/// Dispatches on the YAML node kind rather than deriving `untagged`, which discards the inner+/// error and turns a misspelled key into "data did not match any variant".+impl<'de> Deserialize<'de> for Extractors {+ fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {+ use serde::de::Error;++ let value = serde_yaml::Value::deserialize(deserializer)?;+ let extractors = if value.is_sequence() {+ Vec::<Extractor>::deserialize(value).map_err(D::Error::custom)?+ } else {+ vec![Extractor::deserialize(value).map_err(D::Error::custom)?]+ };+ Ok(Extractors(extractors))+ }+}++impl Extractors {+ pub fn as_slice(&self) -> &[Extractor] {+ &self.0+ }++ fn validate(&self) -> Result<()> {+ if self.0.is_empty() {+ bail!("an extractor list cannot be empty");+ }+ for extractor in &self.0 {+ extractor.validate()?;+ }+ Ok(())+ }+}++/// A shard width, stored as the seconds it parsed to so no consumer re-parses the string.+#[derive(Debug, Clone, Copy, PartialEq)]+pub struct Shard(u64);++impl Shard {+ pub fn seconds(self) -> u64 {+ self.0+ }+}++impl Default for Shard {+ fn default() -> Self {+ Shard(3600)+ }+}++impl<'de> Deserialize<'de> for Shard {+ fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {+ let raw = String::deserialize(deserializer)?;+ parse_shard(&raw)+ .map(Shard)+ .map_err(serde::de::Error::custom)+ }+}++#[derive(Debug, Clone, Deserialize)]+#[serde(deny_unknown_fields)]+pub struct Source {+ pub url: Url,+ #[serde(default)]+ pub tenant: Option<String>,+}++#[derive(Debug, Clone, Deserialize)]+#[serde(deny_unknown_fields)]+pub struct Range {+ pub from: DateTime<Utc>,+ pub to: DateTime<Utc>,+ pub select: Vec<String>,+}++impl Range {+ fn validate(&self) -> Result<()> {+ if self.from >= self.to {+ bail!("range.from must be earlier than range.to");+ }+ // Loki's query_range speaks nanosecond epochs, which chrono can only represent between+ // 1677 and 2262. A range outside that turns into a silently wrong query.+ let floor = Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap();+ let ceiling = Utc::now() + chrono::Duration::days(1);+ if self.from < floor {+ bail!("range.from must be 1970-01-01 or later");+ }+ if self.to > ceiling {+ bail!("range.to must not be more than a day in the future");+ }+ if self.select.is_empty() {+ bail!(+ "range.select needs at least one stream selector; LogQL rejects an empty matcher"+ );+ }+ if let Some(position) = self.select.iter().position(|s| s.trim().is_empty()) {+ bail!("range.select[{position}] is empty; LogQL rejects an empty matcher");+ }+ Ok(())+ }+}++#[derive(Debug, Clone, Deserialize)]+#[serde(deny_unknown_fields)]+pub struct Extract {+ pub service_name: Extractors,+ #[serde(default)]+ pub severity: Option<Extractors>,+ #[serde(default)]+ pub trace_id: Option<Extractors>,+ #[serde(default)]+ pub span_id: Option<Extractors>,+ /// Labels promoted to resource attributes. Every other label becomes a per-record attribute.+ /// Absent means every label is a resource attribute, which is the Loki-faithful reading.+ #[serde(default)]+ pub resource_labels: Option<Vec<String>>,+ #[serde(default)]+ pub extra_attributes: HashMap<String, Extractors>,+}++impl Extract {+ fn validate(&self) -> Result<()> {+ let named = [+ ("service_name", Some(&self.service_name)),+ ("severity", self.severity.as_ref()),+ ("trace_id", self.trace_id.as_ref()),+ ("span_id", self.span_id.as_ref()),+ ];+ for (field, extractors) in named {+ if let Some(extractors) = extractors {+ extractors+ .validate()+ .with_context(|| format!("extract.{field} is invalid"))?;+ }+ }+ for (name, extractors) in &self.extra_attributes {+ extractors+ .validate()+ .with_context(|| format!("extract.extra_attributes.{name} is invalid"))?;+ }+ if self.resource_labels.as_ref().is_some_and(|l| l.is_empty()) {+ bail!(+ "extract.resource_labels is an empty list, which promotes no labels at all. \+ Remove the key to promote every label instead."+ );+ }+ Ok(())+ }+}++#[derive(Debug, Clone, Deserialize)]+#[serde(default, deny_unknown_fields)]+pub struct Tuning {+ pub shard: Shard,+ pub max_records_per_second: NonZeroU32,+ /// Must stay under the intake's `MAX_REQUEST_BODY_SIZE_BYTES`. The Rust default is 2 MiB but+ /// PostHog Cloud raises it, so this is configurable rather than a constant.+ pub max_request_bytes: usize,+}++/// The intake rejects a larger body with 413, and no batching can recover from that.+const MAX_SUPPORTED_REQUEST_BYTES: usize = 10 * 1024 * 1024;++impl Default for Tuning {+ fn default() -> Self {+ Self {+ shard: Shard::default(),+ max_records_per_second: NonZeroU32::new(20_000).expect("20000 is not zero"),+ max_request_bytes: 1_500_000,+ }+ }+}++impl Tuning {+ fn validate(&self) -> Result<()> {+ if self.max_request_bytes < 64 * 1024 {+ bail!("tuning.max_request_bytes must be at least 65536, or no batch can be filled");+ }+ if self.max_request_bytes > MAX_SUPPORTED_REQUEST_BYTES {+ bail!(+ "tuning.max_request_bytes {} is above {MAX_SUPPORTED_REQUEST_BYTES}; the intake \+ answers a larger body with 413",+ self.max_request_bytes+ );+ }+ Ok(())+ }+}++#[derive(Debug, Clone, Deserialize)]+#[serde(deny_unknown_fields)]+pub struct LokiImportConfig {+ pub version: u32,+ pub source: Source,+ pub range: Range,+ pub extract: Extract,+ #[serde(default)]+ pub tuning: Tuning,+}++/// Read only the version, so a future config reports its version rather than the first field this+/// build does not recognize.+#[derive(Deserialize)]+struct VersionProbe {+ version: u32,+}++impl LokiImportConfig {+ pub fn parse(yaml: &str) -> Result<Self> {+ if let Ok(probe) = serde_yaml::from_str::<VersionProbe>(yaml) {+ if probe.version != 1 {+ bail!("unsupported config version {}, expected 1", probe.version);+ }+ }++ let config: Self =+ serde_yaml::from_str(yaml).context("failed to parse the import config")?;+ config.validate()?;+ Ok(config)+ }++ fn validate(&self) -> Result<()> {+ if self.version != 1 {+ bail!("unsupported config version {}, expected 1", self.version);+ }+ self.range.validate()?;+ self.extract.validate()?;+ self.tuning.validate()+ }+}++/// Parses a shard width such as `30m`, `1h` or `6h` into seconds.+fn parse_shard(shard: &str) -> Result<u64> {+ let split = shard+ .find(|c: char| !c.is_ascii_digit())+ .unwrap_or(shard.len());+ let (value, unit) = shard.split_at(split);+ let value: u64 = value.parse().with_context(|| {+ format!("shard '{shard}' must start with a number of seconds, minutes or hours")+ })?;+ if value == 0 {+ bail!("shard must be longer than zero");+ }+ let per_unit = match unit {+ "s" => 1,+ "m" => 60,+ "h" => 3600,+ _ => bail!("shard '{shard}' must end in s, m or h"),+ };+ let seconds = value+ .checked_mul(per_unit)+ .with_context(|| format!("shard '{shard}' is too large to express in seconds"))?;+ // Loki's max_query_length defaults to 721h, and a shard is issued as one query_range call.+ if seconds > 24 * 3600 {+ bail!("shard '{shard}' is over 24h, which risks Loki's max_query_length limit");+ }+ Ok(seconds)+}++#[cfg(test)]+mod tests {+ use super::*;++ const FULL: &str = r#"+version: 1+source:+ url: https://loki.example.com+ tenant: prod+range:+ from: 2025-03-01T00:00:00Z+ to: 2026-09-01T00:00:00Z+ select:+ - '{namespace="prod"}'+extract:+ service_name: { type: label, name: app }+ severity:+ - { type: json_field, path: level }+ - { type: label, name: level }+ resource_labels: [namespace, app]+tuning:+ shard: 1h+"#;++ #[test]+ fn parses_a_full_config() {+ let config = LokiImportConfig::parse(FULL).expect("valid");++ assert_eq!(+ config.extract.service_name.as_slice(),+ [Extractor::Label {+ name: "app".to_string()+ }]+ );+ assert_eq!(config.extract.severity.unwrap().as_slice().len(), 2);+ assert_eq!(config.tuning.shard.seconds(), 3600);+ assert_eq!(config.tuning.max_request_bytes, 1_500_000);+ }++ #[test]+ fn rejects_configs_that_would_fail_hours_into_a_run() {+ // (case, yaml patch applied to FULL, expected substring of the error)+ let cases = [+ (+ "an unknown key",+ FULL.replace("shard: 1h", "shard: 1h\n concurency: 4"),+ "unknown field",+ ),+ (+ "a typo inside an extractor, which must name the field",+ FULL.replace("{ type: label, name: app }", "{ type: label, nmae: app }"),+ "nmae",+ ),+ (+ "a misspelled extractor type",+ FULL.replace("{ type: label, name: app }", "{ type: labl, name: app }"),+ "labl",+ ),+ (+ "a reversed range",+ FULL.replace("from: 2025-03-01T00:00:00Z", "from: 2027-03-01T00:00:00Z"),+ "earlier than",+ ),+ (+ "a range reaching past what Loki nanosecond epochs express",+ FULL.replace("to: 2026-09-01T00:00:00Z", "to: 2099-01-01T00:00:00Z"),+ "day in the future",+ ),+ (+ "an empty selector list",+ FULL.replace(" - '{namespace=\"prod\"}'\n", ""),+ "at least one stream selector",+ ),+ (+ "a blank selector inside the list",+ FULL.replace("'{namespace=\"prod\"}'", "''"),+ "is empty",+ ),+ (+ "a malformed source url",+ FULL.replace("https://loki.example.com", "loki.example.com"),+ "relative URL",+ ),+ (+ "an uncompilable regex",+ FULL.replace(+ "{ type: label, name: app }",+ "{ type: regex, pattern: '([unclosed', group: 1 }",+ ),+ "invalid pattern",+ ),+ (+ "a regex group the pattern does not have",+ FULL.replace(+ "{ type: label, name: app }",+ "{ type: regex, pattern: '(\\w+)', group: 4 }",+ ),+ "capture groups",+ ),+ (+ "a shard that overflows on the unit multiply",+ FULL.replace("shard: 1h", "shard: 1152921504606846977h"),+ "too large",+ ),+ (+ "a shard wider than Loki will answer",+ FULL.replace("shard: 1h", "shard: 48h"),+ "over 24h",+ ),+ (+ "a shard with no unit",+ FULL.replace("shard: 1h", "shard: '90'"),+ "must end in",+ ),+ (+ "a zero shard",+ FULL.replace("shard: 1h", "shard: 0h"),+ "longer than zero",+ ),+ (+ "a body size the intake answers with 413",+ FULL.replace("shard: 1h", "shard: 1h\n max_request_bytes: 50000000"),+ "413",+ ),+ (+ "a body size no batch can fill",+ FULL.replace("shard: 1h", "shard: 1h\n max_request_bytes: 1024"),+ "at least",+ ),+ (+ "an empty resource_labels list, which means the opposite of absent",+ FULL.replace("resource_labels: [namespace, app]", "resource_labels: []"),+ "empty list",+ ),+ (+ "a future config version",+ FULL.replace("version: 1", "version: 2"),+ "unsupported config version",+ ),+ ];++ for (case, yaml, expected) in cases {+ let error = LokiImportConfig::parse(&yaml).unwrap_err_or_else_case(case);+ let rendered = format!("{error:#}");+ assert!(+ rendered.contains(expected),+ "{case}: expected an error mentioning '{expected}', got: {rendered}"+ );+ }+ }++ trait ExpectErrCase {+ fn unwrap_err_or_else_case(self, case: &str) -> anyhow::Error;+ }++ impl ExpectErrCase for Result<LokiImportConfig> {+ fn unwrap_err_or_else_case(self, case: &str) -> anyhow::Error {+ match self {+ Ok(_) => panic!("{case}: expected the config to be rejected, but it parsed"),+ Err(error) => error,+ }+ }+ }++ #[test]+ fn severity_accepts_one_extractor_or_a_list() {+ const SINGLE: &str = r#"+version: 1+source: { url: https://loki.example.com }+range:+ from: 2025-03-01T00:00:00Z+ to: 2026-09-01T00:00:00Z+ select: ['{namespace="prod"}']+extract:+ service_name: { type: label, name: app }+ severity: { type: label, name: level }+"#;++ let config = LokiImportConfig::parse(SINGLE).expect("valid");++ assert_eq!(config.extract.severity.unwrap().as_slice().len(), 1);+ assert_eq!(config.tuning.shard.seconds(), 3600);+ }++ #[test]+ fn parse_shard_converts_units() {+ assert_eq!(parse_shard("30s").unwrap(), 30);+ assert_eq!(parse_shard("15m").unwrap(), 900);+ assert_eq!(parse_shard("6h").unwrap(), 21600);+ }+}diff --git a/cli/src/experimental/logs/mod.rs b/cli/src/experimental/logs/mod.rsnew file mode 100644index 000000000000..6d982acc80b8--- /dev/null+++ b/cli/src/experimental/logs/mod.rs@@ -0,0 +1,2 @@+pub mod command;+pub mod config;diff --git a/cli/src/experimental/mod.rs b/cli/src/experimental/mod.rsindex e0c52ce59c3b..6b3b5f4f8d98 100644--- a/cli/src/experimental/mod.rs+++ b/cli/src/experimental/mod.rs@@ -2,6 +2,7 @@// Things in here should be considered unstable and possibly brokenpub mod endpoints;+pub mod logs;pub mod query;pub mod schema;pub mod tasks;