Skip to content

Commit

Permalink
feat: redis tests (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer-friedman authored Mar 29, 2023
1 parent 58fbf1e commit 7e0c156
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/expect-opentelemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
toQueryPostgreSQL,
toReceiveGrpcRequest,
toSendGrpcRequest,
toSendRedisCommand,
} from './matchers/service';
import { expect } from '@jest/globals';
import {
GrpcRequest,
HttpRequest,
PostgreSQLQuery,
RedisCommand,
Service,
} from './resources';
export { setDefaultOptions, getDefaultOptions } from './options';
Expand All @@ -24,6 +26,7 @@ const serviceMatchers = {
toQueryPostgreSQL,
toReceiveGrpcRequest,
toSendGrpcRequest,
toSendRedisCommand,
};

interface TraceMatchers {
Expand All @@ -32,6 +35,7 @@ interface TraceMatchers {
toQueryPostgreSQL(): PostgreSQLQuery;
toReceiveGrpcRequest(): GrpcRequest;
toSendGrpcRequest(): GrpcRequest;
toSendRedisCommand(): RedisCommand;
}

function createMatcher(matcher, type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './to-send-http-request';
export * from './to-query-postgresql';
export * from './to-receive-grpc-request';
export * from './to-send-grpc-request';
export * from './to-send-redis-command';
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { opentelemetry } from '@traceloop/otel-proto';
import { RedisCommand, Service } from '../../resources';

export function toSendRedisCommand(service: Service): RedisCommand {
const { name: serviceName, spans } = service;

const filteredSpans = spans.filter((span) => {
return (
span.kind ===
opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_CLIENT &&
span.attributes?.find(
(attribute: opentelemetry.proto.common.v1.IKeyValue) => {
return (
attribute.key === SemanticAttributes.DB_SYSTEM &&
attribute.value?.stringValue === 'redis'
);
},
)
);
});

if (filteredSpans.length === 0) {
throw new Error(`No redis command from ${serviceName} found`);
}

return new RedisCommand(filteredSpans, serviceName);
}
1 change: 1 addition & 0 deletions packages/expect-opentelemetry/src/resources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './http-request';
export * from './service';
export * from './postgresql-query';
export * from './grpc-request';
export * from './redis-command';
47 changes: 47 additions & 0 deletions packages/expect-opentelemetry/src/resources/redis-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { opentelemetry } from '@traceloop/otel-proto';
import {
CompareOptions,
filterByAttributeStringValue,
} from '../matchers/utils';

export class RedisCommand {
constructor(
readonly spans: opentelemetry.proto.trace.v1.ISpan[],
private readonly serviceName: string,
) {}

withDatabaseName(name: string | RegExp, options: CompareOptions) {
const filteredSpans = filterByAttributeStringValue(
this.spans,
SemanticAttributes.DB_NAME,
name,
options,
);

if (filteredSpans.length === 0) {
throw new Error(
`No redis command from service ${this.serviceName} to database ${name} found`,
);
}

return new RedisCommand(filteredSpans, this.serviceName);
}

withStatement(statement: string | RegExp, options: CompareOptions) {
const filteredSpans = filterByAttributeStringValue(
this.spans,
SemanticAttributes.DB_STATEMENT,
statement,
options,
);

if (filteredSpans.length === 0) {
throw new Error(
`No redis command with statement ${statement} from service ${this.serviceName} found`,
);
}

return new RedisCommand(filteredSpans, this.serviceName);
}
}

0 comments on commit 7e0c156

Please sign in to comment.