Skip to content

Commit

Permalink
Add tests for csharp metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopgrassi committed Mar 26, 2024
1 parent 5687567 commit 646ca2c
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 63 deletions.
2 changes: 2 additions & 0 deletions internal/common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require go.opentelemetry.io/proto/otlp v1.1.0

require google.golang.org/protobuf v1.33.0

require github.com/google/go-cmp v0.5.8 // indirect

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions internal/common/go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/proto/otlp v1.1.0 h1:2Di21piLrCqJ3U3eXGCTPHE9R8Nh+0uglSnOyxikMeI=
go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7ej/RNTae6MdY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
6 changes: 3 additions & 3 deletions internal/common/testutils/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"google.golang.org/protobuf/proto"
)

func AssertCounter(t *testing.T, tc *MetricTestCase, actualMetrics []*otlpmetrics.Metric) {
func AssertCounter[T Number](t *testing.T, tc *MetricTestCase[T], actualMetrics []*otlpmetrics.Metric) {
// find metric by name
m := findMetric(t, actualMetrics, tc.metricName)

Expand All @@ -22,14 +22,14 @@ func AssertCounter(t *testing.T, tc *MetricTestCase, actualMetrics []*otlpmetric
assert.Equal(t, tc.unit, m.GetUnit())
s := m.GetData().(*v1.Metric_Sum)
dp := s.Sum.DataPoints[0]
assert.Equal(t, tc.value, dp.GetAsDouble())
assert.Equal(t, tc.value, dp.GetAsInt())

for _, exp := range tc.attributes {
assert.Contains(t, dp.Attributes, exp)
}
}

func AssertGauge(t *testing.T, tc *MetricTestCase, actualMetrics []*otlpmetrics.Metric) {
func AssertGauge[T Number](t *testing.T, tc *MetricTestCase[T], actualMetrics []*otlpmetrics.Metric) {
// find metric by name
m := findMetric(t, actualMetrics, tc.metricName)

Expand Down
62 changes: 13 additions & 49 deletions internal/common/testutils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,60 +49,24 @@ func NewSpanTest(options ...SpanTestOption) *SpanTest {
}
}

type MetricTestCase struct {
type Number interface {
int | int64 | float64
}

type MetricTestCase[T Number] struct {
metricName string
description string
unit string
value float64
value T
attributes []*otlpcommon.KeyValue
}
type MetricTestCaseOptions struct {
MetricName string
Description string
Unit string
Value float64
Attributes []*otlpcommon.KeyValue
}

type MetricTestCaseOption func(*MetricTestCaseOptions)

func WithMetricName(name string) MetricTestCaseOption {
return func(s *MetricTestCaseOptions) {
s.MetricName = name
}
}
func WithMetricDescription(description string) MetricTestCaseOption {
return func(s *MetricTestCaseOptions) {
s.Description = description
}
}
func WithMetricUnit(unit string) MetricTestCaseOption {
return func(s *MetricTestCaseOptions) {
s.Unit = unit
}
}
func WithMetricValue(value float64) MetricTestCaseOption {
return func(s *MetricTestCaseOptions) {
s.Value = value
}
}
func WithMetricAttributes(attributes ...*otlpcommon.KeyValue) MetricTestCaseOption {
return func(s *MetricTestCaseOptions) {
s.Attributes = attributes
}
}

func NewMetricTestCase(options ...MetricTestCaseOption) *MetricTestCase {
opts := &MetricTestCaseOptions{}
for _, option := range options {
option(opts)
}

return &MetricTestCase{
metricName: opts.MetricName,
description: opts.Description,
unit: opts.Unit,
value: opts.Value,
attributes: opts.Attributes,
func NewMetricTestCase[T Number](name, description, unit string, value T, attributes ...*otlpcommon.KeyValue) *MetricTestCase[T] {
return &MetricTestCase[T]{
metricName: name,
description: description,
unit: unit,
value: value,
attributes: attributes,
}
}
13 changes: 9 additions & 4 deletions src/csharp/metrics/console/App.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;

namespace Console
{
public class App
{
// Creates the Meter
private static readonly Meter Meter = new("csharp.console.app", "1.0");

// Creates the Counter instrument
private static readonly Counter<int> MyCounter = Meter.CreateCounter<int>("myCounter", "1", "I count things");

// Creates the Gauge instrument passing the callback that will produce the metric values
static App()
{
Expand All @@ -24,7 +26,10 @@ public static void Main(string[] args)
// Configures the SDK, exporting to a local running Collector
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("csharp.console.app")
.AddOtlpExporter()
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("csharp.console.app"))
.AddOtlpExporter(opts => {
opts.Endpoint = new Uri("http://collector-otel-recipes:4317");
})
.Build();

// Add to our counter with an attribute
Expand Down
2 changes: 1 addition & 1 deletion src/csharp/metrics/console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Console</RootNamespace>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/csharp/metrics/console/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source

# Copy csproj and restore as distinct layers
Expand All @@ -9,7 +9,7 @@ COPY . ./
RUN dotnet publish -c Release -o /app --no-cache

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime:7.0
FROM mcr.microsoft.com/dotnet/runtime:8.0
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "Console.dll"]
17 changes: 17 additions & 0 deletions src/csharp/metrics/console/collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
receivers:
otlp:
protocols:
grpc:
http:
processors:
exporters:
debug:
verbosity: detailed
otlphttp:
endpoint: http://otlp-backend:4319
compression: none
service:
pipelines:
metrics:
receivers: [otlp]
exporters: [otlphttp, debug]
37 changes: 37 additions & 0 deletions src/csharp/metrics/console/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: "2.4"
services:

app:
build:
context: .
dockerfile: Dockerfile
depends_on:
- otlp-backend
- collector-otel-recipes
networks:
- otel-recipes

otlp-backend:
build:
context: ../../../../internal/otlp_backend
dockerfile: Dockerfile
ports:
- "4319:4319" # OTLP HTTP receiver
networks:
- otel-recipes

collector-otel-recipes:
image: otel/opentelemetry-collector-contrib:0.96.0
command: ["--config=/etc/collector-config.yaml", "${OTELCOL_ARGS}"]
volumes:
- ./collector-config.yaml:/etc/collector-config.yaml
ports:
- "13133:13133" # health_check extension
- "4317:4317" # OTLP gRPC receiver
- "4318:4318" # OTLP HTTP receiver
depends_on:
- otlp-backend
networks:
- otel-recipes
networks:
otel-recipes:
16 changes: 16 additions & 0 deletions src/csharp/metrics/console/test/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module github.com/joaopgrassi/otel-recipes/csharp/metrics/console

go 1.22.1

require github.com/joaopgrassi/otel-recipes/internal/common v0.0.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/joaopgrassi/otel-recipes/internal/common v0.0.0 => ../../../../../internal/common
18 changes: 18 additions & 0 deletions src/csharp/metrics/console/test/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/proto/otlp v1.1.0 h1:2Di21piLrCqJ3U3eXGCTPHE9R8Nh+0uglSnOyxikMeI=
go.opentelemetry.io/proto/otlp v1.1.0/go.mod h1:GpBHCBWiqvVLDqmHZsoMM3C5ySeKTC7ej/RNTae6MdY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20 changes: 20 additions & 0 deletions src/csharp/metrics/console/test/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package test

import (
"testing"

tu "github.com/joaopgrassi/otel-recipes/internal/common/testutils"
)

func TestMetricsGeneratedFromSample(t *testing.T) {
rm := tu.GetMetricsWithRetry(t, "csharp.console.app")
m := rm.GetScopeMetrics()[0].Metrics

// Counter metric
ctc := tu.NewMetricTestCase("myCounter", "I count things", "1", int64(3), tu.StringAttribute("foo", "bar"))
tu.AssertCounter(t, ctc, m)

// Gauge metric
ctg := tu.NewMetricTestCase("myGauge", "I gauge things", "1", float64(3.5), tu.StringAttribute("foo", "bar"))
tu.AssertGauge(t, ctg, m)
}

0 comments on commit 646ca2c

Please sign in to comment.