@@ -26,6 +26,7 @@ import (
2626
2727 "github.com/stretchr/testify/assert"
2828 "github.com/stretchr/testify/require"
29+ "github.com/twmb/franz-go/pkg/kerr"
2930 "github.com/twmb/franz-go/pkg/kgo"
3031 "go.opentelemetry.io/otel/attribute"
3132 sdkmetric "go.opentelemetry.io/otel/sdk/metric"
@@ -44,17 +45,18 @@ func TestProducerMetrics(t *testing.T) {
4445 t * testing.T ,
4546 producer apmqueue.Producer ,
4647 rdr sdkmetric.Reader ,
48+ name string ,
4749 want []metricdata.Metrics ,
4850 ) {
49- topic := apmqueue .Topic ("default-topic" )
51+ topic := apmqueue .Topic (name )
5052 producer .Produce (ctx ,
5153 apmqueue.Record {Topic : topic , Value : []byte ("1" )},
5254 apmqueue.Record {Topic : topic , Value : []byte ("2" )},
5355 apmqueue.Record {Topic : topic , Value : []byte ("3" )},
5456 )
5557
5658 // Fixes https://github.com/elastic/apm-queue/issues/464
57- <- time .After (1 * time .Millisecond )
59+ <- time .After (time .Millisecond )
5860
5961 // Close the producer so records are flushed.
6062 require .NoError (t , producer .Close ())
@@ -108,7 +110,7 @@ func TestProducerMetrics(t *testing.T) {
108110 }
109111 ctx , cancel := context .WithTimeout (context .Background (), 0 )
110112 defer cancel ()
111- test (ctx , t , producer , rdr , want )
113+ test (ctx , t , producer , rdr , "default-topic" , want )
112114 })
113115 t .Run ("ContextCanceled" , func (t * testing.T ) {
114116 producer , rdr := setupTestProducer (t , nil )
@@ -138,11 +140,11 @@ func TestProducerMetrics(t *testing.T) {
138140 }
139141 ctx , cancel := context .WithCancel (context .Background ())
140142 cancel ()
141- test (ctx , t , producer , rdr , want )
143+ test (ctx , t , producer , rdr , "default-topic" , want )
142144 })
143145 t .Run ("Unknown error reason" , func (t * testing.T ) {
144146 producer , rdr := setupTestProducer (t , nil )
145- want := metricdata.Metrics {
147+ want := [] metricdata.Metrics { {
146148 Name : "producer.messages.count" ,
147149 Description : "The number of messages produced" ,
148150 Unit : "1" ,
@@ -164,9 +166,38 @@ func TestProducerMetrics(t *testing.T) {
164166 },
165167 },
166168 },
167- }
169+ }}
168170 require .NoError (t , producer .Close ())
169- test (context .Background (), t , producer , rdr , []metricdata.Metrics {want })
171+ test (context .Background (), t , producer , rdr , "default-topic" , want )
172+ })
173+ t .Run ("unknown topic" , func (t * testing.T ) {
174+ producer , rdr := setupTestProducer (t , nil )
175+ want := []metricdata.Metrics {{
176+ Name : "producer.messages.count" ,
177+ Description : "The number of messages produced" ,
178+ Unit : "1" ,
179+ Data : metricdata.Sum [int64 ]{
180+ Temporality : metricdata .CumulativeTemporality ,
181+ IsMonotonic : true ,
182+ DataPoints : []metricdata.DataPoint [int64 ]{
183+ {
184+ Value : 3 ,
185+ Attributes : attribute .NewSet (
186+ attribute .String ("outcome" , "failure" ),
187+ attribute .String (errorReasonKey ,
188+ kerr .UnknownTopicOrPartition .Message ,
189+ ),
190+ attribute .String ("namespace" , "name_space" ),
191+ attribute .String ("topic" , "name_space-unknown-topic" ),
192+ semconv .MessagingSystem ("kafka" ),
193+ semconv .MessagingDestinationName ("unknown-topic" ),
194+ semconv .MessagingKafkaDestinationPartition (0 ),
195+ ),
196+ },
197+ },
198+ },
199+ }}
200+ test (context .Background (), t , producer , rdr , "unknown-topic" , want )
170201 })
171202 t .Run ("Produced" , func (t * testing.T ) {
172203 producer , rdr := setupTestProducer (t , func (topic string ) attribute.KeyValue {
@@ -281,7 +312,7 @@ func TestProducerMetrics(t *testing.T) {
281312 }},
282313 },
283314 }
284- test (context .Background (), t , producer , rdr , want )
315+ test (context .Background (), t , producer , rdr , "default-topic" , want )
285316 })
286317 t .Run ("ProducedWithHeaders" , func (t * testing.T ) {
287318 producer , rdr := setupTestProducer (t , func (topic string ) attribute.KeyValue {
@@ -365,7 +396,7 @@ func TestProducerMetrics(t *testing.T) {
365396 "key" : "value" ,
366397 "some key" : "some value" ,
367398 })
368- test (ctx , t , producer , rdr , want )
399+ test (ctx , t , producer , rdr , "default-topic" , want )
369400 })
370401}
371402
0 commit comments