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

add functionality for --force-parent-span-id #241

Merged
merged 6 commits into from
Jul 14, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ then config file, then environment variables.
| --attrs | OTEL_CLI_ATTRIBUTES | span_attributes | k=v,a=b |
| --force-trace-id | OTEL_CLI_FORCE_TRACE_ID | force_trace_id | 00112233445566778899aabbccddeeff |
| --force-span-id | OTEL_CLI_FORCE_SPAN_ID | force_span_id | beefcafefacedead |
| --force-parent-span-id | OTEL_CLI_FORCE_PARENT_SPAN_ID | force_parent_span_id | eeeeeeb33fc4f3d3 |
| --tp-required | OTEL_CLI_TRACEPARENT_REQUIRED | traceparent_required | false |
| --tp-carrier | OTEL_CLI_CARRIER_FILE | traceparent_carrier_file | filename.txt |
| --tp-ignore-env | OTEL_CLI_IGNORE_ENV | traceparent_ignore_env | false |
Expand Down
13 changes: 8 additions & 5 deletions data_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,27 +922,30 @@ var suites = []FixtureSuite{
},
},
},
// --force-trace-id and --force-span-id allow setting/forcing custom trace & span ids
// --force-trace-id, --force-span-id and --force-parent-span-id allow setting/forcing custom trace, span and parent span ids
{
{
Name: "forced trace & span ids",
Name: "forced trace, span and parent span ids",
Config: FixtureConfig{
CliArgs: []string{
"status",
"--endpoint", "{{endpoint}}",
"--fail",
"--force-trace-id", "00112233445566778899aabbccddeeff",
"--force-span-id", "beefcafefacedead",
"--force-parent-span-id", "e4e3eeb33fc4f3d3",
},
},
Expect: Results{
Config: otlpclient.DefaultConfig().WithEndpoint("{{endpoint}}"),
SpanData: map[string]string{
"trace_id": "00112233445566778899aabbccddeeff",
"span_id": "beefcafefacedead",
"trace_id": "00112233445566778899aabbccddeeff",
"span_id": "beefcafefacedead",
"parent_span_id": "e4e3eeb33fc4f3d3",
},
SpanCount: 1,
Diagnostics: otlpclient.Diagnostics{
NumArgs: 7,
NumArgs: 10,
IsRecording: true,
DetectedLocalhost: true,
ParsedTimeoutMs: 1000,
Expand Down
3 changes: 2 additions & 1 deletion otelcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,10 @@ func addSpanParams(cmd *cobra.Command, config *otlpclient.Config) {
// --kind / -k
cmd.Flags().StringVarP(&config.Kind, "kind", "k", defaults.Kind, "set the trace kind, e.g. internal, server, client, producer, consumer")

// expert options: --force-trace-id, --force-span-id allow setting custom trace & span ids
// expert options: --force-trace-id, --force-span-id, --force-parent-span-id allow setting custom trace, span and parent span ids
cmd.Flags().StringVar(&config.ForceTraceId, "force-trace-id", defaults.ForceTraceId, "expert: force the trace id to be the one provided in hex")
cmd.Flags().StringVar(&config.ForceSpanId, "force-span-id", defaults.ForceSpanId, "expert: force the span id to be the one provided in hex")
cmd.Flags().StringVar(&config.ForceParentSpanId, "force-parent-span-id", defaults.ForceParentSpanId, "expert: force the parent span id to be the one provided in hex")

addSpanStatusParams(cmd, config)
}
Expand Down
2 changes: 2 additions & 0 deletions otlpclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func DefaultConfig() Config {
Kind: "client",
ForceTraceId: "",
ForceSpanId: "",
ForceParentSpanId: "",
Attributes: map[string]string{},
TraceparentCarrierFile: "",
TraceparentIgnoreEnv: false,
Expand Down Expand Up @@ -91,6 +92,7 @@ type Config struct {
StatusCode string `json:"span_status_code" env:"OTEL_CLI_STATUS_CODE"`
StatusDescription string `json:"span_status_description" env:"OTEL_CLI_STATUS_DESCRIPTION"`
ForceSpanId string `json:"force_span_id" env:"OTEL_CLI_FORCE_SPAN_ID"`
ForceParentSpanId string `json:"force_parent_span_id" env:"OTEL_CLI_FORCE_PARENT_SPAN_ID"`
ForceTraceId string `json:"force_trace_id" env:"OTEL_CLI_FORCE_TRACE_ID"`

TraceparentCarrierFile string `json:"traceparent_carrier_file" env:"OTEL_CLI_CARRIER_FILE"`
Expand Down
8 changes: 6 additions & 2 deletions otlpclient/protobuf_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func NewProtobufSpanWithConfig(c Config) *tracepb.Span {
span.SpanId = emptySpanId
}

// --force-trace-id and --force-span-id let the user set their own trace & span ids
// --force-trace-id, --force-span-id and --force-parent-span-id let the user set their own trace, span & parent span ids
// these work in non-recording mode and will stomp trace id from the traceparent
var err error
if c.ForceTraceId != "" {
Expand All @@ -106,6 +106,10 @@ func NewProtobufSpanWithConfig(c Config) *tracepb.Span {
span.SpanId, err = parseHex(c.ForceSpanId, 8)
c.SoftFailIfErr(err)
}
if c.ForceParentSpanId != "" {
span.ParentSpanId, err = parseHex(c.ForceParentSpanId, 8)
c.SoftFailIfErr(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this isn't failing?

I'm really glad you built the test this way, this should be failing and it's problematic if it's not.

}

SetSpanStatus(span, c)

Expand Down Expand Up @@ -297,7 +301,7 @@ func SpanToStringMap(span *tracepb.Span, rss *tracepb.ResourceSpans) map[string]
return map[string]string{
"trace_id": hex.EncodeToString(span.GetTraceId()),
"span_id": hex.EncodeToString(span.GetSpanId()),
"parent": hex.EncodeToString(span.GetParentSpanId()),
"parent_span_id": hex.EncodeToString(span.GetParentSpanId()),
"name": span.Name,
"kind": SpanKindIntToString(span.GetKind()),
"start": strconv.FormatUint(span.StartTimeUnixNano, 10),
Expand Down