demo.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. # Copyright (c) Tailscale Inc & AUTHORS
  3. # SPDX-License-Identifier: BSD-3-Clause
  4. #
  5. # This shell script demonstrates writing logs from machines
  6. # and then reprocessing those logs to amalgamate python tracebacks
  7. # into a single log entry in a new collection.
  8. #
  9. # To run this demo, first install the example applications:
  10. #
  11. # go install tailscale.com/logtail/example/...
  12. #
  13. # Then generate a LOGTAIL_API_KEY and two test collections by visiting:
  14. #
  15. # https://log.tailscale.com
  16. #
  17. # Then set the three variables below.
  18. trap 'rv=$?; [ "$rv" = 0 ] || echo "-- exiting with code $rv"; exit $rv' EXIT
  19. set -e
  20. LOG_TEXT='server starting
  21. config file loaded
  22. answering queries
  23. Traceback (most recent call last):
  24. File "/Users/crawshaw/junk.py", line 6, in <module>
  25. main()
  26. File "/Users/crawshaw/junk.py", line 4, in main
  27. raise Exception("oops")
  28. Exception: oops'
  29. die() {
  30. echo "$0: $*" >&2
  31. exit 1
  32. }
  33. msg() {
  34. echo "-- $*" >&2
  35. }
  36. if [ -z "$LOGTAIL_API_KEY" ]; then
  37. die "LOGTAIL_API_KEY is not set"
  38. fi
  39. if [ -z "$COLLECTION_IN" ]; then
  40. die "COLLECTION_IN is not set"
  41. fi
  42. if [ -z "$COLLECTION_OUT" ]; then
  43. die "COLLECTION_OUT is not set"
  44. fi
  45. # Private IDs are 32-bytes of random hex.
  46. # Normally you'd keep the same private IDs from one run to the next, but
  47. # this is just an example.
  48. msg "Generating keys..."
  49. privateid1=$(hexdump -n 32 -e '8/4 "%08X"' /dev/urandom)
  50. privateid2=$(hexdump -n 32 -e '8/4 "%08X"' /dev/urandom)
  51. privateid3=$(hexdump -n 32 -e '8/4 "%08X"' /dev/urandom)
  52. # Public IDs are the SHA-256 of the private ID.
  53. publicid1=$(echo -n $privateid1 | xxd -r -p - | shasum -a 256 | sed 's/ -//')
  54. publicid2=$(echo -n $privateid2 | xxd -r -p - | shasum -a 256 | sed 's/ -//')
  55. publicid3=$(echo -n $privateid3 | xxd -r -p - | shasum -a 256 | sed 's/ -//')
  56. # Write the machine logs to the input collection.
  57. # Notice that this doesn't require an API key.
  58. msg "Producing new logs..."
  59. echo "$LOG_TEXT" | logtail -c $COLLECTION_IN -k $privateid1 >/dev/null
  60. echo "$LOG_TEXT" | logtail -c $COLLECTION_IN -k $privateid2 >/dev/null
  61. # Adopt the logs, so they will be kept and are readable.
  62. msg "Adopting logs..."
  63. logadopt -p "$LOGTAIL_API_KEY" -c "$COLLECTION_IN" -m $publicid1
  64. logadopt -p "$LOGTAIL_API_KEY" -c "$COLLECTION_IN" -m $publicid2
  65. # Reprocess the logs, amalgamating python tracebacks.
  66. #
  67. # We'll take that reprocessed output and write it to a separate collection,
  68. # again via logtail.
  69. #
  70. # Time out quickly because all our "interesting" logs (generated
  71. # above) have already been processed.
  72. msg "Reprocessing logs..."
  73. logreprocess -t 3s -c "$COLLECTION_IN" -p "$LOGTAIL_API_KEY" 2>&1 |
  74. logtail -c "$COLLECTION_OUT" -k $privateid3