Skip to content

Commit

Permalink
[refactor] Refactor jptrace/attributes_tests.go for readability (#6786
Browse files Browse the repository at this point in the history
)

## Description of the changes
- Addresses the leftover comments from
#6785

## How was this change tested?
- CI

## Checklist
- [x] I have read
/~https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `npm run lint` and `npm run test`

---------

Signed-off-by: Mahad Zaryab <mahadzaryab1@gmail.com>
  • Loading branch information
mahadzaryab1 authored Feb 28, 2025
1 parent 06cc410 commit a15552f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/jaeger/internal/integration/trace_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *traceReader) FindTraces(
Query: &api_v3.TraceQueryParameters{
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Attributes: jptrace.AttributesToMap(query.Attributes),
Attributes: jptrace.PcommonMapToPlainMap(query.Attributes),
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/app/apiv3/grpc_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (h *Handler) internalFindTraces(
TraceQueryParams: tracestore.TraceQueryParams{
ServiceName: query.GetServiceName(),
OperationName: query.GetOperationName(),
Attributes: jptrace.MapToAttributes(query.GetAttributes()),
Attributes: jptrace.PlainMapToPcommonMap(query.GetAttributes()),
SearchDepth: int(query.GetSearchDepth()),
},
RawTraces: query.GetRawTraces(),
Expand Down
12 changes: 6 additions & 6 deletions internal/jptrace/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ const (
FormatAttribute = "@jaeger@format"
)

func AttributesToMap(attributes pcommon.Map) map[string]string {
tags := make(map[string]string)
func PcommonMapToPlainMap(attributes pcommon.Map) map[string]string {
mapAttributes := make(map[string]string)
attributes.Range(func(k string, v pcommon.Value) bool {
tags[k] = v.AsString()
mapAttributes[k] = v.AsString()
return true
})
return tags
return mapAttributes
}

func MapToAttributes(tags map[string]string) pcommon.Map {
func PlainMapToPcommonMap(attributesMap map[string]string) pcommon.Map {
attributes := pcommon.NewMap()
for k, v := range tags {
for k, v := range attributesMap {
attributes.PutStr(k, v)
}
return attributes
Expand Down
44 changes: 13 additions & 31 deletions internal/jptrace/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
)

func TestAttributesToMap(t *testing.T) {
func TestPcommonMapToPlainMap(t *testing.T) {
tests := []struct {
name string
attributes pcommon.Map
Expand Down Expand Up @@ -54,53 +54,35 @@ func TestAttributesToMap(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := AttributesToMap(test.attributes)
result := PcommonMapToPlainMap(test.attributes)
require.Equal(t, test.expected, result)
})
}
}

func TestMapToAttributes(t *testing.T) {
func TestPlainMapToPcommonMap(t *testing.T) {
tests := []struct {
name string
tags map[string]string
requireFn func(t *testing.T, result pcommon.Map)
name string
expected map[string]string
}{
{
name: "empty map",
tags: map[string]string{},
requireFn: func(t *testing.T, result pcommon.Map) {
require.Equal(t, 0, result.Len(), "Expected map to be empty")
},
name: "empty map",
expected: map[string]string{},
},
{
name: "single tag",
tags: map[string]string{"key1": "value1"},
requireFn: func(t *testing.T, result pcommon.Map) {
val, exists := result.Get("key1")
require.True(t, exists)
require.Equal(t, "value1", val.Str())
},
name: "single attribute",
expected: map[string]string{"key1": "value1"},
},
{
name: "multiple tags",
tags: map[string]string{"key1": "value1", "key2": "value2"},
requireFn: func(t *testing.T, result pcommon.Map) {
val, exists := result.Get("key1")
require.True(t, exists)
require.Equal(t, "value1", val.Str())

val, exists = result.Get("key2")
require.True(t, exists)
require.Equal(t, "value2", val.Str())
},
name: "multiple attributes",
expected: map[string]string{"key1": "value1", "key2": "value2"},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := MapToAttributes(test.tags)
test.requireFn(t, result)
result := PlainMapToPcommonMap(test.expected)
require.Equal(t, test.expected, PcommonMapToPlainMap(result))
})
}
}
2 changes: 1 addition & 1 deletion internal/storage/v2/api/tracestore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (t *TraceQueryParams) ToSpanStoreQueryParameters() *spanstore.TraceQueryPar
return &spanstore.TraceQueryParameters{
ServiceName: t.ServiceName,
OperationName: t.OperationName,
Tags: jptrace.AttributesToMap(t.Attributes),
Tags: jptrace.PcommonMapToPlainMap(t.Attributes),
StartTimeMin: t.StartTimeMin,
StartTimeMax: t.StartTimeMax,
DurationMin: t.DurationMin,
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/v2/v1adapter/spanreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (sr *SpanReader) FindTraces(
getTracesIter := sr.traceReader.FindTraces(ctx, tracestore.TraceQueryParams{
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Attributes: jptrace.MapToAttributes(query.Tags),
Attributes: jptrace.PlainMapToPcommonMap(query.Tags),
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
Expand All @@ -96,7 +96,7 @@ func (sr *SpanReader) FindTraceIDs(
traceIDsIter := sr.traceReader.FindTraceIDs(ctx, tracestore.TraceQueryParams{
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Attributes: jptrace.MapToAttributes(query.Tags),
Attributes: jptrace.PlainMapToPcommonMap(query.Tags),
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
Expand Down

0 comments on commit a15552f

Please sign in to comment.