Skip to content

Commit e57dc53

Browse files
Manik2708amol-verma-allen
authored andcommitted
[v2] Implement GetOperations and GetServices for memory backend (jaegertracing#7053)
## Which problem is this PR solving? - Fixes a part of: jaegertracing#7052 ## Description of the changes - Implement GetOperations and GetServices for memory backend ## How was this change tested? - Unit tests ## 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: Manik2708 <[email protected]>
1 parent 61ab9dd commit e57dc53

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

internal/storage/v2/memory/fixtures/db_traces_01.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"spanId": "0000000000000002",
2424
"parentSpanId": "0000000000000003",
2525
"flags": 1,
26-
"name": "test-general-conversion",
26+
"name": "test-general-conversion-2",
2727
"startTimeUnixNano": "1485467191639875000",
2828
"endTimeUnixNano": "1485467191639880000",
2929
"attributes": [
@@ -120,7 +120,7 @@
120120
"spanId": "0000000000000004",
121121
"parentSpanId": "0000000000000011",
122122
"flags": 1,
123-
"name": "test-general-conversion",
123+
"name": "test-general-conversion-4",
124124
"startTimeUnixNano": "1485467191639875000",
125125
"endTimeUnixNano": "1485467191639880000",
126126
"attributes": [

internal/storage/v2/memory/fixtures/db_traces_02.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"spanId": "0000000000000003",
2424
"parentSpanId": "0000000000000010",
2525
"flags": 1,
26-
"name": "test-general-conversion",
26+
"name": "test-general-conversion-3",
2727
"startTimeUnixNano": "1485467191639875000",
2828
"endTimeUnixNano": "1485467191639880000",
2929
"attributes": [
@@ -120,7 +120,7 @@
120120
"spanId": "0000000000000005",
121121
"parentSpanId": "0000000000000010",
122122
"flags": 1,
123-
"name": "test-general-conversion",
123+
"name": "test-general-conversion-5",
124124
"startTimeUnixNano": "1485467191639875000",
125125
"endTimeUnixNano": "1485467191639880000",
126126
"attributes": [

internal/storage/v2/memory/fixtures/otel_traces_01.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"spanId": "0000000000000002",
2424
"parentSpanId": "0000000000000003",
2525
"flags": 1,
26-
"name": "test-general-conversion",
26+
"name": "test-general-conversion-2",
2727
"startTimeUnixNano": "1485467191639875000",
2828
"endTimeUnixNano": "1485467191639880000",
2929
"attributes": [
@@ -112,7 +112,7 @@
112112
"spanId": "0000000000000003",
113113
"parentSpanId": "0000000000000010",
114114
"flags": 1,
115-
"name": "test-general-conversion",
115+
"name": "test-general-conversion-3",
116116
"startTimeUnixNano": "1485467191639875000",
117117
"endTimeUnixNano": "1485467191639880000",
118118
"attributes": [
@@ -209,7 +209,7 @@
209209
"spanId": "0000000000000004",
210210
"parentSpanId": "0000000000000011",
211211
"flags": 1,
212-
"name": "test-general-conversion",
212+
"name": "test-general-conversion-4",
213213
"startTimeUnixNano": "1485467191639875000",
214214
"endTimeUnixNano": "1485467191639880000",
215215
"attributes": [
@@ -298,7 +298,7 @@
298298
"spanId": "0000000000000005",
299299
"parentSpanId": "0000000000000010",
300300
"flags": 1,
301-
"name": "test-general-conversion",
301+
"name": "test-general-conversion-5",
302302
"startTimeUnixNano": "1485467191639875000",
303303
"endTimeUnixNano": "1485467191639880000",
304304
"attributes": [

internal/storage/v2/memory/memory.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ func (st *Store) WriteTraces(ctx context.Context, td ptrace.Traces) error {
7777
return nil
7878
}
7979

80+
// GetOperations returns operations based on the service name and span kind
81+
func (st *Store) GetOperations(ctx context.Context, query tracestore.OperationQueryParams) ([]tracestore.Operation, error) {
82+
m := st.getTenant(tenancy.GetTenant(ctx))
83+
m.RLock()
84+
defer m.RUnlock()
85+
var retMe []tracestore.Operation
86+
if operations, ok := m.operations[query.ServiceName]; ok {
87+
for operation := range operations {
88+
if query.SpanKind == "" || query.SpanKind == operation.SpanKind {
89+
retMe = append(retMe, operation)
90+
}
91+
}
92+
}
93+
return retMe, nil
94+
}
95+
96+
// GetServices returns a list of all known services
97+
func (st *Store) GetServices(ctx context.Context) ([]string, error) {
98+
m := st.getTenant(tenancy.GetTenant(ctx))
99+
m.RLock()
100+
defer m.RUnlock()
101+
var retMe []string
102+
for k := range m.services {
103+
retMe = append(retMe, k)
104+
}
105+
return retMe, nil
106+
}
107+
80108
// reshuffleResourceSpans reshuffles the resource spans so as to group the spans from same traces together. To understand this reshuffling
81109
// take an example of 2 resource spans, then these two resource spans have 2 scope spans each.
82110
// Every scope span consists of 2 spans with trace ids: 1 and 2. Now the final structure should look like:

internal/storage/v2/memory/memory_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"os"
13+
"sort"
1314
"testing"
1415

1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
1718
"go.opentelemetry.io/collector/pdata/pcommon"
1819
"go.opentelemetry.io/collector/pdata/ptrace"
20+
conventions "go.opentelemetry.io/collector/semconv/v1.16.0"
1921

2022
v1 "github.com/jaegertracing/jaeger/internal/storage/v1/memory"
23+
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
2124
"github.com/jaegertracing/jaeger/internal/tenancy"
2225
)
2326

@@ -37,6 +40,58 @@ func TestNewStore_DefaultConfig(t *testing.T) {
3740
require.True(t, ok)
3841
expected2 := loadOutputTraces(t, 2)
3942
testTraces(t, expected2, traces2)
43+
operations, err := store.GetOperations(context.Background(), tracestore.OperationQueryParams{ServiceName: "service-x"})
44+
require.NoError(t, err)
45+
expectedOperations := []tracestore.Operation{
46+
{
47+
Name: "test-general-conversion-2",
48+
SpanKind: "Unspecified",
49+
},
50+
{
51+
Name: "test-general-conversion-3",
52+
SpanKind: "Unspecified",
53+
},
54+
{
55+
Name: "test-general-conversion-4",
56+
SpanKind: "Unspecified",
57+
},
58+
{
59+
Name: "test-general-conversion-5",
60+
SpanKind: "Unspecified",
61+
},
62+
}
63+
sort.Slice(operations, func(i, j int) bool {
64+
return operations[i].Name < operations[j].Name
65+
})
66+
assert.Equal(t, expectedOperations, operations)
67+
expectedServices := []string{"service-x"}
68+
services, err := store.GetServices(context.Background())
69+
require.NoError(t, err)
70+
assert.Equal(t, expectedServices, services)
71+
}
72+
73+
func TestGetOperationsWithKind(t *testing.T) {
74+
store := NewStore(v1.Configuration{})
75+
td := ptrace.NewTraces()
76+
resourceSpans := td.ResourceSpans().AppendEmpty()
77+
attrs := resourceSpans.Resource().Attributes()
78+
attrs.PutStr(conventions.AttributeServiceName, "service-x")
79+
span1 := resourceSpans.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
80+
span1.SetTraceID(fromString(t, "00000000000000010000000000000000"))
81+
span1.SetKind(ptrace.SpanKindConsumer)
82+
span1.SetName("operation-with-kind")
83+
span2 := resourceSpans.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
84+
span2.SetTraceID(fromString(t, "00000000000000010000000000000000"))
85+
err := store.WriteTraces(context.Background(), td)
86+
require.NoError(t, err)
87+
operations, err := store.GetOperations(context.Background(), tracestore.OperationQueryParams{
88+
ServiceName: "service-x",
89+
SpanKind: ptrace.SpanKindConsumer.String(),
90+
})
91+
assert.Len(t, operations, 1)
92+
assert.Equal(t, "operation-with-kind", operations[0].Name)
93+
assert.Equal(t, operations[0].SpanKind, ptrace.SpanKindConsumer.String())
94+
require.NoError(t, err)
4095
}
4196

4297
func TestWriteTraces_WriteTwoBatches(t *testing.T) {

0 commit comments

Comments
 (0)