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

Another example of usage with some different properties #237

Open
howardjohn opened this issue Jul 7, 2023 · 2 comments
Open

Another example of usage with some different properties #237

howardjohn opened this issue Jul 7, 2023 · 2 comments

Comments

@howardjohn
Copy link

While playing around with this - which is super helpful, thanks! - I came up with another approach than the current demos to meet my needs.

The benefits of this is that:

  • No otel-cli exec, so we can use bash functions
  • No background, so we don't need to deal with the race issues
  • No tp-carrier needed either, which is not a big deal but avoids a lot of temporary files

Just wanted to share and didn't have a good way to do so, so I thought I would open an issue:

#!/bin/bash
export OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317

function gen-uid() {
  tr -dc 'a-f0-9' < /dev/urandom | head -c$1
}

# Usage: trace <span name> [command ...]
function trace() {
  # Throughout, "local" usage is critical to avoid nested calls overwriting things
  local start="$(date -u +%s.%N)"
  # First, get a trace and span ID. We need to get one now so we can propogate it to the child
  # Get trace ID from TRACEPARENT, if present
  local tid="$(<<<${TRACEPARENT:-} cut -d- -f2)"
  tid="${tid:-"$(gen-uid 32)"}"
  # Always generate a new span ID
  local sid="$(gen-uid 16)"

  # Execute the command they wanted with the propogation through TRACEPARENT
  TRACEPARENT="00-${tid}-${sid}-01" "${@:2}"

  local end="$(date -u +%s.%N)"

  # Now report this span. We override the IDs to the ones we set before.
  # TODO: support attributes
  otel-cli span \
    --service "test2" \
    --name "$1" \
    --start "$start" \
    --end "$end" \
    --force-trace-id "$tid" \
    --force-span-id "$sid"
}

function nested() {
  trace "child1" sleep .1
  trace "child2" sleep .2
  trace "deep" deep
}

function deep() {
  trace "in-deep" sleep .1
}

trace "parent" nested

I am happy to close this immediately as an "FYI", send it to demos/, etc, whatever.

@howardjohn
Copy link
Author

Actually got a bit carried away and wrote up a bunch of options: https://blog.howardjohn.info/posts/shell-tracing/

@tobert
Copy link
Collaborator

tobert commented Jul 11, 2023

I would love a PR to add this to demos! I'm also open to having an otel-cli command that dumps some shell code folks can source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants