logtail-rack 0.2.8 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/actions/cleanup-sources/action.yml +33 -0
- data/.github/actions/cleanup-sources/run.sh +51 -0
- data/.github/actions/create-source/action.yml +46 -0
- data/.github/actions/create-source/run.sh +40 -0
- data/.github/actions/verify-logs/action.yml +47 -0
- data/.github/actions/verify-logs/run.sh +104 -0
- data/.github/workflows/e2e.yml +292 -0
- data/.github/workflows/main.yml +0 -3
- data/.github/workflows/notify_linear_failure.yml +153 -0
- data/.gitignore +0 -1
- data/Gemfile +2 -0
- data/e2e/example.sh +66 -0
- data/e2e/expected.json +42 -0
- data/e2e/plan.rb +130 -0
- data/example-project/Gemfile +6 -1
- data/example-project/README.md +9 -13
- data/example-project/config.ru +12 -6
- data/example-project/logging_app.rb +10 -3
- data/lib/logtail-rack/error_event.rb +1 -1
- data/lib/logtail-rack/http_context.rb +22 -10
- data/lib/logtail-rack/http_events.rb +149 -8
- data/lib/logtail-rack/http_request.rb +3 -1
- data/lib/logtail-rack/http_response.rb +3 -1
- data/lib/logtail-rack/middleware.rb +14 -0
- data/lib/logtail-rack/session_context.rb +5 -1
- data/lib/logtail-rack/user_context.rb +3 -0
- data/lib/logtail-rack/util/request.rb +4 -0
- data/lib/logtail-rack/version.rb +1 -1
- data/logtail-ruby-rack.gemspec +1 -1
- metadata +18 -2
- data/example-project/Gemfile.lock +0 -23
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
name: "Notify Linear on failure"
|
|
2
|
+
|
|
3
|
+
# Files a Linear issue for a failed run: "E2E failure: <workflow name> (<owner/repo>)" at High priority, listing the
|
|
4
|
+
# failed jobs and steps with their errors, or with their summaries when job_summaries is set. When every failed job's
|
|
5
|
+
# name starts with "canary" (pre-release runtime or dependencies), the title says "E2E canary failure" and the
|
|
6
|
+
# priority is Low. While an issue with the same title is still open, the run is added to it as a comment instead,
|
|
7
|
+
# so a test that keeps failing every day makes one issue, not one per day.
|
|
8
|
+
# Secrets: LINEAR_API_KEY, LINEAR_TEAM_TELEMETRY_ID, LINEAR_STATUS_PLAN_ID, LINEAR_PROJECT_INGESTION_FOR_EVERYTHING_ID,
|
|
9
|
+
# LINEAR_ASSIGNEE_PETR_ID.
|
|
10
|
+
on:
|
|
11
|
+
workflow_call:
|
|
12
|
+
inputs:
|
|
13
|
+
job_summaries:
|
|
14
|
+
description: >-
|
|
15
|
+
Adds each failed job's summary to the issue, or its errors when it has none. A job hands its summary over as
|
|
16
|
+
an artifact named "e2e-summary-<job name>" holding summary.md.
|
|
17
|
+
type: boolean
|
|
18
|
+
default: false
|
|
19
|
+
|
|
20
|
+
jobs:
|
|
21
|
+
create-linear-issue:
|
|
22
|
+
runs-on: ubuntu-latest
|
|
23
|
+
# A caller must grant checks: read as well (the errors come from the annotations), or this job cannot start.
|
|
24
|
+
permissions:
|
|
25
|
+
contents: read
|
|
26
|
+
actions: read
|
|
27
|
+
checks: read
|
|
28
|
+
steps:
|
|
29
|
+
- name: "Gather failure context"
|
|
30
|
+
id: ctx
|
|
31
|
+
env:
|
|
32
|
+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
33
|
+
RUN_ID: ${{ github.event.workflow_run.id || github.run_id }}
|
|
34
|
+
RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt || github.run_attempt }}
|
|
35
|
+
JOB_SUMMARIES: ${{ inputs.job_summaries }}
|
|
36
|
+
run: |
|
|
37
|
+
JOBS=$(gh api \
|
|
38
|
+
"repos/${{ github.repository }}/actions/runs/$RUN_ID/attempts/$RUN_ATTEMPT/jobs" \
|
|
39
|
+
--paginate \
|
|
40
|
+
--jq '.jobs[]
|
|
41
|
+
| select(.conclusion == "failure")
|
|
42
|
+
| {id, name, steps: ([.steps[] | select(.conclusion == "failure") | .name] | join(", "))}
|
|
43
|
+
| @json')
|
|
44
|
+
|
|
45
|
+
FAILED=""
|
|
46
|
+
while read -r job; do
|
|
47
|
+
[ -n "$job" ] || continue
|
|
48
|
+
ID=$(jq -r .id <<<"$job")
|
|
49
|
+
NAME=$(jq -r .name <<<"$job")
|
|
50
|
+
STEPS=$(jq -r .steps <<<"$job")
|
|
51
|
+
FAILED="$FAILED### $NAME"$'\n\n'"Failed at **${STEPS}**"$'\n\n'
|
|
52
|
+
# A job's summary already says why it failed. Otherwise, or when the job stopped before writing it, the
|
|
53
|
+
# issue gets its ::error:: lines, without the runner's closing "Process completed with exit code 1."
|
|
54
|
+
SUMMARY_DIR="summaries/$ID"
|
|
55
|
+
if [ "$JOB_SUMMARIES" = true ]; then
|
|
56
|
+
gh run download "$RUN_ID" -R "${{ github.repository }}" -n "e2e-summary-$NAME" -D "$SUMMARY_DIR" >/dev/null 2>&1 || true
|
|
57
|
+
fi
|
|
58
|
+
if [ -f "$SUMMARY_DIR/summary.md" ]; then
|
|
59
|
+
FAILED="$FAILED$(cat "$SUMMARY_DIR/summary.md")"$'\n\n'
|
|
60
|
+
else
|
|
61
|
+
ERRORS=$(gh api "repos/${{ github.repository }}/check-runs/$ID/annotations" \
|
|
62
|
+
--jq '[.[] | select(.annotation_level == "failure" and (.message | startswith("Process completed with exit code") | not)) | .message | gsub("\n"; " ")] | unique | .[] | "- \(.)"') || ERRORS=""
|
|
63
|
+
FAILED="$FAILED${ERRORS:+$ERRORS$'\n\n'}"
|
|
64
|
+
fi
|
|
65
|
+
done <<<"$JOBS"
|
|
66
|
+
# Canary only when there are failed jobs and all of them are canary jobs.
|
|
67
|
+
CANARY=false
|
|
68
|
+
if [ -n "$JOBS" ] && jq -se 'all(.[]; .name | startswith("canary"))' <<<"$JOBS" >/dev/null; then CANARY=true; fi
|
|
69
|
+
|
|
70
|
+
while [[ "$FAILED" == *$'\n' ]]; do FAILED="${FAILED%$'\n'}"; done
|
|
71
|
+
if [ -z "$FAILED" ]; then
|
|
72
|
+
FAILED="_(no failed jobs reported by the API yet)_"
|
|
73
|
+
elif [ "${#FAILED}" -gt 20000 ]; then
|
|
74
|
+
FAILED="${FAILED:0:20000}"$'\n\n'"_(cut at 20,000 characters, the rest is in the run)_"
|
|
75
|
+
fi
|
|
76
|
+
printf '%s\n' "$FAILED" > failed_jobs.md
|
|
77
|
+
echo "canary=$CANARY" >> "$GITHUB_OUTPUT"
|
|
78
|
+
|
|
79
|
+
- name: "Create the Linear issue, or comment on the open one with the same title"
|
|
80
|
+
env:
|
|
81
|
+
LINEAR_API_KEY: ${{ secrets.LINEAR_API_KEY }}
|
|
82
|
+
LINEAR_TEAM_ID: ${{ secrets.LINEAR_TEAM_TELEMETRY_ID }}
|
|
83
|
+
LINEAR_STATUS_PLAN_ID: ${{ secrets.LINEAR_STATUS_PLAN_ID }}
|
|
84
|
+
LINEAR_PROJECT_INGESTION_FOR_EVERYTHING_ID: ${{ secrets.LINEAR_PROJECT_INGESTION_FOR_EVERYTHING_ID }}
|
|
85
|
+
LINEAR_ASSIGNEE_PETR_ID: ${{ secrets.LINEAR_ASSIGNEE_PETR_ID }}
|
|
86
|
+
CANARY: ${{ steps.ctx.outputs.canary }}
|
|
87
|
+
REPOSITORY: ${{ github.repository }}
|
|
88
|
+
WORKFLOW: ${{ github.event.workflow_run.name || github.workflow }}
|
|
89
|
+
REF_NAME: ${{ github.event.workflow_run.head_branch || github.ref_name }}
|
|
90
|
+
RUN_URL: ${{ github.event.workflow_run.html_url || format('{0}/{1}/actions/runs/{2}', github.server_url, github.repository, github.run_id) }}
|
|
91
|
+
RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt || github.run_attempt }}
|
|
92
|
+
EVENT_NAME: ${{ github.event.workflow_run.event || github.event_name }}
|
|
93
|
+
SHA: ${{ github.event.workflow_run.head_sha || github.sha }}
|
|
94
|
+
COMMIT_MESSAGE: ${{ github.event.workflow_run.head_commit.message || github.event.head_commit.message }}
|
|
95
|
+
run: |
|
|
96
|
+
if [ "$CANARY" = true ]; then
|
|
97
|
+
TITLE="E2E canary failure: $WORKFLOW ($REPOSITORY)"; PRIORITY=4
|
|
98
|
+
else
|
|
99
|
+
TITLE="E2E failure: $WORKFLOW ($REPOSITORY)"; PRIORITY=2
|
|
100
|
+
fi
|
|
101
|
+
|
|
102
|
+
# head_commit is only populated on push events; skip the subject otherwise
|
|
103
|
+
COMMIT_LINE="**Commit:** \`${SHA:0:7}\`"
|
|
104
|
+
[ -z "$COMMIT_MESSAGE" ] || COMMIT_LINE="$COMMIT_LINE: $(printf '%s' "$COMMIT_MESSAGE" | head -n1)"
|
|
105
|
+
ATTEMPT_NOTE=""
|
|
106
|
+
[ "$RUN_ATTEMPT" = 1 ] || ATTEMPT_NOTE=" (attempt #$RUN_ATTEMPT)"
|
|
107
|
+
{
|
|
108
|
+
echo "**$WORKFLOW** failed on \`$REF_NAME\` via \`$EVENT_NAME\`$ATTEMPT_NOTE"
|
|
109
|
+
echo
|
|
110
|
+
echo "**Failing jobs:**"
|
|
111
|
+
echo
|
|
112
|
+
cat failed_jobs.md
|
|
113
|
+
echo
|
|
114
|
+
echo "$COMMIT_LINE"
|
|
115
|
+
echo
|
|
116
|
+
echo "[View failing run]($RUN_URL)"
|
|
117
|
+
} > description.md
|
|
118
|
+
|
|
119
|
+
linear() {
|
|
120
|
+
curl -sS --fail-with-body -X POST https://api.linear.app/graphql \
|
|
121
|
+
-H "Authorization: $LINEAR_API_KEY" -H 'Content-Type: application/json' -d "$1"
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
# Open means any state but done, canceled or marked as a duplicate. Better a second issue than none: when the
|
|
125
|
+
# lookup fails, a new issue is created.
|
|
126
|
+
FIND=$(jq -nc --arg title "$TITLE" --arg teamId "$LINEAR_TEAM_ID" '{
|
|
127
|
+
query: "query ($title: String!, $teamId: ID!) { issues(filter: {title: {eq: $title}, team: {id: {eq: $teamId}}, state: {type: {nin: [\"completed\", \"canceled\", \"duplicate\"]}}}, first: 1) { nodes { id url } } }",
|
|
128
|
+
variables: {title: $title, teamId: $teamId}
|
|
129
|
+
}')
|
|
130
|
+
FOUND=$(linear "$FIND") || { echo "::warning::looking for an open issue titled \"$TITLE\" failed: $FOUND"; FOUND='{}'; }
|
|
131
|
+
OPEN=$(jq -r '.data.issues.nodes[0] // empty | "\(.id) \(.url)"' <<<"$FOUND")
|
|
132
|
+
|
|
133
|
+
if [ -n "$OPEN" ]; then
|
|
134
|
+
read -r ISSUE_ID ISSUE_URL <<<"$OPEN"
|
|
135
|
+
PAYLOAD=$(jq -nc --arg issueId "$ISSUE_ID" --rawfile body description.md '{
|
|
136
|
+
query: "mutation ($input: CommentCreateInput!) { commentCreate(input: $input) { success } }",
|
|
137
|
+
variables: {input: {issueId: $issueId, body: $body}}
|
|
138
|
+
}')
|
|
139
|
+
RES=$(linear "$PAYLOAD") || true
|
|
140
|
+
jq -e '.data.commentCreate.success' <<<"$RES" >/dev/null || { echo "::error::commenting on $ISSUE_URL failed: $RES"; exit 1; }
|
|
141
|
+
echo "added this run to the open issue $ISSUE_URL"
|
|
142
|
+
else
|
|
143
|
+
PAYLOAD=$(jq -nc --arg title "$TITLE" --rawfile description description.md --arg teamId "$LINEAR_TEAM_ID" \
|
|
144
|
+
--arg assigneeId "$LINEAR_ASSIGNEE_PETR_ID" --arg stateId "$LINEAR_STATUS_PLAN_ID" \
|
|
145
|
+
--arg projectId "$LINEAR_PROJECT_INGESTION_FOR_EVERYTHING_ID" --argjson priority "$PRIORITY" '{
|
|
146
|
+
query: "mutation ($input: IssueCreateInput!) { issueCreate(input: $input) { success issue { url } } }",
|
|
147
|
+
variables: {input: {title: $title, description: $description, teamId: $teamId, assigneeId: $assigneeId,
|
|
148
|
+
stateId: $stateId, projectId: $projectId, priority: $priority}}
|
|
149
|
+
}')
|
|
150
|
+
RES=$(linear "$PAYLOAD") || true
|
|
151
|
+
ISSUE_URL=$(jq -er '.data.issueCreate.issue.url' <<<"$RES") || { echo "::error::creating the issue failed: $RES"; exit 1; }
|
|
152
|
+
echo "created $ISSUE_URL"
|
|
153
|
+
fi
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
|
@@ -5,3 +5,5 @@ gemspec
|
|
|
5
5
|
gem "base64" if RUBY_VERSION >= "3.4.0"
|
|
6
6
|
# No longer a default gem on Ruby 4.0 and TruffleRuby 40, and logtail 0.1.17 requires it undeclared
|
|
7
7
|
gem "logger"
|
|
8
|
+
# Rack 3 moved the session middlewares the tests use into this gem; its 1.x releases for older Racks are empty
|
|
9
|
+
gem "rack-session"
|
data/e2e/example.sh
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Runs the example project the way its README tells a customer to: `bundle exec rackup` in example-project/, a visit
|
|
3
|
+
# to / (the app logs three lines, the middlewares the request and the response) and one to /error (the app raises, the
|
|
4
|
+
# middlewares log the error and a 500 response), then Ctrl+C. The example sends what is still buffered when the
|
|
5
|
+
# server stops, so the server is stopped right after the second request. The server's output goes to out/example.log,
|
|
6
|
+
# and a failure ends with one ::error:: line.
|
|
7
|
+
# Env: BETTER_STACK_SOURCE_TOKEN, BETTER_STACK_INGESTING_HOST, and example-project/Gemfile's gems installed.
|
|
8
|
+
set -euo pipefail
|
|
9
|
+
|
|
10
|
+
ROOT=$(cd "$(dirname "$0")/.." && pwd)
|
|
11
|
+
OUT=$ROOT/out
|
|
12
|
+
LOG=$OUT/example.log
|
|
13
|
+
# rackup's default port. Rack 1 listens on 0.0.0.0, newer ones on localhost, so the requests go to localhost.
|
|
14
|
+
URL=http://localhost:9292
|
|
15
|
+
mkdir -p "$OUT"
|
|
16
|
+
|
|
17
|
+
# The server's output, then the error, if any.
|
|
18
|
+
SERVER=""
|
|
19
|
+
ERROR=""
|
|
20
|
+
fail() {
|
|
21
|
+
ERROR=$1
|
|
22
|
+
exit 1
|
|
23
|
+
}
|
|
24
|
+
finish() {
|
|
25
|
+
[ -z "$SERVER" ] || kill -KILL "$SERVER" 2> /dev/null || true
|
|
26
|
+
cat "$LOG"
|
|
27
|
+
[ -z "$ERROR" ] || echo "::error::$ERROR"
|
|
28
|
+
}
|
|
29
|
+
trap finish EXIT
|
|
30
|
+
|
|
31
|
+
cd "$ROOT/example-project"
|
|
32
|
+
bundle exec rackup > "$LOG" 2>&1 &
|
|
33
|
+
SERVER=$!
|
|
34
|
+
|
|
35
|
+
# Ready once the port takes a connection. A request would be logged and stored as a row of its own.
|
|
36
|
+
for ((second = 0; ; second++)); do
|
|
37
|
+
if (exec 3<> /dev/tcp/localhost/9292) 2> /dev/null; then break; fi
|
|
38
|
+
kill -0 "$SERVER" 2> /dev/null || fail "The example project exited before it listened: $(tail -n 1 "$LOG")"
|
|
39
|
+
[ "$second" -lt 60 ] || fail "The example project did not listen on $URL within 60 s"
|
|
40
|
+
sleep 1
|
|
41
|
+
done
|
|
42
|
+
|
|
43
|
+
request() {
|
|
44
|
+
local status
|
|
45
|
+
status=$(curl -sS -m 30 -o "$OUT/response.txt" -w '%{http_code}' "$URL$1") || status=000
|
|
46
|
+
echo "GET $1 answered HTTP $status"
|
|
47
|
+
[ "$status" = "$2" ] || fail "GET $1 answered HTTP $status instead of $2: $(head -c 300 "$OUT/response.txt" 2> /dev/null)"
|
|
48
|
+
}
|
|
49
|
+
request / 200
|
|
50
|
+
request /error 500
|
|
51
|
+
|
|
52
|
+
# Ctrl+C. rackup traps SIGINT and shuts the server down, then Ruby runs the at_exit hooks.
|
|
53
|
+
kill -INT "$SERVER"
|
|
54
|
+
for ((second = 0; ; second++)); do
|
|
55
|
+
kill -0 "$SERVER" 2> /dev/null || break
|
|
56
|
+
[ "$second" -lt 30 ] || fail "The example project did not stop within 30 s of SIGINT"
|
|
57
|
+
sleep 1
|
|
58
|
+
done
|
|
59
|
+
STATUS=0
|
|
60
|
+
wait "$SERVER" || STATUS=$?
|
|
61
|
+
[ "$STATUS" = 0 ] || fail "The example project exited with status $STATUS: $(tail -n 1 "$LOG")"
|
|
62
|
+
|
|
63
|
+
# logtail 0.1.21 and newer warn about a batch Better Stack rejected, older releases drop it silently.
|
|
64
|
+
if grep -q 'Better Stack rejected' "$LOG"; then
|
|
65
|
+
fail "$(grep -m 1 'Better Stack rejected' "$LOG")"
|
|
66
|
+
fi
|
data/e2e/expected.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"level": "info",
|
|
4
|
+
"message": "Started GET \"/\"",
|
|
5
|
+
"contains": {"event": {"http_request_received": {"method": "GET", "path": "/", "host": "localhost", "port": 9292, "scheme": "http", "headers_json": {"Host": "localhost:9292"}}}, "context": {"runtime": {"file": "lib/logtail-rack/http_events.rb"}, "http": {"method": "GET", "path": "/", "host": "localhost"}}}
|
|
6
|
+
},
|
|
7
|
+
{
|
|
8
|
+
"level": "info",
|
|
9
|
+
"message": "I am using Better Stack! 🚀",
|
|
10
|
+
"contains": {"context": {"runtime": {"file": "logging_app.rb"}, "http": {"method": "GET", "path": "/", "host": "localhost"}}}
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"level": "debug",
|
|
14
|
+
"message": "Logging structured data...",
|
|
15
|
+
"contains": {"name": {"first": "John", "last": "Smith"}, "id": 123456, "context": {"runtime": {"file": "logging_app.rb"}, "http": {"method": "GET", "path": "/", "host": "localhost"}}}
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"level": "warn",
|
|
19
|
+
"message": "Log structured data",
|
|
20
|
+
"contains": {"item": {"url": "https://fictional-store.com/item-123", "price": 100.0}, "context": {"runtime": {"file": "logging_app.rb"}, "http": {"method": "GET", "path": "/", "host": "localhost"}}}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"level": "info",
|
|
24
|
+
"message_prefix": "Completed 200 OK in ",
|
|
25
|
+
"contains": {"event": {"http_response_sent": {"status": 200, "headers_json": {"content-type": "text/plain"}}}, "context": {"runtime": {"file": "lib/logtail-rack/http_events.rb"}, "http": {"method": "GET", "path": "/", "host": "localhost"}}}
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"level": "info",
|
|
29
|
+
"message": "Started GET \"/error\"",
|
|
30
|
+
"contains": {"event": {"http_request_received": {"method": "GET", "path": "/error", "host": "localhost", "port": 9292, "scheme": "http", "headers_json": {"Host": "localhost:9292"}}}, "context": {"runtime": {"file": "lib/logtail-rack/http_events.rb"}, "http": {"method": "GET", "path": "/error", "host": "localhost"}}}
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"level": "fatal",
|
|
34
|
+
"message": "RuntimeError (Visiting /error raises an error. You should see it in Better Stack.)",
|
|
35
|
+
"contains": {"error": {"name": "RuntimeError", "message": "Visiting /error raises an error. You should see it in Better Stack.", "backtrace_json": ["example-project/logging_app.rb:9:in "]}, "context": {"runtime": {"file": "lib/logtail-rack/error_event.rb"}, "http": {"method": "GET", "path": "/error", "host": "localhost"}}}
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"level": "info",
|
|
39
|
+
"message_prefix": "Completed 500 Internal Server Error in ",
|
|
40
|
+
"contains": {"event": {"http_response_sent": {"status": 500}}, "context": {"runtime": {"file": "lib/logtail-rack/http_events.rb"}, "http": {"method": "GET", "path": "/error", "host": "localhost"}}}
|
|
41
|
+
}
|
|
42
|
+
]
|
data/e2e/plan.rb
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# Prints the job matrix of the E2E workflow (.github/workflows/e2e.yml) as JSON.
|
|
2
|
+
#
|
|
3
|
+
# ruby e2e/plan.rb [--leg all|floor|rack2|latest|canary] [--ruby <version>] [--rack <version>] [--logtail <version>] [--source checkout|rubygems]
|
|
4
|
+
#
|
|
5
|
+
# floor Ruby 2.7 with the oldest rack and logtail releases the gemspec allows that run the example (see FLOOR_*)
|
|
6
|
+
# rack2 Ruby 3.2 with the newest Rack 2 and the newest logtail release
|
|
7
|
+
# latest the newest stable Ruby that ruby/setup-ruby installs, with the newest rack and logtail releases. Bundler
|
|
8
|
+
# failing to install them, say a Rack 4 that the gemspec's "< 4.0" excludes, fails the job.
|
|
9
|
+
# canary ruby-head with rack's main branch and the newest logtail release
|
|
10
|
+
#
|
|
11
|
+
# --ruby, --rack and --logtail replace the planned version of every leg that runs with one exact version, which is
|
|
12
|
+
# how a run bisects a failure to one release. --source is copied into every entry: checkout installs the gem built
|
|
13
|
+
# from this checkout, rubygems the newest logtail-rack release, which is then the client version planned.
|
|
14
|
+
#
|
|
15
|
+
# Standard library only, so the plan job needs nothing but a Ruby.
|
|
16
|
+
require "json"
|
|
17
|
+
require "net/http"
|
|
18
|
+
require "optparse"
|
|
19
|
+
require "rubygems"
|
|
20
|
+
|
|
21
|
+
ROOT = File.expand_path("..", __dir__)
|
|
22
|
+
GEMSPEC = File.join(ROOT, "logtail-ruby-rack.gemspec")
|
|
23
|
+
# What the ruby/setup-ruby@v1 that the test job uses can install.
|
|
24
|
+
SETUP_RUBY_VERSIONS_URL = "https://raw.githubusercontent.com/ruby/setup-ruby/v1/ruby-builder-versions.json"
|
|
25
|
+
RUBYGEMS_VERSIONS_URL = "https://rubygems.org/api/v1/versions/%s.json"
|
|
26
|
+
LEGS = %w[floor rack2 latest canary].freeze
|
|
27
|
+
# Job names become artifact names (e2e-summary-<job name>), which must not contain any of these.
|
|
28
|
+
NOT_IN_NAMES = ':"<>|*?/\\'.chars.freeze
|
|
29
|
+
|
|
30
|
+
# The gemspec allows Ruby 2.3, but main.yml tests Rack 1, the oldest the gemspec allows, on Ruby 2.7 only.
|
|
31
|
+
FLOOR_RUBY = "2.7".freeze
|
|
32
|
+
# The gemspec requires logtail 0.1.15 or newer, and the floor leg runs that floor: releases before 0.1.14 have no
|
|
33
|
+
# ingesting_host option, which the example and the README pass, and 0.1.14 calls Rails' String#present? and fails
|
|
34
|
+
# without Rails.
|
|
35
|
+
FLOOR_LOGTAIL = Gem::Version.new("0.1.15")
|
|
36
|
+
# The other Ruby main.yml tests the Rack pins on.
|
|
37
|
+
RACK2_RUBY = "3.2".freeze
|
|
38
|
+
|
|
39
|
+
def fetch_json(url, attempts: 3)
|
|
40
|
+
error = nil
|
|
41
|
+
attempts.times do |attempt|
|
|
42
|
+
# A registry hiccup at schedule time should not file a Linear issue, so the lookup is retried.
|
|
43
|
+
sleep 10 if attempt > 0
|
|
44
|
+
begin
|
|
45
|
+
response = Net::HTTP.get_response(URI(url))
|
|
46
|
+
return JSON.parse(response.body) if response.is_a?(Net::HTTPSuccess)
|
|
47
|
+
|
|
48
|
+
error = "HTTP #{response.code}"
|
|
49
|
+
rescue StandardError => e
|
|
50
|
+
error = e.message
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
abort "GET #{url} failed #{attempts} times, last: #{error}"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Every release of a gem on rubygems.org, without pre-releases, oldest first.
|
|
57
|
+
def releases(name)
|
|
58
|
+
@releases ||= {}
|
|
59
|
+
@releases[name] ||= fetch_json(format(RUBYGEMS_VERSIONS_URL, name))
|
|
60
|
+
.reject { |version| version["prerelease"] }
|
|
61
|
+
.map { |version| Gem::Version.new(version["number"]) }
|
|
62
|
+
.uniq.sort
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def newest_release(name, requirement = Gem::Requirement.default)
|
|
66
|
+
releases(name).select { |version| requirement.satisfied_by?(version) }.max ||
|
|
67
|
+
abort("rubygems.org lists no #{name} release matching #{requirement}")
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def lowest_release(name, requirement)
|
|
71
|
+
releases(name).find { |version| requirement.satisfied_by?(version) } ||
|
|
72
|
+
abort("rubygems.org lists no #{name} release matching #{requirement}")
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def newest_stable_ruby
|
|
76
|
+
fetch_json(SETUP_RUBY_VERSIONS_URL).fetch("ruby")
|
|
77
|
+
.grep(/\A\d+\.\d+\.\d+\z/)
|
|
78
|
+
.max_by { |version| Gem::Version.new(version) }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def dependency(spec, name)
|
|
82
|
+
spec.runtime_dependencies.find { |dependency| dependency.name == name } ||
|
|
83
|
+
abort("#{File.basename(GEMSPEC)} declares no #{name} dependency")
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def plan(leg, spec, ruby: nil, rack: nil, logtail: nil)
|
|
87
|
+
case leg
|
|
88
|
+
when "floor"
|
|
89
|
+
ruby ||= FLOOR_RUBY
|
|
90
|
+
rack ||= lowest_release("rack", dependency(spec, "rack").requirement).to_s
|
|
91
|
+
logtail ||= [lowest_release("logtail", dependency(spec, "logtail").requirement), FLOOR_LOGTAIL].max.to_s
|
|
92
|
+
when "rack2"
|
|
93
|
+
ruby ||= RACK2_RUBY
|
|
94
|
+
rack ||= newest_release("rack", Gem::Requirement.new("~> 2.0")).to_s
|
|
95
|
+
when "latest"
|
|
96
|
+
ruby ||= newest_stable_ruby
|
|
97
|
+
rack ||= newest_release("rack").to_s
|
|
98
|
+
when "canary"
|
|
99
|
+
ruby ||= "head"
|
|
100
|
+
rack ||= "main"
|
|
101
|
+
end
|
|
102
|
+
logtail ||= newest_release("logtail").to_s
|
|
103
|
+
|
|
104
|
+
name = "#{leg} (Ruby #{ruby}, Rack #{rack}, logtail #{logtail})"
|
|
105
|
+
abort "job name #{name.inspect} contains one of #{NOT_IN_NAMES.join}, which artifact names do not allow" if (name.chars & NOT_IN_NAMES).any?
|
|
106
|
+
{
|
|
107
|
+
leg: leg,
|
|
108
|
+
name: name,
|
|
109
|
+
ruby: ruby,
|
|
110
|
+
rack: rack,
|
|
111
|
+
logtail: logtail,
|
|
112
|
+
# The floor and rack2 run on the oldest Ubuntu image GitHub offers, the other legs follow ubuntu-latest.
|
|
113
|
+
runner: %w[floor rack2].include?(leg) ? "ubuntu-24.04" : "ubuntu-latest",
|
|
114
|
+
}
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
options = { leg: "all", source: "checkout" }
|
|
118
|
+
OptionParser.new do |parser|
|
|
119
|
+
parser.on("--leg LEG", ["all", *LEGS])
|
|
120
|
+
parser.on("--ruby VERSION")
|
|
121
|
+
parser.on("--rack VERSION")
|
|
122
|
+
parser.on("--logtail VERSION")
|
|
123
|
+
parser.on("--source SOURCE", %w[checkout rubygems])
|
|
124
|
+
end.parse!(into: options)
|
|
125
|
+
overrides = options.slice(:ruby, :rack, :logtail).transform_values(&:strip).reject { |_name, version| version.empty? }
|
|
126
|
+
|
|
127
|
+
spec = Gem::Specification.load(GEMSPEC) || abort("could not load #{GEMSPEC}")
|
|
128
|
+
client = options[:source] == "rubygems" ? newest_release("logtail-rack").to_s : spec.version.to_s
|
|
129
|
+
legs = options[:leg] == "all" ? LEGS : [options[:leg]]
|
|
130
|
+
puts JSON.generate(legs.map { |leg| plan(leg, spec, **overrides).merge(source: options[:source], client: client) })
|
data/example-project/Gemfile
CHANGED
|
@@ -2,6 +2,11 @@ source "https://rubygems.org"
|
|
|
2
2
|
|
|
3
3
|
gem "rack", "~> 3.0"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
# The rackup command, its own gem since Rack 3. With an older Rack this resolves to an empty release
|
|
6
|
+
# and the rackup command of the rack gem starts the app.
|
|
7
|
+
gem "rackup"
|
|
8
|
+
|
|
9
|
+
# The web server rackup starts, no longer part of Ruby since 3.0
|
|
10
|
+
gem "webrick"
|
|
6
11
|
|
|
7
12
|
gem "logtail-rack", "~> 0.2.6"
|
data/example-project/README.md
CHANGED
|
@@ -10,31 +10,27 @@ You can download the [example project](https://github.com/logtail/logtail-ruby-r
|
|
|
10
10
|
bundle install
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
This will install all dependencies listed in the `Gemfile
|
|
14
|
-
|
|
15
|
-
Then replace `<source_token>` and `<ingesting_host>` in `config.ru` with your actual source token and ingesting host which you can find by going to [Better Stack Telemetry](https://teleemetry.betterstack.com/dashboard) -> Source -> Configure.
|
|
16
|
-
|
|
17
|
-
```ruby
|
|
18
|
-
http_io_device = Logtail::LogDevices::HTTP.new("<source_token>", ingesting_host: "<ingesting_host>")
|
|
19
|
-
```
|
|
13
|
+
This will install all dependencies listed in the `Gemfile`.
|
|
20
14
|
|
|
21
15
|
## Run the example project
|
|
22
16
|
|
|
23
|
-
To run the example application,
|
|
17
|
+
To run the example application, set your source token and ingesting host, which you can find by going to [Better Stack Telemetry](https://telemetry.betterstack.com/dashboard) -> Source -> Configure, and start the server:
|
|
24
18
|
|
|
25
19
|
```bash
|
|
26
|
-
rackup
|
|
20
|
+
BETTER_STACK_SOURCE_TOKEN=<source_token> BETTER_STACK_INGESTING_HOST=<ingesting_host> bundle exec rackup
|
|
27
21
|
```
|
|
28
22
|
|
|
29
|
-
This will start a local server and you visit [http://
|
|
23
|
+
This will start a local server, and you can visit [http://localhost:9292](http://localhost:9292) in your browser.
|
|
30
24
|
|
|
31
25
|
You should see the following output:
|
|
32
26
|
|
|
33
27
|
```bash
|
|
34
28
|
All done!
|
|
35
|
-
Log into your
|
|
29
|
+
Log into your Better Stack account to check your logs.
|
|
36
30
|
```
|
|
37
31
|
|
|
38
|
-
This will create a total of
|
|
32
|
+
This will create a total of 5 different logs: the request, an info, a debug and a warning line from the app, and the response. You can review these logs in Better Stack.
|
|
33
|
+
|
|
34
|
+
You can visit any path on the server to see the request path being logged in context. Visit [/error](http://localhost:9292/error) to see an example exception being logged.
|
|
39
35
|
|
|
40
|
-
|
|
36
|
+
Stop the server with Ctrl+C. The logs still buffered are sent before it exits.
|
data/example-project/config.ru
CHANGED
|
@@ -6,8 +6,17 @@ use Logtail::Integrations::Rack::HTTPContext
|
|
|
6
6
|
use Logtail::Integrations::Rack::HTTPEvents
|
|
7
7
|
use Logtail::Integrations::Rack::ErrorEvent
|
|
8
8
|
|
|
9
|
-
# HTTP IO device sends logs to Better Stack,
|
|
10
|
-
|
|
9
|
+
# HTTP IO device sends logs to Better Stack, to the source whose token and ingesting host are in
|
|
10
|
+
# the BETTER_STACK_SOURCE_TOKEN and BETTER_STACK_INGESTING_HOST environment variables
|
|
11
|
+
source_token = ENV["BETTER_STACK_SOURCE_TOKEN"].to_s
|
|
12
|
+
ingesting_host = ENV["BETTER_STACK_INGESTING_HOST"].to_s
|
|
13
|
+
if source_token.empty? || ingesting_host.empty?
|
|
14
|
+
abort "Usage: BETTER_STACK_SOURCE_TOKEN=<source_token> BETTER_STACK_INGESTING_HOST=<ingesting_host> bundle exec rackup"
|
|
15
|
+
end
|
|
16
|
+
http_io_device = Logtail::LogDevices::HTTP.new(source_token, ingesting_host: ingesting_host)
|
|
17
|
+
|
|
18
|
+
# Sends the logs still buffered when the server stops
|
|
19
|
+
at_exit { http_io_device.close }
|
|
11
20
|
|
|
12
21
|
# STDOUT IO device sends logs to console output
|
|
13
22
|
stdout_io_device = STDOUT
|
|
@@ -17,7 +26,4 @@ logger = Logtail::Logger.new(http_io_device, stdout_io_device)
|
|
|
17
26
|
Logtail::Config.instance.logger = logger
|
|
18
27
|
|
|
19
28
|
# App initialization
|
|
20
|
-
|
|
21
|
-
run do |env|
|
|
22
|
-
logging_app.run(env)
|
|
23
|
-
end
|
|
29
|
+
run LoggingApp.new(logger)
|
|
@@ -5,7 +5,9 @@ class LoggingApp
|
|
|
5
5
|
@logger = logger
|
|
6
6
|
end
|
|
7
7
|
|
|
8
|
-
def
|
|
8
|
+
def call(env)
|
|
9
|
+
raise RuntimeError.new("Visiting /error raises an error. You should see it in Better Stack.") if env["PATH_INFO"].start_with?("/error")
|
|
10
|
+
|
|
9
11
|
@logger.info("I am using Better Stack! 🚀")
|
|
10
12
|
|
|
11
13
|
# You can also provide additional information when logging
|
|
@@ -17,8 +19,13 @@ class LoggingApp
|
|
|
17
19
|
id: 123456
|
|
18
20
|
)
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
@logger.warn("Log structured data",
|
|
23
|
+
item: {
|
|
24
|
+
url: "https://fictional-store.com/item-123",
|
|
25
|
+
price: 100.00
|
|
26
|
+
}
|
|
27
|
+
)
|
|
21
28
|
|
|
22
|
-
[200, {}, ["All done!\nLog into your
|
|
29
|
+
[200, { "content-type" => "text/plain" }, ["All done!\nLog into your Better Stack account to check your logs.\n"]]
|
|
23
30
|
end
|
|
24
31
|
end
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
require "logtail/config"
|
|
1
2
|
require "logtail/contexts/http"
|
|
2
3
|
require "logtail/current_context"
|
|
3
4
|
require "logtail-rack/middleware"
|
|
@@ -10,19 +11,30 @@ module Logtail
|
|
|
10
11
|
# A Rack middleware that is reponsible for adding the HTTP context {Logtail::Contexts::HTTP}.
|
|
11
12
|
class HTTPContext < Middleware
|
|
12
13
|
def call(env)
|
|
13
|
-
|
|
14
|
-
context
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
request_id: request.request_id
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
CurrentContext.with(context.to_hash) do
|
|
14
|
+
context = get_http_context(env)
|
|
15
|
+
if context
|
|
16
|
+
CurrentContext.with(context) do
|
|
17
|
+
@app.call(env)
|
|
18
|
+
end
|
|
19
|
+
else
|
|
23
20
|
@app.call(env)
|
|
24
21
|
end
|
|
25
22
|
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
def get_http_context(env)
|
|
26
|
+
request = Util::Request.new(env)
|
|
27
|
+
Contexts::HTTP.new(
|
|
28
|
+
host: Util::Encoding.force_utf8_encoding(request.host),
|
|
29
|
+
method: Util::Encoding.force_utf8_encoding(request.request_method),
|
|
30
|
+
path: request.path,
|
|
31
|
+
remote_addr: Util::Encoding.force_utf8_encoding(request.ip),
|
|
32
|
+
request_id: request.request_id
|
|
33
|
+
).to_hash
|
|
34
|
+
rescue StandardError => e
|
|
35
|
+
Logtail::Config.instance.debug { "Could not build the HTTP context: #{e.inspect}\n\n#{e.backtrace}" }
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
26
38
|
end
|
|
27
39
|
end
|
|
28
40
|
end
|